标签:des style blog http color strong
Time Limit: 5000MS | Memory Limit: 65536K |
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 #include<set> 2 #include<queue> 3 #include<vector> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 const int N = 1025; 11 #define Ch1 ((i)<<1) 12 #define Ch2 ((Ch1)|1) 13 #define For(i,n) for(int i=1;i<=n;i++) 14 #define Rep(i,l,r) for(int i=l;i<=r;i++) 15 16 struct tnodey{ 17 short l,r,mid; 18 int sum; 19 }; 20 21 struct tnodex{ 22 short l,r,mid; 23 tnodey y[N*3]; 24 }T[N*3]; 25 26 int op,n,A; 27 int x1,y1,x2,y2,x,y; 28 29 void BuildY(int root,int i,int l,int r){ 30 T[root].y[i].l = l;T[root].y[i].r = r; T[root].y[i].mid = (l+r)>>1; 31 T[root].y[i].sum = 0; 32 if(l==r) return; 33 BuildY(root,Ch1,l,T[root].y[i].mid); BuildY(root,Ch2,T[root].y[i].mid+1,r); 34 } 35 36 void BuildX(int i,int l,int r){ 37 BuildY(i,1,0,n); 38 T[i].l = l; T[i].r = r; T[i].mid = (l+r)>>1; 39 if(l==r) return; 40 BuildX(Ch1,l,T[i].mid);BuildX(Ch2,T[i].mid+1,r); 41 } 42 43 void ModifyY(int root,int i,int x,int delta){ 44 if(T[root].y[i].l==T[root].y[i].r) {T[root].y[i].sum += delta;return;} 45 if(x<=T[root].y[i].mid) ModifyY(root,Ch1,x,delta); 46 else ModifyY(root,Ch2,x,delta); 47 T[root].y[i].sum = T[root].y[Ch1].sum + T[root].y[Ch2].sum; 48 } 49 50 void ModifyX(int i,int x,int delta){ 51 ModifyY(i,1,y,delta); 52 if(T[i].l==T[i].r) return; 53 if(x<=T[i].mid) ModifyX(Ch1,x,delta); 54 else ModifyX(Ch2,x,delta); 55 } 56 57 int queryY(int root,int i,int l,int r){ 58 if(l<=T[root].y[i].l&&T[root].y[i].r<=r) return T[root].y[i].sum; 59 if(r<=T[root].y[i].mid) return queryY(root,Ch1,l,r);else 60 if(l>T[root].y[i].mid) return queryY(root,Ch2,l,r);else 61 return queryY(root,Ch1,l,T[root].y[i].mid) + queryY(root,Ch2,T[root].y[i].mid+1,r); 62 } 63 64 int queryX(int i,int l,int r){ 65 if(l<=T[i].l&&T[i].r<=r) return queryY(i,1,y1,y2); 66 if(r<=T[i].mid) return queryX(Ch1,l,r);else 67 if(l>T[i].mid) return queryX(Ch2,l,r);else 68 return queryX(Ch1,l,T[i].mid) + queryX(Ch2,T[i].mid+1,r); 69 } 70 71 void init(){ 72 scanf("%d%d",&op,&n);n--; 73 BuildX(1,0,n); 74 while(op!=3){ 75 scanf("%d",&op); 76 if(op==1){ 77 scanf("%d%d%d",&x,&y,&A); 78 ModifyX(1,x,A); 79 } 80 if(op==2){ 81 scanf("%d%d%d%d",&x1,&y1,&x2,&y2); 82 printf("%d\n",queryX(1,x1,x2)); 83 } 84 } 85 } 86 87 int main(){ 88 init(); 89 return 0; 90 }
Mobile phones·POJ1195,布布扣,bubuko.com
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/kjerome/p/3818637.html