1. 程式人生 > >[P2023][AHOI2009]維護序列(線段樹)

[P2023][AHOI2009]維護序列(線段樹)

線段 有一個 strong 全部 AI AR 由於 AS for

題目描述

老師交給小可可一個維護數列的任務,現在小可可希望你來幫他完成。 有長為N的數列,不妨設為a1,a2,…,aN 。有如下三種操作形式: (1)把數列中的一段數全部乘一個值; (2)把數列中的一段數全部加一個值; (3)詢問數列中的一段數的和,由於答案可能很大,你只需輸出這個數模P的值。

輸入輸出格式

輸入格式:

第一行兩個整數N和P(1≤P≤1000000000)。 第二行含有N個非負整數,從左到右依次為a1,a2,…,aN, (0≤ai≤1000000000,1≤i≤N)。 第三行有一個整數M,表示操作總數。 從第四行開始每行描述一個操作,輸入的操作有以下三種形式: 操作1:“1 t g c”(不含雙引號)。表示把所有滿足t≤i≤g的ai改為ai×c (1≤t≤g≤N,0≤c≤1000000000)。 操作2:“2 t g c”(不含雙引號)。表示把所有滿足t≤i≤g的ai改為ai+c (1≤t≤g≤N,0≤c≤1000000000)。 操作3:“3 t g”(不含雙引號)。詢問所有滿足t≤i≤g的ai的和模P的值 (1≤t≤g≤N)。 同一行相鄰兩數之間用一個空格隔開,每行開頭和末尾沒有多余空格。

輸出格式:

對每個操作3,按照它在輸入中出現的順序,依次輸出一行一個整數表示詢問結果。

輸入輸出樣例

輸入樣例#1: 復制
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
輸出樣例#1: 復制
2
35
8

說明

【樣例說明】

初始時數列為(1,2,3,4,5,6,7)。

經過第1次操作後,數列為(1,10,15,20,25,6,7)。

對第2次操作,和為10+15+20=45,模43的結果是2。

經過第3次操作後,數列為(1,10,24,29,34,15,16}

對第4次操作,和為1+10+24=35,模43的結果是35。

對第5次操作,和為29+34+15+16=94,模43的結果是8。

測試數據規模如下表所示

數據編號 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

原來小可可十幾年前就是AHOI的主人公啊。

線段樹打標記就好了,先乘後加。

 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 }

[P2023][AHOI2009]維護序列(線段樹)