HDU 5828 (segment tree)

Source: Internet
Author: User

Problem Rikka with Sequence

Main topic

Maintains a sequence that supports three operations.

Action 1: Interval plus.  Operation two: Interval open square root (rounding down). Action 3: Interval summation.

Analysis of Problem solving

It can be found that after several operations, the number in the whole interval tends to be the same.

You can also open a marker to indicate whether the number in this interval is the same, thus optimizing the complexity of the interval open radical.

And then the card is over.

When pushdown, be careful not to pass a lazy tag down two times.

Reference Program

1#include <cstdio>2#include <cmath>3 4 #defineMAXN 2000085 #defineLson L, M, RT << 16 #defineRson m+1, R, RT << 1 | 17 #defineEPS 1e-88 Long LongSUM[MAXN <<2],LAZY[MAXN <<2];9 Long LongTAG[MAXN <<2];Ten intN; One  A voidPushup (intRT) { -SUM[RT] = Sum[rt <<1] + sum[rt <<1|1]; -     if(Tag[rt <<1] = = Tag[rt <<1|1]) Tag[rt]=tag[rt <<1];ElseTAG[RT] =0; the } -  - voidPushdown (intRtintm) { -     if(Tag[rt]) { +Tag[rt <<1] =Tag[rt]; -Tag[rt <<1|1] =Tag[rt]; +Sum[rt <<1] = tag[rt] * (m-m/2); ASum[rt <<1|1] = tag[rt] * (m/2); atlazy[rt]=0; -     } -     if(Lazy[rt]) { -Lazy[rt <<1] +=Lazy[rt]; -Lazy[rt <<1|1] +=Lazy[rt]; -Sum[rt <<1] + = lazy[rt] * (M-M/2); inSum[rt <<1|1] + = lazy[rt] * (M/2); -         if(Tag[rt <<1]) Tag[rt <<1] +=Lazy[rt]; to         if(Tag[rt <<1|1]) Tag[rt <<1|1] +=Lazy[rt]; +LAZY[RT] =0; -     } the } *  $ voidBuild (intLintRintRT) {Panax NotoginsengLAZY[RT] =0; -TAG[RT] =0; the     if(l==r) { +scanf"%d",&Sum[rt]); Atag[rt]=Sum[rt]; the         return; +     } -     intm = (L + r) >>1; $ Build (Lson); $ Build (Rson); - pushup (RT); - } the  - voidUpdate (intLintRintValintLintRintRT) {Wuyi     if(l <= l && R <=R) { theSUM[RT] + = 1ll* (r-l +1) *Val; -LAZY[RT] + =Val; Wu         if(Tag[rt]) tag[rt]+=Val; -         return; About     } $Pushdown (Rt,r-l +1); -     intm = (L + r) >>1; -     if(L <=m) Update (L,r,val,lson); -     if(M <R) Update (L,r,val,rson); A pushup (RT); + } the  - voidUpdate_2 (intLintRintLintRintRT) { $     if(L<=l && r<=r &&Tag[rt]) { theTAG[RT] = (Long Long) (sqrt (Tag[rt]) +EPS); theSUM[RT] = 1ll*tag[rt] * (R-l +1); the         return; the     } -Pushdown (Rt,r-l +1); in     intm = (L + r) >>1; the     if(L <=m) update_2 (L,r,lson); the     if(M <R) update_2 (L,r,rson); About pushup (RT); the } the  the Long LongQuery (intLintRintLintRintRT) { +     if(l <= l && R <=R) { -         returnSum[rt]; the     }BayiPushdown (Rt,r-l +1); the     intm = (L + r) >>1; the     Long Longres =0; -     if(L <= m) Res + =Query (L,r,lson); -     if(M < R) Res + =Query (L,r,rson); the     returnRes; the } the  the  - intMain () { the     intT; thescanf"%d",&T); the      while(t--){94         intQ; thescanf"%d%d",&n,&Q); theBuild (1N1); the          while(q--) {98             intx,y,z,w; Aboutscanf"%d",&W); -             if(w==1) {101scanf"%d%d%d",&x,&y,&z);102Update (x, Y, Z,1N1);103             }104             if(w==2){ thescanf"%d%d",&x,&y);106Update_2 (x, Y,1N1);107             }108             if(w==3){109scanf"%d%d",&x,&y); theprintf"%i64d\n", Query (x, Y,1N1)); 111             } the         }113     } the}
View Code

HDU 5828 (segment tree)

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.