hdu4578 (segment tree)

Source: Internet
Author: User
Tags mul

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=4578

Test instructions: N number, initial value is 0, 4 operations:

1. Add another value to all values in an interval;

2. Multiply all the values of the interval by another value;

3. Place all values of the interval to a certain value;

4. The P-side and of all values in the query interval.

Detailed Analysis: http://www.cnblogs.com/GBRgbr/archive/2013/08/13/3254442.html

#pragmaComment (linker, "/stack:102400000,102400000")#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 10007#defineINF 0x3f3f3f3f#defineN 100010#defineFILL (A, B) (Memset (A,b,sizeof (a)))#defineLson l,m,rt<<1#defineRson m+1,r,rt<<1|1using namespacestd; LL Sum1[n<<2],sum2[n<<2],sum3[n<<2]; LL Mul[n<<2],add[n<<2];voidPushup (intRT) {Sum1[rt]= (sum1[rt<<1]+sum1[rt<<1|1])%MoD; SUM2[RT]= (sum2[rt<<1]+sum2[rt<<1|1])%MoD; SUM3[RT]= (sum3[rt<<1]+sum3[rt<<1|1])%MoD;}voidPushdown (intRtintLen) {    if(mul[rt]!=1) {Mul[rt<<1]=mul[rt<<1]*mul[rt]%MoD; Mul[rt<<1|1]=mul[rt<<1|1]*mul[rt]%MoD; Add[rt<<1]=add[rt<<1]*mul[rt]%MoD; Add[rt<<1|1]=add[rt<<1|1]*mul[rt]%MoD; LL u=mul[rt]*mul[rt]%mod,v=mul[rt]*mul[rt]*mul[rt]%MoD; Sum1[rt<<1]=sum1[rt<<1]*mul[rt]%MoD; Sum1[rt<<1|1]=sum1[rt<<1|1]*mul[rt]%MoD; Sum2[rt<<1]=sum2[rt<<1]*u%MoD; Sum2[rt<<1|1]=sum2[rt<<1|1]*u%MoD; Sum3[rt<<1]=sum3[rt<<1]*v%MoD; Sum3[rt<<1|1]=sum3[rt<<1|1]*v%MoD; MUL[RT]=1; }    if(add[rt]!=0) {Add[rt<<1]= (add[rt<<1]+ADD[RT])%MoD; Add[rt<<1|1]= (add[rt<<1|1]+ADD[RT])%MoD; LL u=add[rt]*add[rt]%mod,v=add[rt]*add[rt]*add[rt]%MoD; LL T1=sum1[rt<<1],t2=sum1[rt<<1|1]; LL T3=sum2[rt<<1],t4=sum2[rt<<1|1]; Sum1[rt<<1]= (sum1[rt<<1]+ (len-(len>>1)) (*add[rt])%MoD; Sum1[rt<<1|1]= (sum1[rt<<1|1]+ (len>>1) *add[rt])%MoD; Sum2[rt<<1]= (sum2[rt<<1]+ (len-(len>>1)) *u+t1*add[rt]*2)%MoD; Sum2[rt<<1|1]= (sum2[rt<<1|1]+ (len>>1) *u+t2*add[rt]*2)%MoD; Sum3[rt<<1]= (sum3[rt<<1]+ (len-(len>>1)) *v+t3*add[rt]*3+3*U*T1)%MoD; Sum3[rt<<1|1]= (sum3[rt<<1|1]+ (len>>1) *v+t4*add[rt]*3+3*U*T2)%MoD; ADD[RT]=0; }}voidBuildintLintRintRT) {Sum1[rt]=sum2[rt]=sum3[rt]=0; MUL[RT]=1; add[rt]=0; if(L==R)return; intM= (l+r) >>1;    Build (Lson); Build (Rson);}voidUpdateintLintR,ll C,intLintRintRtintop) {    if(l<=l&&r<=R) {LL u=c*c%mod,v=c*c*c%MoD; LL Len=r-l+1; if(op==1) {LL T1=sum1[rt],t2=Sum2[rt]; ADD[RT]+=c;add[rt]%=MoD; SUM1[RT]= (sum1[rt]+len*c)%MoD; SUM2[RT]= (sum2[rt]+2*t1*c+len*u)%MoD; SUM3[RT]= (sum3[rt]+len*v+3*c*t2+3*U*T1)%MoD; }        Else if(op==2) {Mul[rt]*=c;mul[rt]%=MoD; ADD[RT]*=c;add[rt]%=MoD; SUM1[RT]=sum1[rt]*c%MoD; SUM2[RT]=sum2[rt]*u%MoD; SUM3[RT]=sum3[rt]*v%MoD; }        Else{Add[rt]=c;mul[rt]=0; SUM1[RT]=len*c%MoD; SUM2[RT]=len*u%MoD; SUM3[RT]=len*v%MoD; }        return; } pushdown (Rt,r-l+1); intM= (l+r) >>1; if(l<=m) update (L,R,C,LSON,OP); if(m<R) Update (L,R,C,RSON,OP); Pushup (RT);} LL Query (intLintRintLintRintRtintop) {    if(l<=l&&r<=R) {if(op==1)returnSum1[rt]; Else if(op==2)returnSum2[rt]; Else returnSum3[rt]; } pushdown (Rt,r-l+1); intM= (l+r) >>1; LL Res=0; if(l<=m) res+=query (L,R,LSON,OP); if(M<r) res+=query (L,R,RSON,OP); returnres%MoD;}intMain () {intn,m; intOp,x,y,c;  while(SCANF ("%d%d", &n,&m) >0)    {        if(m+n==0) Break; Build (1N1);  while(m--) {scanf ("%d%d%d%d",&op,&x,&y,&c); if(op==4) {printf ("%i64d\n", query (x, Y,1N1, c)); }            Else{update (X,Y,C,1N1, op); //printf ("%d\n", sum1[1]);            }        }    }}
View Code

hdu4578 (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.