Learn the third day of line tree ... Really did not learn something good, but also a water problem, pure template, I personally feel that my Line segment tree template is still good (after all, my first day equivalent to nothing to learn ...) Find a whole day template, compared to several, finally found their favorite type, Chinese topic, directly on the code
I feel that my code has a characteristic bar ... A bit verbose, but each line of thinking and the previous line is closely linked, the novice to see the words will not feel the leap of thinking, it is easier to understand, because I looked at some of the great God's code, do not see half of what he said below what the meaning of ... So I hope my code can benefit the novice. =
1#include <cstdio>2#include <string>3#include <iostream>4 #defineN 500035 using namespacestd;6 intnum[50003];7 Chars[5];8 structnod9 {Ten intData,l,r; One}tree[n*4];//tree structure to spend more space, here to A - voidPUSH_UP (inti)//Update I node via child node of I node - { theTree[i].data = tree[i*2].data + tree[i*2+1].data; - } - - voidBuild_tree (intIintLintR) + { -TREE[I].L =l; +TREE[I].R =R; A if(L = =R) { atTree[i].data =Num[l]; - return ; - } - intMid = (L + r)/2; -Build_tree (i*2, l,mid); -Build_tree (i*2+1, mid+1, R); in push_up (i); - } to + voidUpdateintIintKintv) - { the if(tree[i].l==k&&tree[i].r==k) {//single-point update to find a specific K-node if found, interval update to find a range → look at my other article. *Tree[i].data + =v;//Note that the title means to increase or decrease the V-person, so is + = $ return;Panax Notoginseng } - intMid = (TREE[I].L + TREE[I].R)/2; the if(k <=mid) +Update (i*2, k,v); A Else theUpdate (i*2+1, k,v); + push_up (i); - } $ $ intQueryintIintLintR) - { - if(l<=tree[i].l&&tree[i].r<=R) { the returnTree[i].data; - }Wuyi intMid = (TREE[I].L + TREE[I].R)/2; the if(R <=mid)//do not understand here to see me another article http://www.cnblogs.com/liwenchi/p/5761257.html - returnQuery (i*2, l,r); Wu if(L >mid) - returnQuery (i*2+1, l,r); About returnQuery (i*2, L,r) +query (i*2+1, l,r); $ } - - intMain () - { A intNoc,ug;//UG This name casually up ... the case number equals Ug-noc,noc is also casually up ... numbers of cases +scanf"%d",&NOC); theUG =NOC; - while(noc--) $ { the intn,l,r,i,j; thescanf"%d",&n); the for(intI=1; i<=n;i++) thescanf"%d",&num[i]); -Build_tree (1,1, n); inprintf"Case %d:\n", ug-NOC); the while(SCANF ("%s", s)) the { About if(s[0] =='E') Break; the if(s[0] =='Q'){ thescanf"%d%d",&l,&R); theprintf"%d\n", Query (1, L,r)); + } - Else if(s[0] =='A'){ thescanf"%d%d",&i,&j);BayiUpdate1, i,j); the } the Else{ -scanf"%d%d",&i,&j);//reduce the number of words to the J minus the update on the OK -Update1, i,-j); the } the } the } the}
If there is no understanding where you can see my other line of the tree article, are very basic!
"Original" Hdu 1166 Enemy Soldiers (line segment tree → single point update, interval query)