Mobile phones
Time Limit: 5000MS | Memory Limit: 65536K |
Description
Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.
Input
The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. Table size: 1 * 1 <= S * S <= 1024 * 1024 Cell value V at any time: 0 <= V <= 32767 Update amount: -32768 <= A <= 32767 No of instructions in input: 3 <= U <= 60002 Maximum number of phones in the whole table: M= 2^30
Output
Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.
Sample Input
0 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3
Sample Output
34
Source
好像没有标记下传的东西= = 不过想想也知道lazy肯定是打在y轴上的。。
Codes:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 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);else60 if(l>T[root].y[i].mid) return queryY(root,Ch2,l,r);else61 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);else67 if(l>T[i].mid) return queryX(Ch2,l,r);else68 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 }