HDU 1166 Enemy Soldiers
The simplest single-point update, because it is the first line of the tree, learning the problem of the Internet after writing
1#include <cstdio>2#include <iostream>3 using namespacestd;4 5 Const intMAXN =50000;6 7 structsegment{8 intl,r,sum;9} t[maxn*4];Ten One voidBuildintLintRintk) A { -T[K].L =l; -T[K].R =R; theT[k].sum =0; - if(L = = r)return; - intMid = (L + r) >>1; -Build (L, Mid,2*k); +Build (mid+1R2*k+1); - } + A //Enquiry at intans; - voidQueryintKintLintR) - { - if(T[K].L = = L && T[K].R = =R) - { -Ans + =t[k].sum; in return ; - } to intMid = (t[k].l + T[K].R) >>1; + if(r<=mid) Query (2*k,l,r); - Else if(l>mid) Query (2*k+1, l,r); the Else * { $Query2*k,l,mid);Panax NotoginsengQuery2*k+1, mid+1, R); - } the return ; + } A the voidUpdateintKintDintN) + { - if(T[K].L = = T[K].R && T[K].L = =d) $ { $T[k].sum + = n;//leaf node, updating sum - return ; - } the intMid = (t[k].l + T[K].R) >>1; - if(d <= mid) update (k*2, d, N);ElseUpdate (k*2+1, D, N);Wuyi //Update sum of this node theT[k].sum = t[k*2].sum + t[k*2+1].sum; - } Wu - About $ intMain () - { - intT, Case =0; -scanf"%d",&t); A while(t--) + { the intn,x; -scanf"%d",&n); $Build1N1); the for(inti =1; I <= N; i++) the { thescanf"%d",&x); theUpdate1, i,x); - } inprintf"Case %d:\n",++Case ); the Charstr[ One]; the while(~SCANF ("%s", str) && str[0] !='E') About { the intb; thescanf"%d%d",&a,&b); the if(str[0] =='Q') + { -Ans =0; theQuery1, A, b);Bayiprintf"%d\n", ans); the } the if(str[0] =='A') -Update1, A, b); - if(str[0] =='S') theUpdate1, a,-b); the } the } the return 0; -}
Line Tree FB