[P2023][AHOI2009]Maintenance sequence (segment tree)

Topic description

The teacher gave Xiao Keke a task to maintain the sequence, and now Xiao Keke wants you to help him complete it. If there is a sequence of length N, it may be set to a1, a2,...,aN. There are three operation forms as follows: (1) Multiply all the numbers in the sequence by a value; (2) Add a value to all the numbers in the sequence; (3) Ask the sum of the numbers in the sequence, because the answer may be Very large, you just output the value of this number modulo P.

Input and output format

Input format:

The first line contains 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 representing the total number of operations. Starting from the fourth line, each line describes an operation, and the input operation has the following three forms: Operation 1: "1 tgc" (without double quotation marks). Indicates that all ai satisfying t≤i≤g are changed to ai×c (1≤t≤g≤N, 0≤c≤1000000000). Action 2: "2 tgc" (without double quotes). Indicates that all ai satisfying t≤i≤g are changed to ai+c (1≤t≤g≤N, 0≤c≤1000000000). Action 3: "3 tg" (without double quotes). Query all values ​​of the sum modulo P of ai satisfying t≤i≤g (1≤t≤g≤N). A space is used to separate two adjacent numbers on the same line, and there are no extra spaces at the beginning and end of each line.

Output format:

For each operation 3, in the order in which it appears in the input, one line of integers is output to represent the query result.

Input and output example

Input Example #1: Copy
7 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
Output Sample #1: Copy
2
35
8

illustrate

【Example description】

The initial hours are listed as (1,2,3,4,5,6,7).

After the first operation, the number column is (1,10,15,20,25,6,7).

For the second operation, the sum is 10+15+20=45, and the result modulo 43 is 2.

After the 3rd operation, the sequence is (1,10,24,29,34,15,16}

For the fourth operation, the sum is 1+10+24=35, and the result modulo 43 is 35.

For the fifth operation, the sum is 29+34+15+16=94, and the result modulo 43 is 8.

The scale of the test data is shown in the table below

Data number 1 2 3 4 5 6 7 8 9 10

N= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

M= 10 1000 1000 10000 60000 70000 80000 90000 100000 100000

Source: Ahoi 2009

It turns out that Xiao Keke was the protagonist of AHOI more than ten years ago.

Just mark the line segment tree, multiply first and then add.

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define ls (x<<1)
 4 #define rs (ls|1)
 5 #define lson ls,L,mid
 6 #define rson rs,mid+1,R
 7 #define rep(i,l,r) for (int i=l; i<=r; i++)
 8 using namespace std;
 9 
10 const int N=100010;
11 int n,m,mod,l,r,op,k,a[N],sm[N<<2],mt[N<<2],at[N<<2];
12 
13 void put(int x,int L,int R,int Mt,int At){
14     if (Mt!=1) sm[x]=1ll*sm[x]*Mt%mod,at[x]=1ll*at[x]*Mt%mod,mt[x]=1ll*mt[x]*Mt%mod;
15     if (At) sm[x]=(sm[x]+1ll*(R-L+1)*At)%mod,at[x]=(at[x]+At)%mod;
16 }
17 
18 void push(int x,int L,int R){
19     int mid=(L+R)>>1;
20     put(lson,mt[x],at[x]); put(rson,mt[x],at[x]);
21     mt[x]=1; at[x]=0;
22 }
23 
24 void build(int x,int L,int R){
25     if (L==R){ sm[x]=a[L]; mt[x]=1; return; }
26     int mid=(L+R)>>1; mt[x]=1;
27     build(lson); build(rson);
28     sm[x]=(sm[ls]+sm[rs])%mod;
29 }
30 
31 void add(int x,int L,int R,int l,int r,int k){
32     if (L==l && r==R){ put(x,L,R,1,k); return; }
33     int mid=(L+R)>>1; push(x,L,R);
34     if (r<=mid) add(lson,l,r,k);
35     else if (l>mid) add(rson,l,r,k);
36         else add(lson,l,mid,k),add(rson,mid+1,r,k);
37     sm[x]=(sm[ls]+sm[rs])%mod;
38 }
39 
40 void mul(int x,int L,int R,int l,int r,int k){
41     if (L==l && r==R){ put(x,L,R,k,0); return; }
42     int mid=(L+R)>>1; push(x,L,R);
43     if (r<=mid) mul(lson,l,r,k);
44     else if (l>mid) mul(rson,l,r,k);
45         else mul(lson,l,mid,k),mul(rson,mid+1,r,k);
46     sm[x]=(sm[ls]+sm[rs])%mod;
47 }
48 
49 int que(int x,int L,int R,int l,int r){
50     if (L==l && r==R) return sm[x];
51     int mid=(L+R)>>1; push(x,L,R);
52     if (r<=mid) return que(lson,l,r);
53     else if (l>mid) return que(rson,l,r);
54         else return (que(lson,l,mid)+que(rson,mid+1,r))%mod;
55 }
56 
57 int main(){
58     freopen("P2023.in","r",stdin);
59     freopen("P2023.out","w",stdout);
60     scanf("%d%d",&n,&mod);
61     rep(i,1,n) scanf("%d",&a[i]);
62     build(1,1,n); scanf("%d",&m);
63     rep(i,1,m){
64         scanf("%d",&op);
65         if (op==1) scanf("%d%d%d",&l,&r,&k),mul(1,1,n,l,r,k);
66         if (op==2) scanf("%d%d%d",&l,&r,&k),add(1,1,n,l,r,k);
67         if (op==3) scanf("%d%d",&l,&r),printf("%d\n",que(1,1,n,l,r));
68     }
69     return 0;
70 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325118595&siteId=291194637