# bzoj 1798: [Ahoi2009]Seq 维护序列seq (线段树 ，多重标记下放）

## 1798: [Ahoi2009]Seq 维护序列seq

Time Limit: 30 Sec  Memory Limit: 64 MB
Submit: 7773  Solved: 2792
[Submit][Status][Discuss]

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

2
35
8

## HINT

【样例说明】

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

## Source

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int M = 2e5+10;

void pushup(int rt){
sum[rt] = (sum[rt<<1] + sum[rt<<1|1])%md;
}

void pushdown(int m,int rt){
mul[rt<<1] = mul[rt<<1]*mul[rt]%md;
mul[rt<<1|1] = mul[rt<<1|1]*mul[rt]%md;
add[rt] = 0; mul[rt] = 1;
}

void build(int l,int r,int rt){
if(l==r){
scanf("%lld",&sum[rt]);
return;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}

void update(int L,int R,int c,int v,int l,int r,int rt){
if(L <= l&&R >= r){
if(v==1){
sum[rt] = sum[rt]*c%md;
mul[rt] = mul[rt]*c%md;
}
else{
sum[rt] = (sum[rt] + (ll)c*(r-l+1))%md;
}
return;
}
pushdown(r-l+1,rt);
int m = (l + r) >> 1;
if(L <= m) update(L,R,c,v,lson);
if(R > m) update(L,R,c,v,rson);
pushup(rt);
}

ll query(int L,int R,int l,int r,int rt){
if(L <= l&&R >= r){
return sum[rt]%md;
}
pushdown(r-l+1,rt);
ll ret = 0;
int m = (l + r) >> 1;
if(L <= m) ret = (ret+query(L,R,lson))%md;
if(R > m) ret = (ret+query(L,R,rson))%md;
return ret;
}

int main()
{
int n,q,a,c,b,d;
scanf("%d %lld",&n,&md);
build(1,n,1);
scanf("%d",&q);
while(q--){
scanf("%d",&a);
if(a==1){
scanf("%d %d %d",&b,&c,&d);
update(b,c,d,1,1,n,1);
}
else if(a==2){
scanf("%d %d %d",&b,&c,&d);
update(b,c,d,2,1,n,1);
}
else{
scanf("%d%d",&b,&c);
printf("%lld\n",query(b,c,1,n,1));
}
}
}

posted @ 2018-03-15 22:18  冥想选手  阅读(152)  评论(0编辑  收藏  举报