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 96 97 98 99 100 101 102
| #include <iostream> #define ll long long #define mid (l + r) >> 1 #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r const int maxn = 100010; int n, m, p; ll sum[maxn << 2]; struct node { ll mul, add; } tag[maxn << 2]; ; using namespace std;
void pushup(int rt) { sum[rt] = (sum[rt << 1] + sum[rt << 1 | 1]) % p; }
void pushdown(int rt, int l, int r) { int l_node = rt << 1, r_node = rt << 1 | 1, m = mid; sum[l_node] = (sum[l_node] * tag[rt].mul % p + tag[rt].add * (m - l + 1) % p) % p; tag[l_node].mul = (tag[l_node].mul * tag[rt].mul) % p; tag[l_node].add = (tag[l_node].add * tag[rt].mul + tag[rt].add) % p;
sum[r_node] = (sum[r_node] * tag[rt].mul % p + tag[rt].add * (r - (m + 1) + 1) % p) % p; tag[r_node].mul = (tag[r_node].mul * tag[rt].mul) % p; tag[r_node].add = (tag[r_node].add * tag[rt].mul + tag[rt].add) % p;
tag[rt].add = 0; tag[rt].mul = 1; }
void build(int rt, int l, int r) { if (l == r) { cin >> sum[rt]; return; } int m = mid; build(lson); build(rson); pushup(rt); }
void update(char op, int x, int y, int num, int rt, int l, int r) { if (x <= l && r <= y) { if (op == 'A') { sum[rt] = (sum[rt] + (ll) (r - l + 1) * num % p) % p; tag[rt].add = (tag[rt].add + num) % p; } else { sum[rt] = (sum[rt] * num) % p; tag[rt].mul = (tag[rt].mul * num) % p; tag[rt].add = (tag[rt].add * num) % p; } return; } pushdown(rt, l, r); int m = mid; if (x <= m) update(op, x, y, num, lson); if (y > m) update(op, x, y, num, rson); pushup(rt); }
ll query(int x, int y, int rt, int l, int r) { if (x <= l && r <= y) return sum[rt]; pushdown(rt, l, r); int m = mid; ll ans = 0; if (x <= m) ans += query(x, y, lson); if (y > m) ans += query(x, y, rson); return ans; }
void init(int n) { for (int i = 1; i <= 4 * n; i++) { tag[i].add = 0; tag[i].mul = 1; } }
int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> p; init(n); build(1, 1, n); int num; while (m--) { int order, x, y; cin >> order >> x >> y; if (order == 1) { cin >> num; update('M', x, y, num, 1, 1, n); } else if (order == 2) { cin >> num; update('A', x, y, num, 1, 1, n); } else { cout << query(x, y, 1, 1, n) % p << "\n"; } } return 0; }
|