逛了一圈讨论区也没找到我错在哪里了qwq
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, mod;
struct node
{
int l, r, tag, multi;
long long w;
} tree[4 * maxn];
int nums[maxn];
void update(int p)
{
tree[p].w = (tree[p * 2].w + tree[p * 2 + 1].w) % mod;
}
void build(int l, int r, int p)
{
tree[p].l = l;
tree[p].r = r;
tree[p].tag = 0;
tree[p].multi = 1;
if (l == r)
{
tree[p].w = nums[l] % mod;
return;
}
int m = l + r >> 1;
build(l, m, p * 2);
build(m + 1, r, p * 2 + 1);
update(p);
tree[p].w = tree[p].w % mod;
}
void pushdown(int p)
{
int m = tree[p].l + tree[p].r >> 1;
tree[p * 2].w = (tree[p].multi * tree[p * 2].w + tree[p].tag * (tree[p * 2].r - tree[p * 2].l + 1)) % mod;
tree[p * 2 + 1].w = (tree[p].multi * tree[p * 2 + 1].w + tree[p].tag * (tree[p * 2 + 1].r - tree[p * 2 + 1].l + 1)) % mod;
tree[p * 2].multi = (tree[p].multi * tree[p * 2].multi) % mod;
tree[p * 2 + 1].multi = (tree[p].multi * tree[p * 2 + 1].multi) % mod;
tree[p * 2].tag = (tree[p].multi * tree[p * 2].tag + tree[p].tag) % mod;
tree[p * 2 + 1].tag = (tree[p].multi * tree[p * 2 + 1].tag + tree[p].tag) % mod;
tree[p].tag = 0;
tree[p].multi = 1;
}
long long getAns(int l, int r, int p)
{
if (l <= tree[p].l && tree[p].r <= r)
{
return tree[p].w;
}
else
{
pushdown(p);
int mid = (tree[p].l + tree[p].r) / 2;
long long ans = 0;
if (mid >= l)
{
ans += getAns(l, r, p * 2)%mod;
}
if (mid < r)
{
ans += getAns(l, r, p * 2 + 1)%mod;
}
return ans%mod;
}
}
void add(int l, int r, int val, int p)
{
if (l <= tree[p].l && tree[p].r <= r)
{
tree[p].w = (tree[p].w + val * (tree[p].r - tree[p].l + 1)) % mod;
tree[p].tag = (tree[p].tag + val) % mod;
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) / 2;
if (mid >= l)
{
add(l, r, val, p * 2);
}
if (mid + 1 <= r)
{
add(l, r, val, p * 2 + 1);
}
update(p);
}
void multi(int l, int r, int val, int p)
{
if (l <= tree[p].l && tree[p].r <= r)
{
tree[p].w = (val * tree[p].w) % mod;
tree[p].multi = (tree[p].multi * val) % mod;
tree[p].tag = (tree[p].tag * val) % mod;
return;
}
pushdown(p);
int mid = (tree[p].l + tree[p].r) / 2;
if (mid >= l)
{
multi(l, r, val, p * 2);
}
if (mid + 1 <= r)
{
multi(l, r, val, p * 2 + 1);
}
update(p);
}
int main()
{
cin >> n >> m >> mod;
for (int a = 1; a <= n; a++)
{
scanf("%d", &nums[a]);
}
build(1, n, 1);
while (m--)
{
int opt;
scanf("%d", &opt);
if (opt == 2)
{
int x, y, k;
scanf("%d %d %d", &x, &y, &k);
add(x, y, k, 1);
}
else if (opt == 1)
{
int x, y, k;
scanf("%d %d %d", &x, &y, &k);
multi(x, y, k, 1);
}
else
{
int x, y;
scanf("%d %d", &x, &y);
cout << getAns(x, y, 1) << "\n";
}
}
return 0;
}