[AHOI 2009] 维护序列(线段树模板题)

1798: [Ahoi2009]Seq 维护序列seq

Time Limit: 30 Sec  Memory Limit: 64 MB

Description

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。

Input

第一行两个整数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)。 同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。

Output

对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。

Sample Input

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

Sample Output

2
35
8

HINT

【样例说明】

初始时数列为(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

  • 裸的线段树模板题,会乘法tag和加法tag即可。
  • 复杂度O(mlogn),虽然常数较大,不过也能通过本题。
  1 #include <cstdio>
  2 #include <algorithm>
  3 using namespace std;
  4 
  5 struct tree{
  6     int l,r;
  7     long long lz,tg,sum;
  8 };
  9 
 10 int n,p,m,opt,x,y,k,a[100050];
 11 tree t[300050];
 12 
 13 void build(int x,int l,int r) {
 14     t[x].l=l; t[x].r=r; t[x].tg=1;
 15     if (t[x].l==t[x].r) {
 16         t[x].sum=a[l]%p;
 17         return;
 18     }
 19     int mid=(t[x].l+t[x].r)>>1;
 20     build(x*2,l,mid);
 21     build(x*2+1,mid+1,r);
 22     t[x].sum=(t[x*2].sum+t[x*2+1].sum)%p;
 23 }
 24 
 25 void update(int x) {
 26     t[x].sum=(t[x].sum*t[x].tg+t[x].lz*(t[x].r-t[x].l+1))%p;
 27     if (t[x].l==t[x].r) {
 28         t[x].tg=1;
 29         t[x].lz=0;
 30         return;
 31     }
 32     t[x*2].tg=(t[x*2].tg*t[x].tg)%p;
 33     t[x*2+1].tg=(t[x*2+1].tg*t[x].tg)%p;
 34     t[x*2].lz=(t[x*2].lz*t[x].tg+t[x].lz)%p;
 35     t[x*2+1].lz=(t[x*2+1].lz*t[x].tg+t[x].lz)%p;
 36     t[x].tg=1;
 37     t[x].lz=0;
 38 }
 39 
 40 void c_tg(int x,int l,int r,int y) {
 41     if ((t[x].lz) || t[x].tg!=1) update(x);
 42     if (t[x].l==l && t[x].r==r) {
 43         t[x].tg=(t[x].tg*y)%p;
 44         return;
 45     }
 46     int mid=(t[x].l+t[x].r)>>1;
 47     if (l>mid) c_tg(x*2+1,l,r,y); else
 48     if (r<=mid) c_tg(x*2,l,r,y); else {
 49         c_tg(x*2,l,mid,y);
 50         c_tg(x*2+1,mid+1,r,y);
 51     }
 52     t[x].sum=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1))%p;
 53 }
 54 
 55 void c_lz(int x,int l,int r,int y) {
 56     if ((t[x].lz) || t[x].tg!=1) update(x);
 57     if (t[x].l==l && t[x].r==r) {
 58         t[x].lz=(t[x].lz+y)%p;
 59         return;
 60     }
 61     int mid=(t[x].l+t[x].r)>>1;
 62     if (l>mid) c_lz(x*2+1,l,r,y); else
 63     if (r<=mid) c_lz(x*2,l,r,y); else {
 64         c_lz(x*2,l,mid,y);
 65         c_lz(x*2+1,mid+1,r,y);
 66     }
 67     t[x].sum=(t[x*2].sum*t[x*2].tg+t[x*2].lz*(t[x*2].r-t[x*2].l+1)+t[x*2+1].sum*t[x*2+1].tg+t[x*2+1].lz*(t[x*2+1].r-t[x*2+1].l+1))%p;
 68 }
 69 
 70 long long find(int x,int l,int r) {
 71     if ((t[x].lz) || t[x].tg!=1) update(x);
 72     if (t[x].l==l && t[x].r==r) return t[x].sum;
 73     int mid=(t[x].l+t[x].r)>>1;
 74     if (l>mid) return(find(x*2+1,l,r)); else
 75     if (r<=mid) return(find(x*2,l,r)); else
 76         return(find(x*2,l,mid)+find(x*2+1,mid+1,r))%p;
 77 }
 78 
 79 int main() {
 80     scanf("%d %d",&n,&p);
 81     for (int i=1; i<=n; i++) scanf("%d",&a[i]);
 82     build(1,1,n);
 83     scanf("%d",&m);
 84     for (int i=1; i<=m; i++) {
 85         scanf("%d",&opt);
 86         if (opt==1) {
 87             scanf("%d %d %d",&x,&y,&k);
 88             c_tg(1,x,y,k%p);
 89         }
 90         if (opt==2) {
 91             scanf("%d %d %d",&x,&y,&k);
 92             c_lz(1,x,y,k%p);
 93         }
 94         if (opt==3) {
 95             scanf("%d %d",&x,&y);
 96             printf("%d
",find(1,x,y));
 97         }
 98     }
 99     return 0;
100 }
原文地址:https://www.cnblogs.com/zoewilly/p/6063150.html