Hiho 20th Week (Segment tree template)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.