Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 153138 Accepted Submission(s): 63587
Problem Description
A rival country C country's ongoing military exercises this time, the country's spy chief C Derek and his men began to busy Tidy up. A country coastline arranged along a line of the N engineering camps, Derek and Tidy task is to monitor the activities of these engineers camps. As a result of some kind of advanced monitoring tools, so the number of engineers in each camp C crystal clear grasp States, the number of engineers in each camp are likely to occur change, may increase or decrease the number of staff, but they can not escape C monitor the country.
CIA to study enemy tactics exactly what exercises, so Derek Tidy to keep the report a certain period of continuous engineering camp a total of how many people, such as Derek asked: "Tidy, immediately report the first three camps to many people of a total of 10 camps ! "Tidy will immediately begin to calculate the total number of this paragraph and report. But the number of enemy camps often change, and Derek asked each segment is different, so every time a Tidy had to go to a number of camps, soon exhausted, Derek to calculate the speed of the Tidy increasingly dissatisfied: "! you a dead fat boy, considered so slow, I fire you" Tidy thought: "you do the math yourself, which is really a tiring job I wish you fire me too!! "In desperation, Tidy had to call a computer expert to help Windbreaker, Windbreaker, said:"! dead fat boy, do you usually called multi-point acm title and see multi-point arithmetic book, now tasted the bitter fruit of it "Tidy said:" I admitted the mistake ... "but Windbreaker has hung up the phone. Tidy very upset, so he really will count crash, the intelligent reader, you can write a program to help him finish the job? But if your program efficiency is not high enough, Tidy will still be scolded Derek's.
CIA to study enemy tactics exactly what exercises, so Derek Tidy to keep the report a certain period of continuous engineering camp a total of how many people, such as Derek asked: "Tidy, immediately report the first three camps to many people of a total of 10 camps ! "Tidy will immediately begin to calculate the total number of this paragraph and report. But the number of enemy camps often change, and Derek asked each segment is different, so every time a Tidy had to go to a number of camps, soon exhausted, Derek to calculate the speed of the Tidy increasingly dissatisfied: "! you a dead fat boy, considered so slow, I fire you" Tidy thought: "you do the math yourself, which is really a tiring job I wish you fire me too!! "In desperation, Tidy had to call a computer expert to help Windbreaker, Windbreaker, said:"! dead fat boy, do you usually called multi-point acm title and see multi-point arithmetic book, now tasted the bitter fruit of it "Tidy said:" I admitted the mistake ... "but Windbreaker has hung up the phone. Tidy very upset, so he really will count crash, the intelligent reader, you can write a program to help him finish the job? But if your program efficiency is not high enough, Tidy will still be scolded Derek's.
Input
A first line integer T, T set of data expressed.
Data of the first line of each a positive integer N (N <= 50000), there are N represents the enemy camps engineer, the next positive integers with N, has the i ai positive integer representing the i-th ai start engineering camp personal (1 <= ai <= 50 ).
Next, each row having a command, command has four forms:
(. 1) ij of the Add, i, and j is a positive integer, denotes the i th camp increase personal j (j is not more than 30)
(2) Sub ij of, i and j It is a positive integer, denotes the i th camps reduce j individuals (j does not exceed 30);
(. 3) Query ij of, i and j are positive integers, i <= j, presents to ask the total number of i-th to j-th camps;
(4) end indicates the end, the last command that appears in each set of data;
each data command to up to 40,000
Data of the first line of each a positive integer N (N <= 50000), there are N represents the enemy camps engineer, the next positive integers with N, has the i ai positive integer representing the i-th ai start engineering camp personal (1 <= ai <= 50 ).
Next, each row having a command, command has four forms:
(. 1) ij of the Add, i, and j is a positive integer, denotes the i th camp increase personal j (j is not more than 30)
(2) Sub ij of, i and j It is a positive integer, denotes the i th camps reduce j individuals (j does not exceed 30);
(. 3) Query ij of, i and j are positive integers, i <= j, presents to ask the total number of i-th to j-th camps;
(4) end indicates the end, the last command that appears in each set of data;
each data command to up to 40,000
Output
Of the i-th group of data, first outputs "Case i:" and enter,
for each Query query, carriage return and output an integer representing the total number of queries in the segment, this number remains within int.
for each Query query, carriage return and output an integer representing the total number of queries in the segment, this number remains within int.
Sample Input
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
Sample Output
Case 1: 6 33 59
Single-point addition, subtraction, summation interval
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int MAXN=5e4+5; 5 int a[MAXN],st[MAXN<<2]; 6 7 void pushup(int root)///根节点更新操作 8 { 9 st[root]=st[root<<1]+st[root<<1|1]; 10 } 11 12 void build(int l,int r,int root)///建树操作 13 { 14 if(l==r) 15 { 16 st[root]=a[l]; 17 return; 18 } 19 int mid=(l+r)>>1; 20 build(l,mid,root<<1); 21 build(mid+1,r,root<<1|1); 22 pushup(root); 23 } 24 25 void update(int pos,int val,int l,int R & lt, int the root) /// update operation, pos: to be updated position, val: to be updated value 26 is { 27 IF (POS <= L && POS> = R & lt) 28 { 29 [the root] + = ST Val; 30 return ; 31 is } 32 int MID = (L + R & lt) >> . 1 ; 33 is IF (POS <= MID) Update (POS, Val, L, MID, the root << . 1 ); 34 is the else Update (POS, Val, MID + . 1 , R & lt, the root << . 1 | . 1 ); 35 a pushup (the root); 36 } 37 [ 38 is int Query ( int L, int R & lt, int L, int R & lt, int the root) /// query, L: left border query, R: right border query 39 { 40 IF (L <= R & lt && L> = R & lt) return ST [the root]; 41 is int MID = (L + R & lt) >> . 1 , RES = 0 ; 42 is IF (L <= MID) = RES + Query (L, R & lt, L, MID, the root << . 1 ); 43 is IF (R & lt> MID) = RES + Query (L, R & lt, MID + . 1 , R & lt, the root << . 1 | . 1 ); 44 is a pushup (the root); 45 return res; 46 } 47 48 int main() 49 { 50 char s[10]; 51 int t,n,x,y,ca=0; 52 scanf("%d",&t); 53 while(t--) 54 { 55 int flag=0; 56 scanf("%d",&n); 57 for(int i=1;i<=n;i++) 58 { 59 scanf("%d",&a[i]); 60 } 61 memset(st,0,sizeof st); 62 build(1,n,1); 63 while(scanf("%s",s),strcmp(s,"End")) 64 { 65 scanf("%d%d",&x,&y); 66 if(strcmp(s,"Query")==0) 67 { 68 if(flag==0) 69 { 70 printf("Case %d:\n",++ca); 71 flag=1; 72 } 73 printf("%d\n",query(x,y,1,n,1)); 74 } 75 else if(strcmp(s,"Add")==0) 76 { 77 update(x,y,1,n,1); 78 } 79 else if(strcmp(s,"Sub")==0) 80 { 81 update(x,-y,1,n,1); 82 } 83 } 84 } 85 }