标签:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 18489 | Accepted: 8558 |
Description
Input
Output
Sample Input
0 4 1 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3
Sample Output
3 4
Source
1 //树状数组模板 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int flag,X,Y,A,L,B,R,T,S; 7 int squ[1050][1050]; 8 int lowbit(int x) 9 { 10 return x&(-x); 11 } 12 void add(int idx,int idy,int val) 13 { 14 for(int i=idx;i<=1024;i+=lowbit(i)) 15 { 16 for(int j=idy;j<=1024;j+=lowbit(j)) 17 { 18 squ[i][j]+=val; 19 } 20 } 21 } 22 int sum(int idx,int idy) 23 { 24 int s=0; 25 for(int i=idx;i>0;i-=lowbit(i)) 26 { 27 for(int j=idy;j>0;j-=lowbit(j)) 28 { 29 s+=squ[i][j]; 30 } 31 } 32 return s; 33 } 34 int ans(int x1,int y1,int x2,int y2) 35 { 36 return (sum(x2,y2)-sum(x1-1,y2)-sum(x2,y1-1)+sum(x1-1,y1-1)); 37 } 38 int main() 39 { 40 while(scanf("%d",&flag)&&flag!=3) 41 { 42 if(flag==0) 43 { 44 scanf("%d",&S); 45 memset(squ,0,sizeof(squ)); 46 } 47 else if(flag==1) 48 { 49 scanf("%d%d%d",&X,&Y,&A); 50 if(A>=0) 51 add(X+1,Y+1,A); 52 else 53 { 54 int num=ans(X+1,Y+1,X+1,Y+1); 55 add(X+1,Y+1,num+A>0?A:-num); 56 } 57 } 58 else if(flag==2) 59 { 60 scanf("%d%d%d%d",&L,&B,&R,&T); 61 printf("%d\n",ans(L+1,B+1,R+1,T+1)); 62 } 63 } 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/5924788.html