P2023 [AHOI2009]维护序列 --线段树

来源:互联网 发布:淘宝流量在哪里看手机 编辑:程序博客网 时间:2024/06/01 10:06
                                 P2023 [AHOI2009]维护序列 --线段树

 

题目描述

老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为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 431 2 3 4 5 6 751 2 5 53 2 42 3 7 93 1 33 4 7
输出样例#1:
2358

说明

【样例说明】

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

 

一道线段树,没事么好说的

 

  1 /*  2     区间加  3     区间乘  4     区间查询  5     标记下方时 要先乘后加   6 */  7 #include<cstdio>  8 #include<ctype.h>  9  10 typedef long long LL;  11  12 const int MAXN=100010; 13  14 LL n,m,p,opt,tt,g,c; 15  16 struct node { 17     int l,r; 18     LL bjp,bjm; 19     LL sum; 20 }; 21 node t[MAXN<<2]; 22  23 inline void read(LL&x) { 24     int f=1;register char c=getchar(); 25     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar()); 26     for(;isdigit(c);x=x*10+c-48,c=getchar()); 27     x=x*f; 28 } 29  30 inline void down(int now) { 31     t[now<<1].bjm=(t[now].bjm*t[now<<1].bjm)%p; 32     t[now<<1|1].bjm=(t[now].bjm*t[now<<1|1].bjm)%p; 33     t[now<<1].bjp=(t[now].bjm*t[now<<1].bjp%p+t[now].bjp)%p; 34     t[now<<1|1].bjp=(t[now].bjm*t[now<<1|1].bjp%p+t[now].bjp)%p; 35     t[now<<1].sum=(t[now].bjm*t[now<<1].sum%p+t[now].bjp*(t[now<<1].r-t[now<<1].l+1)%p); 36     t[now<<1|1].sum=(t[now].bjm*t[now<<1|1].sum%p+t[now].bjp*(t[now<<1|1].r-t[now<<1|1].l+1)%p)%p; 37     t[now].bjm=1;t[now].bjp=0; 38 } 39  40 void build_tree(int now,int l,int r) { 41     t[now].l=l,t[now].r=r; 42     t[now].bjm=1; 43     if(l==r) { 44         read(t[now].sum); 45         return; 46     } 47     int mid=(l+r)>>1; 48     build_tree(now<<1,l,mid); 49     build_tree(now<<1|1,mid+1,r); 50     t[now].sum=(t[now<<1].sum+t[now<<1|1].sum)%p; 51 } 52  53 void modify_plus(int now,int l,int r,int v) { 54     if(l<=t[now].l&&r>=t[now].r) { 55         t[now].bjp+=v; 56         t[now].sum+=(LL)(t[now].r-t[now].l+1)*v; 57         return; 58     } 59     down(now); 60     int mid=(t[now].l+t[now].r)>>1; 61     if(l<=mid) modify_plus(now<<1,l,r,v); 62     if(r>mid) modify_plus(now<<1|1,l,r,v); 63     t[now].sum=(t[now<<1].sum+t[now<<1|1].sum)%p; 64 } 65  66 void modify_mul(int now,int l,int r,int v) { 67     if(l<=t[now].l&&r>=t[now].r) { 68         t[now].bjm=(t[now].bjm*v)%p; 69         t[now].bjp=(t[now].bjp*v)%p; 70         t[now].sum=(t[now].sum*v)%p; 71         return; 72     } 73     down(now); 74     int mid=(t[now].l+t[now].r)>>1; 75     if(l<=mid) modify_mul(now<<1,l,r,v); 76     if(r>mid) modify_mul(now<<1|1,l,r,v); 77     t[now].sum=(t[now<<1].sum+t[now<<1|1].sum)%p; 78 } 79  80 LL query(int now,int l,int r) { 81     if(l<=t[now].l&&r>=t[now].r) return t[now].sum%p; 82     LL tot=0; 83     down(now); 84     int mid=(t[now].l+t[now].r)>>1; 85     if(l<=mid) tot=(tot+query(now<<1,l,r))%p; 86     if(r>mid) tot=(tot+query(now<<1|1,l,r))%p; 87     return tot; 88 } 89  90 int hh() { 91     read(n);read(p); 92     build_tree(1,1,n); 93     read(m); 94     for(int i=1;i<=m;++i) { 95         read(opt);read(tt);read(g); 96         if(opt==1) { 97             read(c); 98             modify_mul(1,tt,g,c); 99         }100         else if(opt==2) {101             read(c);102             modify_plus(1,tt,g,c);103         }104         else {105             LL ans=query(1,tt,g);106             printf("%lld\n",ans);107         }108     }109     return 0;110 }111 112 int sb=hh();113 int main() {;}
代码