Enemy soldiers
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 78528 Accepted submission (s): 33171
Problem Description C State of the death of a country this period is a military exercise, so the C state spy chief Derek and his men tidy again began busy. 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 Input1101 2 3 4 5 6 7 8 9 10Query 1 3Add 3 6Query 2 7Sub 2Add 6 3Query 3 10End
Sample Outputcase 1:63,359
Authorwindbreaker
Recommendeddy | We have carefully selected several similar problems for you:1394 1698 1754 1542 1540
#include <bits/stdc++.h>#defineN 50010using namespacestd;intc[n];intcase,n;intLowbit (intx) { returnx& (-x);}voidUpdateintXintVal) { while(x<=N) {c[x]+=val; X+=Lowbit (x); }}intGetsum (intx) { ints=0; while(x>0) {s+=c[x]; X-=Lowbit (x); } returns;}intmain () {//freopen ("c:\\users\\acer\\desktop\\in.txt", "r", stdin);scanf"%d",&case ); stringop; for(intL=1; l<=case;l++) {memset (c,0,sizeofc); printf ("case %d:\n", l); scanf ("%d",&n); for(intI=1; i<=n;i++) { intcur; scanf ("%d",&cur); Update (i,cur); } while(true) {cin>>op; if(op=="End") break; intb; scanf ("%d%d",&a,&b); if(op=="ADD") Update (a, b); Else if(op=="Sub") Update (a,-b); Else if(op=="Query") printf ("%d\n", Getsum (b)-getsum (a-1)); } //for (int I=1;i<=n;i++)//cout<<c[i]<< ""; //cout<<endl; }}
#include <stdio.h>#include<string.h>Const intmaxn=50010;structnode{intl,r; intsum;} SEGTREE[MAXN*3];voidBuild (intIintLintR//starting from Node 1 update, that is, from the root node of the tree to update, until the left and right son is equal to the leaves, you can stop the update{SEGTREE[I].L=l; SEGTREE[I].R=r; if(l==R) {scanf ("%d",&segtree[i].sum); return; } intMid= (l+r) >>1); Build (i<<1, l,mid); Build ((i<<1)|1, mid+1, r); Segtree[i].sum=segtree[i<<1].sum+segtree[(i<<1)|1].sum;}voidAddintIintTintS//add s to point t,{segtree[i].sum+=s;//each node needs to be updated if(segtree[i].l==segtree[i].r)return;//Until you update to the first T node intMid= (segtree[i].l+segtree[i].r) >>1); if(t<=mid) Add (i<<1, t,s); ElseAdd (i<<1)|1, t,s);}intSumintIintLintR//the value of the interval is added from the root node{ if(segtree[i].l==l&&segtree[i].r==R)returnsegtree[i].sum; intMid= (segtree[i].l+segtree[i].r) >>1); intans=0; if(r<=mid)//left son of current nodeAns=sum (i<<1, l,r); Else if(l>mid)//right son of current nodeAns=sum ((i<<1)|1, l,r); Elseans=sum ((i<<1), l,mid) +sum ((i<<1)|1, mid+1, r); returnans;}intmain () {//freopen ("c:\\users\\acer\\desktop\\in.txt", "r", stdin); intT; intx, y; intn; Charstr[ -]; scanf ("%d",&T); intIcase=0; while(t--) {icase++; scanf ("%d",&n); Build (1,1, n); printf ("case %d:\n", icase); while(SCANF ("%s", &str)! =EOF) { if(STRCMP (str,"End")==0) break; scanf ("%d%d",&x,&y); if(STRCMP (str,"ADD")==0) Add (1, x, y); Else if(STRCMP (str,"Sub")==0) Add (1, x,-y); Elseprintf"%d\n", SUM (1, x, y)); } } return 0;}
HDU 1166 enemy Soldiers (segment tree or Tree-like array)