BZOJ 5039([Jsoi2014]序列维护-线段树区间加,区间乘,区间求和)

来源:互联网 发布:断电后sql启动不了 编辑:程序博客网 时间:2024/06/06 12:45

Description

JYY 有一个维护数列的任务。 他希望你能够来帮助他完成。
JYY 现在有一个长度为 N 的序列 a1,a2,…,aN,有如下三种操作:
1、 把数列中的一段数全部乘以一个值;
2、 把数列中的一段数全部加上一个值;
3、 询问序列中的一段数的和。
由于答案可能很大,对于每个询问,你只需要告诉 JYY 这个询问的答案对 P
取模的结果即可。
Input

第一行包含两个正整数, N 和 P;
第二行包含 N 个非负整数,从左到右依次为 a1,a2,…,aN。
第三行有一个整数 M,表示操作总数。
接下来 M 行,每行满足如下三种形式之一:
1、“ 1 t g c”(不含引号)。表示把所有满足 t ≤ i ≤ g 的 ai 全部乘以 c;
2、“ 2 t g c”(不含引号)。表示把所有满足 t ≤ i ≤ g 的 ai 全部加上 c;
3、“ 3 t g”(不含引号)。表示询问满足 t ≤ i ≤ g 的 ai 的和对 P 取模的值。
1 ≤ N,M ≤ 10^5, 1 ≤ P, c, ai ≤ 2*10^9, 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

【样例说明】

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

Source

Round1

#include<cstdio>#include<cctype>#include<iostream>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define ForkD(i,k,n) for(int i=n;i>=k;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=Pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])  #define Lson (o<<1)#define Rson ((o<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,0x3f,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define MEMx(a,b) memset(a,b,sizeof(a));#define INF (0x3f3f3f3f)#define pb push_back#define mp make_pair #define fi first#define se second#define vi vector<int> #define pi pair<int,int>#define SI(a) ((a).size())#define Pr(kcase,ans) printf("Case #%d: %lld\n",kcase,ans);#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;#define PRi2D(a,n,m) For(i,n) { \                        For(j,m-1) cout<<a[i][j]<<' ';\                        cout<<a[i][m]<<endl; \                        } #pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double ld;typedef unsigned long long ull;ll F;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return ((a-b)%F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}int read(){    int x=0,f=1; char ch=getchar();    while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}    while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}    return x*f;} #define MAXN (400044+10)ll addv[MAXN<<2],mulv[MAXN<<2],sum[MAXN<<2];void pushUp(int o) {    sum[o]=(sum[Lson] + sum[Rson])%F;}void pushDown(int o,ll m) {    if (mulv[o]!=1) {        sum[Lson]=sum[Lson]*mulv[o]%F;        sum[Rson]=sum[Rson]*mulv[o]%F;        mulv[Lson]=mulv[Lson]*mulv[o]%F;        mulv[Rson]=mulv[Rson]*mulv[o]%F;        addv[Lson]=addv[Lson]*mulv[o]%F;        addv[Rson]=addv[Rson]*mulv[o]%F;        mulv[o]=1;    }    if (addv[o]) {        upd(addv[Lson],addv[o]);        upd(addv[Rson],addv[o]);        upd(sum[Lson],(ll)(m-(m>>1))*addv[o]);        upd(sum[Rson],(ll)(m>>1)* addv[o]);        addv[o]=0;    }} void build(int l,int r,int o) {    addv[o]=0;mulv[o]=1;    if (l==r) {        sum[o]=read();        return;    }    int m=(l+r)>>1;    build(l,m,Lson);    build(m+1,r,Rson);    pushUp(o);}void update(int l,int r,int o,int L,int R,ll c) {    if (L<=l&&r<=R) {        addv[o]+=c;        sum[o]+=c*(r-l+1);        addv[o]%=F,sum[o]%=F;        return;    }    pushDown(o,r-l+1);    int m=(l+r)>>1;    if (L<=m) update(l,m,Lson,L,R,c);    if (m<R) update(m+1,r,Rson,L,R,c);    pushUp(o);}void updmul(int l,int r,int o,int L,int R,ll c) {    if (L<=l&&r<=R) {        addv[o]=addv[o]*c%F;        sum[o]=sum[o]*c%F;        mulv[o]=mulv[o]*c%F;        return;    }    pushDown(o,r-l+1);    int m=(l+r)>>1;    if (L<=m) updmul(l,m,Lson,L,R,c);    if (m<R) updmul(m+1,r,Rson,L,R,c);    pushUp(o);}ll query(int l,int r,int o,int L,int R) {    if (L<=l && r<=R) {        return sum[o]%F;    }    pushDown(o,r-l+1);    int m=(l+r)>>1;    ll ret=0;    if (L<=m) ret+=query(l,m,Lson,L,R);    if (m<R) ret+=query(m+1,r,Rson,L,R);    return ret%F;   }int main(){//  freopen("bzoj5039.in","r",stdin);//  freopen(".out","w",stdout);    int n=read();    F=read();    build(1,n,1);    int m=read();    while(m--) {        char op[2]; int a,b;        scanf("%s%d%d",op,&a,&b);        if (op[0]=='3') printf("%lld\n",query(1,n,1,a,b));        else if(op[0]=='2') update(1,n,1,a,b,read());        else updmul(1,n,1,a,b,read());    }       return 0;}
阅读全文
0 0
原创粉丝点击