1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 50; int n, m, mod, a[maxn];
struct { int l, r; ll s, tag_add, tag_mul; #define l(x) tr[x].l #define r(x) tr[x].r #define s(x) tr[x].s #define tag_add(x) tr[x].tag_add #define tag_mul(x) tr[x].tag_mul #define lc(x) x << 1 #define rc(x) (x << 1) + 1 } tr[4 * maxn];
void up(int p) { s(p) = (s(lc(p)) + s(rc(p))) % mod; }
void build(int p, int l, int r) { l(p) = l, r(p) = r; s(p) = 0, tag_add(p) = 0, tag_mul(p) = 1; if (l == r) { s(p) = a[l]; return; } int mid = (l + r) / 2; build(lc(p), l, mid), build(rc(p), mid + 1, r); up(p); }
void add(int p, ll v) { s(p) = (s(p) + (r(p) - l(p) + 1) * v) % mod; tag_add(p) = (tag_add(p) + v) % mod; }
void mul(int p, ll v) { s(p) = (s(p) * v) % mod; tag_mul(p) = (tag_mul(p) * v) % mod; tag_add(p) = (tag_add(p) * v) % mod; }
void down(int p) { if (tag_mul(p) != 1) { mul(lc(p), tag_mul(p)), mul(rc(p), tag_mul(p)); tag_mul(p) = 1; } if (tag_add(p)) { add(lc(p), tag_add(p)), add(rc(p), tag_add(p)); tag_add(p) = 0; } }
void update(int p, int l, int r, ll v, int opt) { if (l <= l(p) && r(p) <= r) { if (opt == 1) mul(p, v); if (opt == 2) add(p, v); return; } down(p); int mid = (l(p) + r(p)) / 2; if (l <= mid) update(lc(p), l, r, v, opt); if (r > mid) update(rc(p), l, r, v, opt); up(p); }
ll ask(int p, int l, int r) { if (l <= l(p) && r(p) <= r) return s(p); down(p); int mid = (l(p) + r(p)) / 2; ll ans = 0; if (l <= mid) ans = (ans + ask(lc(p), l, r)) % mod; if (r > mid) ans = (ans + ask(rc(p), l, r)) % mod; return ans; }
int main() { scanf("%d%d%d", &n, &m, &mod); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); build(1, 1, n); int opt, x, y; ll k; while (m--) { scanf("%d", &opt); if (opt == 1 || opt == 2) { scanf("%d%d%lld", &x, &y, &k); update(1, x, y, k, opt); } else { scanf("%d%d", &x, &y); printf("%lld\n", ask(1, x, y)); } } return 0; }
|