二维前缀和差分+离散化

/*

二维前缀和求法
a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1];

构建前缀和

int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){//初始化
    for(int j=1;j<=m;j++){
        int x;
        scanf("%d",&x);
        a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]+x;
    }
}

输入nm
以及初始值
进行查询
xx
yy的格子最大值为多少
*/

include

include

using namespace std;
const int maxn=1e4+5;
int a[maxn][maxn];
int b[maxn][maxn];
int main(){
int n,m,q;//n*m格子,进行q次询问
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){//初始化
for(int j=1;j<=m;j++){
int x;
scanf("%d",&x);
a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]+x;
}
}

int xx,yy;
int ans=0;
scanf("%d%d",&xx,&yy);
for(int i=xx;i<=n;i++){
    for(int j=yy;j<=m;j++){
        ans=max(ans,a[i][j]-a[i][j-yy]-a[i-xx][j]+a[i-xx][j-yy]);
    }
}
printf("%d\n",ans);

// int q;
// scanf("%d",&q);
// while(q--){//q次询问
//     int x1,y1,x2,y2,p;
//     scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
//     scanf("%d",&p);
//     b[x1][y1]+=p;b[x2][y2]-=p;
// }

// int add=0;
// for(int i=1;i<=n;i++){//
//     for(int j=1;j<=m;j++){
//         add+=b[i][j];
//         add
//     }
// }

// for(int i=1;i<=n;i++){
//     for(int j=1;j<=m;j++){
//         printf("%d ",a[i][j]);
//     }
//     putchar('\n');
// }

return 0;

}

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1e3+5;
int a[maxn][maxn];
int b[maxn][maxn];
int main(){
    int n,k;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    scanf("%d%d",&n,&k);
    while(n--){
        int x1,x2,y1,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        b[x1+1][y1+1]++;
        b[x2+1][y2+1]++;
        b[x1+1][y2+1]--;
        b[x2+1][y1+1]--;
    }
    int ans=0;
    for(int i=1;i=10;i++){
        for(int j=1;j=10;j++){
            printf("%d\n",j);
            a[i][j]=a[i][j-1]+a[i-1][j]-a[i-1][j-1]+b[i][j];
            if(a[i][j]==k)ans++;
        }
    }
    printf("%d\n",ans);

    return 0;
}

原文地址:https://www.cnblogs.com/Emcikem/p/11563897.html

时间: 2024-07-31 07:25:14

二维前缀和差分+离散化的相关文章

HDU-6514 Monitor(二维前缀和+差分)

http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m. But recently Xiaoteng found that his crops were often stolen by a group of people,

Acwing-121-赶牛入圈(二分, 二维前缀和,离散化)

链接: https://www.acwing.com/problem/content/123/ 题意: 农夫约翰希望为他的奶牛们建立一个畜栏. 这些挑剔的畜生要求畜栏必须是正方形的,而且至少要包含C单位的三叶草,来当做它们的下午茶. 畜栏的边缘必须与X,Y轴平行. 约翰的土地里一共包含N单位的三叶草,每单位三叶草位于一个1 x 1的土地区域内,区域位置由其左下角坐标表示,并且区域左下角的X,Y坐标都为整数,范围在1到10000以内. 多个单位的三叶草可能会位于同一个1 x 1的区域内,因为这个原

二维前缀和好题hdu6514

#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; int n,m,a[20000007]; void add(int i,int j,int val){a[(i-1)*m+j]+=val;} int q(int i,int j) { if(i==0||j==0) return 0; return a[(i-1)*m+j]; } int main(){ int p,Q;

openjudge1768 最大子矩阵[二维前缀和or递推|DP]

总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的矩阵 0 -2 -7 09 2 -6 2-4 1 -4 1-1 8 0 -2 的最大子矩阵是 9 2-4 1-1 8 这个子矩阵的大小是15. 输入 输入是一个N * N的矩阵.输入的第一行给出N (0 < N <= 100).再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给

Good Bye 2015 C. New Year and Domino 二维前缀

C. New Year and Domino They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. Limak is a little polar bear who loves to play. He has recently got a rectangular grid with h rows and w

计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将原矩阵顺时针旋转45°,二维前缀和预处理,然后枚举每一个可能砸到的正方形之和并取最大. 注:枚举的正方形的四个顶点必须是从原矩阵璇转过来的点,代码中用vis数组判断. #include <iostream> #include <stdio.h> #include <string.

弱校联盟10.7 I. Special Squares(二维前缀和)

题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. If arbitrary chosen two lines parallel to x-axis and two lines parallel to y-axis, one rectangle, or sometimes a square, will be formed. If a square i

CDOJ 1256 二维前缀和处理

昊昊喜欢运动 他NN 天内会参加MM 种运动(每种运动用一个[1,m][1,m] 的整数表示) 舍友有QQ 个问题 问昊昊第ll 天到第rr 天参加了多少种不同的运动 Input 输入两个数NN , MM (1≤N≤20001≤N≤2000 , 1≤M≤1001≤M≤100 ); 输入NN 个数aiai 表示在第i天昊昊做了第aiai 类型的运动; 输入一个数QQ (1≤Q≤1061≤Q≤106 ); 输入QQ 行 每行两个数 ll , rr (1≤l≤r≤n1≤l≤r≤n ); Output

二维前缀和 - 算法学习 - 输入输出优化

2017-08-27 11:11:38 writer:pprp 二维前缀和主要用到了容斥定理,具体实现还是有点复杂的 详见代码: /* @theme:二维前缀和 @writer:pprp @declare:用到容斥定理 @date:2017/8/27 */ #include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n, m, a[maxn][maxn]; //输入优化 inline int read() {