http://poj.org/problem?id=3468
_ (: Зゝ∠) _ I came back alive, the previous period of time too busy to write the question did not have time to throw up, later.
"Problem description"
Cheng a value, and then asks the interval and.
Ideas
Tell me a few of the places Pushdown and Pushup appear.
Pushup:
(1) At the end of the build, when the leaf nodes have corresponding values, their fathers are equal to their sums.
(2) The end of the update, because at this time the current root of the child has been updated, it also needs to be updated.
Pushdown (Delay Mark):
*pushdown will appear in everything that goes down from the current node.
In query and update, you first pass the value of add to two sons.
The next step is to tidy up the idea of how each of the sub-processes needs to operate:
Build (Create line segment tree)
(1) reset add to zero
(2) If Zuozi equals right subtree, then the leaf node is reached, the value of sum is read, return
(3) recursion to the left and right sub-trees
(4) Pushup update the current root
Update (Modify)
(1) If the current L and R are contained in L and R, let Add+δ, let sum+δ* the length of the current interval, return
(2) Pushdown down delay mark
(3) recursion to the left and right sub-trees
(4) Pushup update the current root
Query (Inquiry)
(1) If the current L and R are contained in L and R, return directly to Sum[rt]
(2) Pushdown down delay mark
(3) Recursive to the left and right subtree, add the resulting return value to the total result
(4) Return result
The general line tree is this operation, in fact, it is quite simple (??). ∀?)?? Recent studies a little busy for a long time did not write the program, tidy up a as to restore the hand speed.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 #defineLson l,m,rt<<18 #defineRson m+1,r,rt<<1|19 #defineLL Long LongTen Const intmaxn=400000+ -; One intn,q; A LL ADD[MAXN]; - LL SUM[MAXN]; - the voidPushup (intRT) - { -sum[rt]=sum[rt<<1]+sum[rt<<1|1]; - } + - voidBuildintLintRintRT) + { Aadd[rt]=0; at if(l==R) - { -scanf"%lld",&Sum[rt]); - return; - } - intM= (L+R)/2; in build (Lson); - build (Rson); to pushup (RT); + } - the voidPushdown (intRtintm) * { $ if(Add[rt])Panax Notoginseng { -add[rt<<1]+=Add[rt]; theadd[rt<<1|1]+=Add[rt]; +sum[rt<<1]+=add[rt]* (M-(m>>1)); Asum[rt<<1|1]+=add[rt]* (m>>1); theadd[rt]=0; + } - } $ $LL Query (intLintRintLintRintRT) - { - if(L<=l && r>=R) the { - returnSum[rt];Wuyi } thePushdown (rt,r-l+1); - intM= (L+R)/2; WuLL rs=0; - if(m>=l) rs+=query (L,r,lson); About if(M<r) rs+=query (L,r,rson); $ returnrs; - } - - voidUpdateintLintRintDeltaintLintRintRT) A { + if(L<=l && r>=R) the { -add[rt]+=Delta; $sum[rt]+= (LL) delta* (r-l+1); the return; the } thePushdown (rt,r-l+1); the intM= (L+R)/2; - if(m>=L) Update (L,r,delta,lson); in if(m<R) Update (L,r,delta,rson); the pushup (RT); the } About the intMain () the { thescanf"%d%d",&n,&q); +Build1N1); - for(intI=0; i<q;i++) the {Bayi Charc[2]; thescanf"%s", c); the if(c[0]=='C') - { - intFr,ed,ad; thescanf"%d%d%d",&fr,&ed,&AD); theUpdate (FR,ED,AD,1N1); the } the Else - { the intb; thescanf"%d%d",&a,&b); theCout<<query (A, B,1N1) <<Endl;94 } the } the return 0; the}
"End Update segment tree template" poj3468-a simple problem with integers