树状数组,我真的是受不鸟了

P3372 【模板】线段树 1

DESTRUCTION_3_2_1 @ 2023-01-17 13:18:08

#include <bits/stdc++.h>
#define int long long
#define lowbit(x) (x & -x)
using namespace std;
const int SZ = 1e5 + 5;
int n, m, opt, x, y, z, a[SZ], t1[SZ], t2[SZ];
void fmodify (int x, int k) {
    int kk = x * k;
    while (x <= k) {
        t1[x] += k, t2[x] += kk;
        x += lowbit(x);
    }
}
int fquery (int t[], int x) {
    int ret = 0;
    while (x) {
        ret += t[x];
        x -= lowbit(x);
    }
    return ret;
}
void tmodify (int l, int r, int k) {
    fmodify(l, k);
    fmodify(r + 1, -k);
}
int tquery (int l, int r) {
    int ret1 = fquery(t1, r) * (r + 1) - fquery(t1, l - 1) * l, ret2 = fquery(t2, r) - fquery(t2, l - 1);
    return ret1 - ret2;
}
signed main(void)
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        fmodify(i, a[i] - a[i - 1]);
    }
    for (int i = 1; i <= m; i++) {
        cin >> opt;
        if (opt == 1) {
            cin >> x >> y >> z;
            tmodify(x, y, z);
        }
        if (opt == 2) {
            cin >> x >> y;
            cout << tquery(x, y) << endl;
        }
    }
    return 0;
}

样例没过,求大佬救救我


by zjy2008 @ 2023-01-17 13:53:07

    while (x <= k) {

->

    while (x <= n) {

by DESTRUCTION_3_2_1 @ 2023-01-17 17:12:36

@OIdrearmer_Z wssb


|