题面样例能过。。。
#include <cstdio>
using namespace std;
using ll = long long;
int mod;
const int maxn = 1e5 + 5;
ll a[maxn];
struct node {
int s, e;
ll ans, slazy, mlazy;
} G[maxn * 4];
inline int lch(int u) {
return u * 2;
}
inline int rch(int u) {
return u * 2 + 1;
}
inline void push_up(int u) {
G[u].ans = (G[lch(u)].ans + G[rch(u)].ans) % mod;
}
void build(int u, int l, int r) {
//printf("%d %d %d;\n", u, l, r);
G[u].s = l;
G[u].e = r;
G[u].slazy = 0;
G[u].mlazy = 1; // 否则0*x(-inf<=x<=inf)=0
if (l == r) {
G[u].ans = a[l] % mod;
return;
}
int mid = (l + r) >> 1;
build(lch(u), l, mid);
build(rch(u), mid + 1, r);
push_up(u);
}
inline void push_down(int u) {
G[lch(u)].ans = (G[lch(u)].ans * G[u].mlazy + G[u].slazy * (G[lch(u)].e-G[lch(u)].s + 1)) % mod;
G[rch(u)].ans = (G[rch(u)].ans * G[u].mlazy + G[u].slazy * (G[rch(u)].e-G[rch(u)].s + 1)) % mod;
G[lch(u)].slazy = (G[lch(u)].slazy * G[u].mlazy + G[u].slazy) % mod;
G[rch(u)].slazy = (G[rch(u)].slazy * G[u].mlazy + G[u].slazy) % mod;
G[lch(u)].mlazy = (G[lch(u)].mlazy * G[u].mlazy) % mod;
G[rch(u)].mlazy = (G[rch(u)].mlazy * G[u].mlazy) % mod;
G[u].slazy = 0;
G[u].mlazy = 1;
}
void impact_add(int u, int s, int e, ll sw) {
//printf("%d %d %d %lld\n", u, G[u].s, G[u].e, sw);
if (s <= G[u].s && G[u].e <= e) {
G[u].slazy += (G[u].slazy + sw) % mod;
G[u].ans += ((G[u].e - G[u].s + 1) * sw) % mod;
return;
}
push_down(u);
int mid = (G[u].s + G[u].e) >> 1;
if (s <= mid) { impact_add(lch(u), s, e, sw); }
if (e > mid) { impact_add(rch(u), s, e, sw); }
push_up(u);
}
void impact_mul(int u, int s, int e, ll mw) {
//printf("%d %d %d %lld\n", u, G[u].s, G[u].e, mw);
if (s <= G[u].s && G[u].e <= e) {
G[u].ans = (G[u].ans * mw) % mod;
G[u].slazy = (G[u].slazy * mw) % mod;
G[u].mlazy = (G[u].mlazy * mw) % mod;
return;
}
push_down(u);
int mid = (G[u].s + G[u].e) >> 1;
if (s <= mid) { impact_mul(lch(u), s, e, mw); }
if (e > mid) { impact_mul(rch(u), s, e, mw); }
push_up(u);
}
ll query(int u, int s, int e) {
if (s <= G[u].s && G[u].e <= e) {
return G[u].ans;
}
push_down(u);
ll re = 0;
int mid = (G[u].s + G[u].e) >> 1;
if (s <= mid) { re += query(lch(u), s, e); re %= mod;}
if (e > mid) { re += query(rch(u), s, e); re %= mod;}
return re;
}
void print(int u, int l, int r) {
printf("node %d: (%d~%d), sum=%lld\n", u, l, r, G[u].ans);
if (l >= r) { return; }
int mid = (l + r) / 2;
print(u * 2, l, mid);
print(u * 2 + 1, mid + 1, r);
}
int main() {
int n, q;
scanf("%d%d%d", &n, &q, &mod);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
build(1, 1, n);
print(1, 1, n);
while (q--) {
int op;
scanf("%d", &op);
if (op == 1) {
int x, y;
ll k;
scanf("%d%d%lld", &x, &y, &k);
impact_mul(1, x, y, k);
}
if (op == 2) {
int x, y;
ll k;
scanf("%d%d%lld", &x, &y, &k);
impact_add(1, x, y, k);
}
if (op == 3) {
int x, y;
scanf("%d%d", &x, &y);
printf("%lld\n", query(1, x, y) % mod);
}
//print(1, 1, n);
}
return 0;
}