Main topic:
Give you a sequence of length n, and then you have the following actions:
Q: Query the and of the [x, Y] interval.
A: For A[x]+y
S: For A[x]-y
E: End Query
Problem Solving Ideas:
Because n is large, the number of inquiries is also very large, the worst case is that whenever I do a or S, I do a q, such a complexity is n*m,,, will definitely t, then we need to solve the problem of query and modification in the Logn time, think of course is the use of tree-like array, A template question for a bare-naked tree-like array.
Code:
# include<cstdio># include<iostream># include<fstream># include<algorithm># include<functional># include<cstring># include<string># include<cstdlib># include<iomanip># include<numeric># include<cctype># include<cmath># include<ctime># include<queue># include<stack># include<list># include<Set># include<map>using namespacestd;Const DoublePi=4.0*atan (1.0); typedefLong Longll;typedef unsignedLong Longull;# define INF999999999# define MAX50000+4intA[max];intTree[max];Charss[ at];intN;intRead (intPOS) { intAns =0; while(Pos >0) {ans+=Tree[pos]; POS-=pos& (-POS); } returnans;}voidUpdateintPosintval) { while(Pos <=N) {Tree[pos]+=Val; POS+=pos& (-POS); }}voidinit () { for(inti =1; I <= n;i++) {update (i,a[i]); }}intMainvoid){ intIcase =1; intT;SCANF ("%d",&t); while(t--) {memset (A,0,sizeof(a)); memset (Tree,0,sizeof(tree)); scanf ("%d",&N); for(inti =1; I <= n;i++) {scanf ("%d",&A[i]); } //GetChar ();init (); printf ("Case %d:\n", icase++); while(SCANF ("%s", SS)) { if(ss[0] =='E' ) Break; intX, Y;SCANF ("%d%d",&x,&y); if(ss[0] =='Q' ) { intANS1 = Read (X-1); intAns2 =read (y); cout<<ans2-ans1<<Endl; } Else if(ss[0] =='A'{update (x, y); } Else if(ss[0] =='S') {Update (x, (-1)*y); } } } return 0;}
HDU 1166 Enemy soldiers (tree-like array)