POJ 1195 Mobile Phones 二维树状数组
1 #include<iostream> 2 #include<math.h> 3 #include<string.h> 4 #include<queue> 5 #include<algorithm> 6 #include<cstdio> 7 #define rep(i,a,n) for(i=a;i<=n;i++) 8 #define per(i,a,n) for(i=n;i>=a;i--) 9 #define scd1(a) scanf("%d",&a) 10 #define scd2(a,b) scanf("%d%d",&a,&b) 11 #define ll long long 12 #define mem(a,b) memset(a,b,sizeof a); 13 using namespace std; 14 15 const int maxn=1024; 16 int T,cnt=0,N,M; 17 int mt[maxn+5][maxn+5]; 18 int ist; 19 20 int lowbit(int num){ 21 return num&(-num); 22 } 23 24 void add(int x, int y, int d){ 25 while(x<=N){ 26 int j=y; 27 while(j<=N){ 28 mt[x][j]+=d;j+=lowbit(j); 29 }x+=lowbit(x); 30 } 31 } 32 33 int sum(int x, int y){ 34 int ret=0; 35 while(x>0){ 36 int j=y; 37 while(j>0){ 38 ret+=mt[x][j];j-=lowbit(j); 39 } 40 x-=lowbit(x); 41 } 42 return ret; 43 } 44 45 int main(){ 46 while(~scd2(ist,N)){ 47 mem(mt,0); 48 while(~scd1(ist)&&ist!=3){ 49 if(ist==1){ 50 int X,Y,A; 51 scd2(X,Y);scd1(A);X++;Y++; 52 add(X,Y,A); 53 } 54 if(ist==2){ 55 int L,B,R,T; 56 scd2(L,B);scd2(R,T);L++;B++;R++;T++; 57 int tmp=sum(R,T); 58 tmp+=sum(L-1,B-1); 59 tmp-=sum(L-1,T); 60 tmp-=sum(R,B-1); 61 printf("%d\n",tmp); 62 } 63 } 64 } 65 }
题意描述: 给出一个矩阵, 每次修改一个点的值, 每次质询要求回答一个子矩阵的数值和.
还是裸的二维树状数组, 它的对偶题是POJ2155 Matrix 题解链接:http://www.cnblogs.com/LiXinze/p/7353436.html
一个人要像一支队伍 每一天要像一场战争