Description teacher to small cocoa a maintenance sequence of tasks, now small cocoa hope you to help him finish. There is a series of n, it may be set as A1,a2,..., an. There are three types of operation: (1) Multiply a number of numbers in a series by one value; (2) Add a value to all the numbers in the series; (3) Ask the number of numbers in the series and, because the answer may be large, you only need to output the value of the modulo p. Input first line two integers n and P (1≤p≤1000000000). The second line contains N non-negative integers, from left to right A1,a2,..., an, (0≤ai≤1000000000,1≤i≤n). The third line has an integer m, which represents the total number of operations. Starting with line fourth, each line describes an operation that has the following three forms: Action 1: "1 T G C" (without double quotes). Indicates that all AI satisfying t≤i≤g is changed to AIXC (1≤t≤g≤n,0≤c≤1000000000). Action 2: "2 T G C" (without double quotes). Indicates that all AI satisfying t≤i≤g is changed to Ai+c (1≤t≤g≤n,0≤c≤1000000000). Action 3: "3 T G" (without double quotes). Ask all the AI's and modulo p values (1≤t≤g≤n) that satisfy the t≤i≤g. The same line is separated by a space and there is no extra space at the beginning and end of each line. Output for each operation 3, follow the order in which it appears in the input, outputting an integer one line to indicate the query result. Sample Input7 43
1 2 3 4 5 6 7
5
1 2 5 5
3 2 4
2 3 7 9
3 1 3
3 4 7
Sample Output2
35
8
HINT
"Sample description"
The initial number of hours is listed (1,2,3,4,5,6,7).
After the 1th operation, the number is listed as (1,10,15,20,25,6,7).
For the 2nd operation, and for 10+15+20=45, the result of modulo 43 is 2.
After the 3rd operation, the number of columns (1,10,24,29,34,15,16}
For the 4th operation, and for 1+10+24=35, the result of modulo 43 is 35.
For the 5th operation, and for 29+34+15+16=94, the result of modulo 43 is 8.
The test data size is shown in the following table
Data Number 12345678910
n=10100010001000060000700008000090000100000100000
m=10100010001000060000700008000090000100000100000
Water line tree, tuned for an afternoon ... hahaha, good fun!!! For the case of multiplication with addition, we do two tage, each multiplication down the first add the mark to multiply. Remember to pushdown at any time, otherwise it may be wrong, concrete has not thought out ...
//after processing and marking, by the way when the mark processing, to open ll HHH#include <cstdio>#definell Long LongConst intn=100010;structtree{intL,r,lch,rch; ll Tage,sum,mult;} Tr[n*2];intn,p,opt,m,cnt;intA[n];voidBuildintKintLintR) {// intMid= (l+r) >>1; tr[++cnt].l=l,tr[k].r=r;tr[cnt].mult=1; if(L==R) {Tr[k].sum=a[l];return;} Tr[k].lch=cnt+1; Build (cnt+1, L,mid); Tr[k].rch=cnt+1; Build (cnt+1, mid+1, R); intLc=tr[k].lch,rc=Tr[k].rch; Tr[k].sum= (tr[lc].sum+tr[rc].sum)%p; } voidPushdown (intk) { intMid= (TR[K].L+TR[K].R) >>1, lc=tr[k].lch,rc=Tr[k].rch; Tr[lc].sum= (tr[lc].sum* (tr[k].mult))%p; Tr[rc].sum= (tr[rc].sum* (tr[k].mult))%p; Tr[lc].sum= (tr[lc].sum+ (tr[k].tage) * (tr[lc].r-tr[lc].l+1))%p; Tr[rc].sum= (tr[rc].sum+ (tr[k].tage) * (tr[rc].r-tr[rc].l+1))%p; Tr[lc].tage= (tr[lc].tage*tr[k].mult)%p; Tr[rc].tage= (tr[rc].tage*tr[k].mult)%p; Tr[lc].tage= (tr[lc].tage+tr[k].tage)%p; Tr[rc].tage= (tr[rc].tage+tr[k].tage)%p; Tr[lc].mult= (tr[lc].mult*tr[k].mult)%p; Tr[rc].mult= (tr[rc].mult*tr[k].mult)%Q; Tr[k].tage=0; tr[k].mult=1; Tr[k].sum= (tr[lc].sum+tr[rc].sum)%p;} voidMultintKintLintRintW) { intMid= (TR[K].L+TR[K].R) >>1, lc=tr[k].lch,rc=Tr[k].rch; if(tr[k].l==l&&tr[k].r==r) {tr[k].sum= (tr[k].sum*w)%p; Tr[k].tage= (tr[k].tage*w)%p; Tr[k].mult= (tr[k].mult*w)%p; return; } pushdown (k); if(r<=mid) mult (TR[K].LCH,L,R,W); Else if(l>mid) mult (TR[K].RCH,L,R,W); ElseMult (Tr[k].lch,l,mid,w), mult (tr[k].rch,mid+1, r,w); Tr[k].sum= (tr[lc].sum+tr[rc].sum)%p;} voidPlusintKintLintRintW) { intMid= (TR[K].L+TR[K].R) >>1, lc=tr[k].lch,rc=Tr[k].rch; if(tr[k].l==l&&tr[k].r==r) {tr[k].sum= (tr[k].sum+w* (r-l+1))%p; Tr[k].tage= (tr[k].tage+w)%p; return; } pushdown (k); if(r<=mid) plus (TR[K].LCH,L,R,W); Else if(l>mid) plus (TR[K].RCH,L,R,W); ElsePlus (TR[K].LCH,L,MID,W), plus (tr[k].rch,mid+1, r,w); Tr[k].sum= (tr[lc].sum+tr[rc].sum)%p;} intQueryintKintLintR) { intMid= (TR[K].L+TR[K].R) >>1, lc=tr[k].lch,rc=Tr[k].rch; if(TR[K].L==L&&TR[K].R==R)returntr[k].sum; Pushdown (k); if(R<=mid)returnquery (LC,L,R); Else if(L>mid)returnquery (RC,L,R); Else return(Query (Lc,l,mid) +query (rc,mid+1, R))%p;} intMain () {scanf ("%d%d",&n,&p); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); Build (1,1, N); scanf ("%d",&m); intl,r,w; for(intI=1; i<=m;i++) {scanf ("%d",&opt); Switch(opt) { Case 1: scanf ("%d%d%d", &l,&r,&w); Mult (1, l,r,w); Break; Case 2: scanf ("%d%d%d", &l,&r,&w);p LUs (1, l,r,w); Break; Case 3: scanf ("%d%d", &l,&r);p rintf ("%d\n", Query (1, l,r)); Break; } }}
"Bzoj 1798" [Ahoi2009]seq Maintenance sequence Seq