poj 1195 Mobile phones(二维树状数组)

来源:互联网 发布:天津网络推广公司 编辑:程序博客网 时间:2024/06/15 16:48

题目大意:

        有一个大小为s的正方形矩阵,有两种操作,第一种:在(x,y)的位置增加数字a,第二种:查询左下角为(l,b),左上角为(r,t)的矩形区域内的数字和。

解题思路:

        二维树状数组模板题。

注意点:

        坐标从0开始,但线段树是从1开始,所以在处理时要给坐标加1.

代码:

#include <iostream>#include <stdio.h>#include <cstring>using namespace std;int c[1050][1050];int s;int lowbit(int x){    return x&(-x);}void updata(int x,int y,int a){     while(x <= s){        int temp = y;        while(temp <= s){            c[x][temp] += a;            temp += lowbit(temp);        }        x += lowbit(x);     }}int query(int x,int y){    int sum = 0;    while(x > 0){        int temp = y;        while(temp > 0){            sum += c[x][temp];            temp -= lowbit(temp);        }        x-= lowbit(x);    }    return sum;}int main(){    int x,y,a,l,b,r,t,comd;    memset(c,0,sizeof(0));    while(true){        scanf("%d",&comd);        if(comd == 0){            scanf("%d",&s);        }        else if(comd == 1){            scanf("%d%d%d",&x,&y,&a);            updata(x+1,y+1,a);        }        else if(comd == 2){            scanf("%d%d%d%d",&l,&b,&r,&t);            printf("%d\n",query(r+1,t+1)-query(l,t+1)-query(r+1,b)+query(l,b));        }        else break;    }    return 0;}





0 0
原创粉丝点击