求调
查看原帖
求调
1145337
Phantom_Landler楼主2024/11/20 14:45
#include <bits/stdc++.h>

#define endl '\n'
#define mid ((l + r) >> 1)
using namespace std;
const int N = 200010;
int a[N];
using ll = long long;
ll mod = 998244353;
struct tree {
    int l, r;
    ll sum, add, mul;
};
tree tr[N * 4];

void pushup(int p) {
    tr[p].sum = (tr[tr[p].l].sum + tr[tr[p].r].sum) % mod;
}

void change(int p, int add, int mul, int l, int r) {
    tr[p].sum = ((tr[p].sum * mul % mod) + add * (r - l + 1)) % mod;
    tr[p].mul = tr[p].mul * mul % mod;
    tr[p].add = (tr[p].add * mul + add) % mod;
}

void pushdown(int p, int l, int r) {
    change(tr[p].l, tr[p].add, tr[p].mul, l, mid);
    change(tr[p].r, tr[p].add, tr[p].mul, mid + 1, r);
    tr[p].add = 0, tr[p].mul = 1;
}

void build(int p, int l, int r) {
    if (l == r) {
        tr[p].sum = a[l];
        tr[p].mul = 1;
        return;
    }
    tr[p].l = p << 1, tr[p].r = p << 1 | 1;
    build(tr[p].l, l, mid);
    build(tr[p].r, mid + 1, r);
    pushup(p);
}

void modify(int p, int add, int mul, int ql, int qr, int l, int r) {
    if (ql <= l && r <= qr) {
        change(p, add, mul, l, r);
        return;
    }
    pushdown(p, l, r);
    if (ql <= mid) modify(tr[p].l, add, mul, ql, qr, l, mid);
    if (qr > mid) modify(tr[p].r, add, mul, ql, qr, mid + 1, r);
    pushup(p);
}

ll query(int p, int ql, int qr, int l, int r) {
    if (ql <= l && r <= qr) {
        return tr[p].sum;
    }
    pushdown(p, l, r);
    ll res = 0;
    if (ql <= mid) res += (res + query(tr[p].l, ql, qr, l, mid)) % mod;
    if (qr > mid) res = (res + query(tr[p].r, ql, qr, mid + 1, r)) % mod;
    return res;
}

int n, m;

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    ll op;
    cin >> n >> m >> mod;
    for (ll i = 1; i <= n; i++) cin >> a[i];
    build(1, 1, n);
    while (m--) {
        cin >> op;
        int x, y, k;
        if (op == 1) {
            cin >> x >> y >> k;
            modify(1, 0, k, x, y, 1, n);
        } else if (op == 2) {
            cin >> x >> y >> k;
            modify(1, k, 1, x, y, 1, n);
        } else {
            cin >> x >> y;
            cout << query(1, x, y, 1, n) << endl;
        }
    }
    return 0;
}
2024/11/20 14:45
加载中...