POJ 2029 (二维树状数组)题解

思路:

大力出奇迹,先用二维树状数组存,然后暴力枚举

算某个矩形区域的值的示意图如下,代码在下面慢慢找...

代码:

#include<cstdio>
#include<map>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N = 1030+5;
int bit[N][N],n,m;
int lowbit(int x){
    return x&(-x);
}
void update(int x,int y,int val){
    for(int i = x;i <= n;i += lowbit(i)){
        for(int j = y;j <= m;j += lowbit(j)){
            bit[i][j] += val;
        }
    }
}
int sum(int x,int y){
    int ret = 0;
    for(int i = x;i > 0;i -= lowbit(i)){
        for(int j = y;j > 0;j -= lowbit(j)){
            ret += bit[i][j];
        }
    }
    return ret;
}
int main(){
    int q,x,y;
    while(scanf("%d",&q) && q){
        memset(bit,0,sizeof(bit));
        scanf("%d%d",&n,&m);
        while(q--){
            scanf("%d%d",&x,&y);
            update(x,y,1);
        }
        int S,T;
        scanf("%d%d",&S,&T);
        int x2,y2;
        int MAX = -1;
        for(int x1 = 1;x1 <= n-S+1;x1++){
            for(int y1 = 1;y1 <= m-T+1;y1++){
                x2 = x1 + S - 1;
                y2 = y1 + T - 1;
                int tmp = sum(x2,y2) - sum(x2,y1 - 1) - sum(x1 - 1,y2) + sum(x1 - 1,y1 -1);    //区域
                MAX = tmp > MAX? tmp : MAX;
            }
        }
        printf("%d\n",MAX);
    }
    return 0;
}


posted @ 2018-07-10 20:02  KirinSB  阅读(279)  评论(0编辑  收藏  举报