Basically knocking the template
Function: Interval update, interval query
Self-development at random.
1#include"iostream"2 using namespacestd;3 #defineLson l,m,rt<<14 #defineRson m+1,r,rt<<1|15 #defineBug cout << "bug!!!" << Endl;6 Const intMAXN =100010;7 8 intsum[maxn<<2];9 intp[maxn<<2];Ten One //Update Father A voidPushup (intRT) - { -SUM[RT] = sum[rt<<1] + sum[rt<<1|1]; the } - - //Decentralized - voidPushdown (intRtintm) + { - if(P[rt]) { +p[rt<<1] = p[rt<<1|1] =P[rt]; Asum[rt<<1] = (M (m>>1)) *P[rt]; atsum[rt<<1|1] = (M >>1) *P[rt]; -P[RT] =0; - } - } - - voidBuildintLintRintRT) in { - if(L = =r) { toCIN >>P[rt]; +SUM[RT] =P[rt]; - return; the } * intm = (l+r) >>1; $ build (Lson);Panax Notoginseng build (Rson); - pushup (RT); the } + A //Interval Update the voidUpdateintLintRintCintLintRintRT) + { - if(l <= l && R <=R) { $P[RT] =C; $SUM[RT] = c* (r-l+1); - return; - } thePushdown (rt,r-l+1); - intm = (l+r) >>1;Wuyi if(L <=m) update (L,r,c,lson); the if(R >m) update (L,r,c,rson); - pushup (RT); Wu } - About //interval Query $ intQueryintLintRintLintRintRT) - { - if(l <= l && R >=r) { - returnSum[rt]; A } +Pushdown (rt,r-l+1);//For example, RT deposit 1-4 and Query 2-5 1-4 will need to devolve the intm = (L + r) >>1; - intRe =0; $ if(L <= m) Re + =query (L,r,lson); the if(R > m) re + =query (L,r,rson); the returnre; the } the - intMain () in { the intN; theCIN >>N; AboutBuild1N1); the intT; theCIN >>T; the for(inti =0; I < t;++i) { + intOK; -CIN >>OK; the if(OK) {Bayi intp1,p2,c; theCin >> P1 >> p2 >>C; theUpdate (P1,P2,C,1N1); - } - Else { the intp1,p2; theCin >> P1 >>P2; thecout << Query (P1,P2,1N1) <<Endl; the } - } the}
code June
Hiho 20th Week (Segment tree template)