Enemy soldiers
C State of the death of a country during the period of military exercises, so the C state spy chief Derek and his men tidy began to busy again. Country A has a line of N engineering camps along the coastline, and Derek and tidy are tasked with monitoring the activities of these engineering camps. As a result of the adoption of some advanced monitoring tools, the number of people in each engineering camp, C, is well-informed and the number of personnel in each engineering camp is likely to change, possibly by increasing or reducing the number of staff, but none of them can escape the surveillance of State C.
The CIA wants to study what tactics the enemy is practicing, so Tidy to report to Derek at any time about how many people there are in a succession of engineering camps, such as Derek asking: "Tidy, Report to the 3rd camp to the 10th camp." Tidy is about to start calculating the total number of people in this section and report it immediately. But the number of enemy barracks is constantly changing, and Derek Each asked the paragraph is not the same, so tidy had to every time a camp to count, quickly exhausted, Derek to tidy's calculation speed more and more dissatisfied: "You a dead Fat boy, calculate so slow, I fire you squid!" Tidy thought, "You can count on yourself, it's a tiring job!" I wish you would fire me! " Helpless under, tidy had to call computer experts windbreaker for help, Windbreaker said: "Die fat, you usually do more than a point of ACM and see more Algorithms book, now taste the bitter pill!" Tidy said: "I know the wrong ... "But Windbreaker already hung up the phone." Tidy is very distressed, so he really will collapse, smart Reader, can you write a program to help him finish the work? But if your program is not efficient enough, tidy will still be scolded by Derek.
Input the first line of an integer t, indicating that there is a T group of data.
The first line of each group of data is a positive integer n (n<=50000), which indicates that the enemy has N engineering camps, followed by n positive integers, and the second positive integer AI represents the first AI individual (1<=ai<=50) in the field of the I barracks.
Next, there is one command in each line, with 4 forms of command:
(1) Add I j,i and J are positive integers, indicating that I camp increase J person (J not more than 30)
(2) Sub i J, I and J are positive integers, indicating that the I camp reduced J person (j not more than 30);
(3) Query i J, I and J are positive integers, i<=j, which indicate the total number of persons who have inquired about the first and the J camps;
(4) End indicates that the command ends in each group of data;
Up to 40,000 commands per set of data
Output to group I data, first outputs "case I:" and carriage return,
For each query, output an integer and enter it to indicate the total number of people in the segment you are asking for, which remains within Int.
Sample Input
Sample Output
Case 1:63,359
Chinese question Emmmm
Tree-like array
#include <iostream>#include<cstdio>#include<cstring>#include<ctime>#include<cstdlib>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<stack>#include<list>#include<Set>#include<map>#include<vector>using namespacestd;#defineLL Long Long#defineMem (A, B) memset (A,b,sizeof (a))#defineP pair< int, int >Const intmaxn=50000+Ten;Const intINF =0x3f3f3f3f3f;intTree[maxn],n;voidAddintKintnum) { while(k<=N) {tree[k]+=num; K+=k& (-k); }}intSumintk) { intans=0; while(k) {ans+=Tree[k]; K-=k& (-k); } returnans;}intMain () {intt,x,y,a,k=1; Charp[6]; scanf ("%d",&t); while(t--) {printf ("Case %d:\n", k++); memset (Tree,0,sizeof(tree)); scanf ("%d",&N); for(intI=1; i<=n; i++) {scanf ("%d",&a); Add (I,a); } while(~SCANF ("%s", p)) { if(p[0]=='E') Break; scanf ("%d%d",&x,&y); if(p[0]=='Q') printf ("%d\n", sum (y)-sum (X-1)); Else if(p[0]=='A') {Add (x, y); } Else if(p[0]=='S') {Add (x,-y); } } } return 0;}
Segment Tree
#include <iostream>#include<cstdio>#include<conio.h>#include<cstdlib>#include<cstring>#include<ctime>using namespacestd;Const intMAXN =50000+Ten;intsum[maxn<<2],a[maxn<<2];voidPushup (introot) {Sum[root]=sum[root<<1]+sum[root<<1|1];}voidBuildintLintRintroot) { if(l==R) {Sum[root]=A[l]; return ; } intM= (l+r) >>1; Build (L,m,root<<1); Build (M+1,r,root<<1|1); Pushup (root);}voidUpdata (intLintCintLintRintroot) { if(l==R) {Sum[root]+=C; return ; } intM= (l+r) >>1; if(l<=m) updata (L,c,l,m,root<<1); ElseUpdata (l,c,m+1,r,root<<1|1); Pushup (root);}intQueryintLintRintLintRintroot) { if(l<=l&&r>=R)returnSum[root]; intM= (l+r) >>1; intans=0; if(l<=m) Ans+=query (l,r,l,m,root<<1); if(r>m) Ans+=query (l,r,m+1,r,root<<1|1); returnans;}intMain () {intT, C; Chard[Ten]; scanf ("%d", &t); for(c =1; C <= t; C++) {printf ("Case %d:\n", c); intN; scanf ("%d", &N); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Build (1N1); while(SCANF ("%s", d)! =EOF) { if(d[0] =='E') Break; intx, y; scanf ("%d%d", &x, &y); if(d[0] =='Q') { intAns = query (x, Y,1N1); printf ("%d\n", ans); } if(d[0] =='S') Updata (x, Y,1N1); if(d[0] =='A') Updata (x, Y,1N1); } } return 0;}
PS: Fish monsters to share the blog, welcome to thank each way Daniel's Guide ~
HDU-1166 enemy soldiers (line tree && tree-like array primer)