# bzoj1798: [Ahoi2009]Seq 维护序列seq（线段树）

bzoj1798

1、将一段区间乘上c
2、将一段区间加上c
3、求一段区间的和

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

可以将每一个数变为$$a \times b + c$$ 的形式，在每一次乘的修改时，直接将和乘c，并将加和乘的标记都乘c即可。而加的修改则只需要将加的标记加上c即可。
在标记下传时，将该点子树的乘标记乘上该点的乘标记；将子树的加标记先乘上该点的乘标记，再加上该点的加标记即可。

#include<cstdio>
#define lc o << 1
#define rc o << 1 | 1
using namespace std;

const int maxn = 1e5 + 5;
int n, m, p, a[maxn], sum[maxn << 2], bj1[maxn << 2], bj2[maxn << 2]; //bj1:乘标记，bj2:加标记

char c; while (c = getchar(), c < '0' || c >'9'); int x = c - '0';
while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; return x;
}

void build(int o, int l, int r) { //建树
bj1[o] = 1; //乘标记的初始值为1
if (l == r) {
sum[o] = a[l] % p;
return;
}
int mid = l + r >> 1;
build(lc, l, mid); build(rc, mid + 1, r);
sum[o] = (sum[lc] + sum[rc]) % p;
}

void pushdown(int o, int l, int r) { //标记下传
int mid = l + r >> 1;
sum[lc] = 1ll * sum[lc] * bj1[o] % p; sum[lc] += 1ll * bj2[o] * (mid - l + 1) % p; sum[lc] %= p;
sum[rc] = 1ll * sum[rc] * bj1[o] % p; sum[rc] += 1ll * bj2[o] * (r - mid) % p; sum[rc] %= p;
bj1[lc] = 1ll * bj1[lc] * bj1[o] % p;
bj1[rc] = 1ll * bj1[rc] * bj1[o] % p;
bj2[lc] = 1ll * bj2[lc] * bj1[o] % p; bj2[lc] = (bj2[lc] + bj2[o]) % p;
bj2[rc] = 1ll * bj2[rc] * bj1[o] % p; bj2[rc] = (bj2[rc] + bj2[o]) % p;
bj1[o] = 1; bj2[o] = 0;
}

void modify1(int o, int l, int r, int ql, int qr,int c) {
if (ql <= l && qr >= r) { //两个标记都要乘上c
sum[o] = 1ll * sum[o] * c % p;
bj1[o] = 1ll * bj1[o] * c % p;
bj2[o] = 1ll * bj2[o] * c % p;
return;
}
int mid = l + r >> 1;
if (bj1[o] != 1 || bj2[o]) pushdown(o, l, r);
if (ql <= mid) modify1(lc, l, mid, ql, qr, c);
if (qr > mid) modify1(rc, mid + 1, r, ql, qr, c);
sum[o] = (sum[lc] + sum[rc]) % p;
}

void modify2(int o, int l, int r, int ql, int qr, int c) {
if (ql <= l && qr >= r) { //加标记直接加上c
sum[o] += 1ll * c * (r - l + 1) % p; sum[o] %= p;
bj2[o] = (bj2[o] + c) % p;
return;
}
int mid = l + r >> 1;
if (bj1[o] != 1 || bj2[o]) pushdown(o, l, r);
if (ql <= mid) modify2(lc, l, mid, ql, qr, c);
if (qr > mid) modify2(rc, mid + 1, r, ql, qr, c);
sum[o] = (sum[lc] + sum[rc]) % p;
}

int query(int o, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) return sum[o];
int mid = l + r >> 1, ans = 0;
if (bj1[o] != 1 || bj2[o]) pushdown(o, l, r);
if (ql <= mid) ans += query(lc, l, mid, ql, qr); ans %= p;
if (qr > mid) ans += query(rc, mid + 1, r, ql, qr); ans %= p;
return ans;
}

int main() {
for (int i = 1; i <= n; ++ i) a[i] = read();
build(1, 1, n);
while (m --) {
if (opt == 1) {
modify1(1, 1, n, x, y, c);
}
else if (opt == 2) {