1798: [AHOI2009]SEQ maintenance sequence Seq time limit:30 Sec Memory limit:64 MB
submit:3349 solved:1241
[Submit] [Status] [Discuss] 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
Source
Program hehe;
Type
Shu=record
H,a,m:int64;
L,r:longint;
End
Var
N,q,t,m,i,j,k,f:longint;
X:ARRAY[0..1000000] of Shu;
Procedure build (A,l,r:longint);
Var
Mid:longint;
Begin
X[a].l:=l;
X[a].r:=r;
X[a].m:=1;
If L=r then read (x[a].h)
Else
Begin
mid:= (l+r) >>1;
Build (A<<1,l,mid);
Build (A<<1+1,mid+1,r);
x[a].h:= (x[a<<1].h+x[a<<1+1].h) mod m;
End
End
Procedure down (a:longint);
Begin
If X[a].m<>1 Then
Begin
X[A<<1].M:=X[A<<1].M*X[A].M mod m;
X[A<<1+1].M:=X[A<<1+1].M*X[A].M mod m;
X[A<<1].A:=X[A<<1].A*X[A].M mod m;
X[A<<1+1].A:=X[A<<1+1].A*X[A].M mod m;
x[a<<1].h:= (x[a<<1].h mod m) *x[a].m mod m;
x[a<<1+1].h:= (x[a<<1+1].h mod m) *x[a].m mod m;
X[a].m:=1;
End
If X[a].a<>0 Then
Begin
x[a<<1].a:=x[a<<1].a+x[a].a;
x[a<<1+1].a:=x[a<<1+1].a+x[a].a;
X[a<<1].h:= (x[a<<1].h+ (x[a<<1].r-x[a<<1].l+1) *x[a].a) mod m;
X[a<<1+1].h:= (x[a<<1+1].h+ (x[a<<1+1].r-x[a<<1+1].l+1) *x[a].a) mod m;
x[a].a:=0;
End
End
Procedure Cheng (a,l,r,b:longint);
Var
Mid:longint;
Begin
Down (a);
if (x[a].l=l) and (x[a].r=r) then
Begin
X[a].m:=b;
x[a].h:= (x[a].h mod m) *b mod m;
End
Else
Begin
mid:= (X[A].L+X[A].R) >>1;
If L>mid then Cheng (a<<1+1,l,r,b)
else if R<=mid then Cheng (a<<1,l,r,b)
Else
Begin
Cheng (a<<1,l,mid,b);
Cheng (a<<1+1,mid+1,r,b);
End
x[a].h:= (x[a<<1].h+x[a<<1+1].h) mod m;
End
End
Procedure Jia (A,l,r,b:longint);
Var
Mid:longint;
Begin
Down (a);
if (x[a].l=l) and (x[a].r=r) then
Begin
X[a].a:=b;
X[a].h:= (x[a].h+ (x[a].r-x[a].l+1) *b) mod m;
End
Else
Begin
mid:= (X[A].L+X[A].R) >>1;
If L>mid then Jia (a<<1+1,l,r,b)
else if R<=mid then Jia (a<<1,l,r,b)
Else
Begin
Jia (a<<1,l,mid,b);
Jia (a<<1+1,mid+1,r,b);
End
x[a].h:= (x[a<<1].h+x[a<<1+1].h) mod m;
End
End
function Find (A,l,r:longint): Int64;
Var
Mid:longint;
Begin
Down (a);
if (x[a].l=l) and (X[a].r=r) then exit (x[a].h mod m);
mid:= (X[A].L+X[A].R) >>1;
If L>mid then exit (Find (A<<1+1,L,R) mod m)
else if R<=mid then exit (Find (A<<1,L,R) mod m)
else Exit ((Find (A<<1,l,mid) +find (a<<1+1,mid+1,r)) mod m);
End
Begin
READLN (N,M);
Build (1,1,n);
READLN (q);
For I:=1 to Q do
Begin
Read (j);
If J=1 Then
Begin
READLN (k,f,t);
Cheng (1,k,f,t);
End
else if j=2 then
Begin
READLN (k,f,t);
Jia (1,k,f,t);
End
Else
Begin
READLN (K,F);
Writeln (Find (1,k,f));
End
End
End.
Line segment Tree: The tune is dead.