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 103 104 105 106 107 108 109
| #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; add[v*2] = (add[v*2]*mul[v]%p+add[v])%p; tree[v*2] = (tree[v*2]*mul[v]%p+add[v]*(ll)(mid-s+1)%p)%p; mul[v*2+1] = mul[v*2+1]*mul[v]%p; add[v*2+1] = (add[v*2+1]*mul[v]%p+add[v])%p; tree[v*2+1] = (tree[v*2+1]*mul[v]%p+add[v]*(ll)(t-mid)%p)%p; mul[v] = 1; add[v] = 0; return; } void create(int v, int s, int t) { mul[v] = 1; add[v] = 0; 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) { add[v] = (add[v]+(ll)x)%p; 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; add[v] = add[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; }
|