# BZOJ1798 【AHOI2009】 seq维护序列 <线段树>

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

【样例说明】

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

#include <iostream>
#include <cstdio>
#include <cstring>
#define MAX_N 100000
#define ll long long
using namespace std;
int n, m;
ll p;
ll tree[MAX_N*4+5], mul[MAX_N*4+5], add[MAX_N*4+5];
void updata(int v) {
tree[v] = (tree[v<<1]+tree[v<<1|1])%p;
}
void downtag(int v, int s, int t, int mid) {
if (mul[v] == 1 && add[v] == 0)    return;
mul[v*2] = mul[v*2]*mul[v]%p;
mul[v*2+1] = mul[v*2+1]*mul[v]%p;
mul[v] = 1;
return;
}
void create(int v, int s, int t) {
mul[v] = 1;
if (s == t) {
scanf("%lld", &tree[v]);
tree[v] %= p;
return;
}
int mid = s+t>>1;
create(v<<1, s, mid);
create(v<<1|1, mid+1, t);
updata(v);
}
void modify1(int v, int s, int t, int l, int r, int x) {
if (s >= l && t <= r) {
tree[v] = (tree[v]+(ll)x*(ll)(t-s+1)%p)%p;
return;
}
int mid = s+t>>1;
downtag(v, s, t, mid);
if (l <= mid) {
modify1(v<<1, s, mid, l, r, x);
}
if (r >= mid+1) {
modify1(v<<1|1, mid+1, t, l, r, x);
}
updata(v);
}
void modify2(int v, int s, int t, int l, int r, int x) {
if (s >= l && t <= r) {
mul[v] = mul[v]*(ll)x%p;
tree[v] = tree[v]*(ll)x%p;
return;
}
int mid = s+t>>1;
downtag(v, s, t, mid);
if (l <= mid) {
modify2(v<<1, s, mid, l, r, x);
}
if (r >= mid+1) {
modify2(v<<1|1, mid+1, t, l, r, x);
}
updata(v);
}
ll query(int v, int s, int t, int l, int r) {
if (s >= l && t <= r) {
return tree[v];
}
int mid = s+t>>1;
ll ret = 0;
downtag(v, s, t, mid);
if (l <= mid) {
ret = (ret+query(v<<1, s, mid, l, r))%p;
}
if (r >= mid+1) {
ret = (ret+query(v<<1|1, mid+1, t, l, r))%p;
}
updata(v);
return ret;
}
int main() {
scanf("%d%d", &n, &p);
create(1, 1, n);
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int f;
scanf("%d", &f);
if (f == 1) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
modify2(1, 1, n, l, r, x%p);
} else if (f == 2) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
modify1(1, 1, n, l, r, x%p);
} else {
int l, r;
scanf("%d%d", &l, &r);
printf("%lld\n", query(1, 1, n, l, r));
}
}
return 0;
}


BZOJ上有个BUG，用cincout输出流会RE，如果无端RE请注意输入输出!

posted @ 2017-09-20 15:25  Azrael_Death  阅读(119)  评论(0编辑  收藏  举报