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
| #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #define ll long long #define int long long #define INF 0x7fffffff #define re register #define rep(i,a,b) for(int i = (a); i <= (b); i++) #define qwq printf("qwq\n"); #define lson tre << 1 #define rson tre << 1 | 1
using namespace std;
long long read() { register long long x = 0,f = 1;register char ch; ch = getchar(); while(ch > '9' || ch < '0'){if(ch == '-') f = -f;ch = getchar();} while(ch <= '9' && ch >= '0'){x = x * 10 + ch - 48;ch = getchar();} return x * f; }
int n,m,p,opt,x,y,z;
struct node { int sum,lazy1,lazy2; }t[1000005];
void build(int tre,int l,int r) { t[tre].lazy2 = 1; if(l == r) { t[tre].sum = read() % p; return ; } int mid = (l + r) >> 1; build(lson,l,mid); build(rson,mid + 1,r); t[tre].sum = (t[lson].sum + t[rson].sum) % p; }
void pushdown(int tre,int l,int r) { int mid = (l + r) >> 1; t[lson].lazy1 = (t[lson].lazy1 * t[tre].lazy2 + t[tre].lazy1) % p; t[rson].lazy1 = (t[rson].lazy1 * t[tre].lazy2 + t[tre].lazy1) % p; t[lson].lazy2 = (t[lson].lazy2 * t[tre].lazy2) % p; t[rson].lazy2 = (t[rson].lazy2 * t[tre].lazy2) % p; t[lson].sum = ((t[lson].sum * t[tre].lazy2) % p + (t[tre].lazy1 * (mid - l + 1)) % p) % p; t[rson].sum = ((t[rson].sum * t[tre].lazy2) % p + (t[tre].lazy1 * (r - mid)) % p) % p; t[tre].lazy1 = 0; t[tre].lazy2 = 1; }
void updata1(int tre,int l,int r,int x,int y,int z) { if(l >= x && r <= y) { t[tre].sum = (t[tre].sum + (r - l + 1) * z) % p; t[tre].lazy1 = (t[tre].lazy1 + z) % p; return ; } pushdown(tre,l,r); int mid = (l + r) >> 1; if(x <= mid) updata1(lson,l,mid,x,y,z); if(y > mid) updata1(rson,mid + 1,r,x,y,z); t[tre].sum = (t[lson].sum + t[rson].sum) % p; }
void updata2(int tre,int l,int r,int x,int y,int z) { if(l >= x && r <= y) { t[tre].sum = (t[tre].sum * z) % p; t[tre].lazy1 = (t[tre].lazy1 * z) % p; t[tre].lazy2 = (t[tre].lazy2 * z) % p; return ; } pushdown(tre,l,r); int mid = (l + r) >> 1; if(x <= mid) updata2(lson,l,mid,x,y,z); if(y > mid) updata2(rson,mid + 1,r,x,y,z); t[tre].sum = (t[lson].sum + t[rson].sum) % p; }
int query(int tre,int l,int r,int x,int y) { if(l >= x && r <= y) return t[tre].sum; pushdown(tre,l,r); int mid = (l + r) >> 1,t1 = 0,t2 = 0; if(x <= mid) t1 = query(lson,l,mid,x,y); if(y > mid) t2 = query(rson,mid + 1,r,x,y); return (t1 + t2) % p; }
signed main() { n = read(); m = read(); p = read(); build(1,1,n); for(int i = 1; i <= m; i++) { opt = read(); x = read(); y = read(); if(opt == 1) z = read(), updata2(1,1,n,x,y,z); if(opt == 2) z = read(), updata1(1,1,n,x,y,z); if(opt == 3) printf("%lld\n",query(1,1,n,x,y)); } return 0; }
|