#include <iostream>#include<cstdio>#include<string>using namespacestd;#defineSize 50000structnode{intL, R, V; Node*Lchild; Node*Rchild;} tree[2*size];//segment tree uses pointers so the entire segment tree occupies twice times the space of the leaf node-1intMid (node*root) { return(Root->l + root->r)/2;}intncount;voidBuildtree (node* root,intLintR)//initializes the segment tree {root] of the interval [l,r]L =L; RootR =R; RootV =0; if(L = =R)return ; Ncount++; Root-Lchild = Tree +ncount; Ncount++; Root-Rchild = Tree +ncount; Buildtree (Root->lchild, L, (l+r)/2 ); Buildtree (Root->rchild, (L+R)/2+1, R);}voidInsert (node* root,intIintv) Insert a number with a value of V on the unit interval I {if(Root->l = = I && root->r = =i) {root->v =v; return ; } Root-V + =the value of the interval on the path where the v;//is updatedif(I <=Mid (Root)) Insert (Root-Lchild, I, v); ElseInsert (Root-Rchild, I, v);}voidADD (node* root,intIintj)//Add j{on Iif(Root->l = = I && root->r = =i) {root->v + =J; return ; } Root->v + =J; if(i<=Mid (Root)) ADD (Root-Lchild, I, J); ElseAdd (Root-Rchild, I, j);}intQuery (node* root,intSinte)//interval [s,e] sum {if(root->l==s && Root->r = =e)returnRoot->V; if(e<=Mid (root))returnQuery (root->Lchild, S, e); Else if(S>=mid (Root) +1 ) returnQuery (root->Rchild, S, e); Else returnQuery (root->Lchild, S, Mid (root))+query (Root->rchild, Mid (root) +1, E);}intMain () {intT, N; CIN>>T; for(intI=1; i<=t; i++) {printf ("Case %d:\n", i); Ncount=0; scanf ("%d", &N); Buildtree (Tree,1, N); intVal; for(intj=1; j<=n; J + +) {scanf ("%d", &Val); Insert (Tree, J, Val); } stringcmd; intA, B; while(Cin>>cmd && cmd!="End"){ if(cmd = ="Query") {scanf ("%d%d", &a, &b); cout<<query (Tree, A, b) <<Endl; } Else if(cmd = ="ADD") {scanf ("%d%d", &a, &b); ADD (Tree, A, b); } Else if(cmd = ="Sub") {scanf ("%d%d", &a, &b); ADD (Tree, A,-b); } } } return 0;}
HDU-1166 enemy Soldiers: proficiency in the "achievements-----interpolation---query" process