树状数组模板_区间修改_单点查询

https://loj.ac/s/1286290

#include <bits/stdc++.h>
#define int long long
#define _rep(i,a,b) for( int i=(a); i<=(b); ++i)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int mx = 1e6 + 9;
//差分即可
int a[mx], c[mx], n, m;
int lowbit(int x) {
    return x & (-x);
}
void add(int i, int k) {
    while (i <= n) {
        c[i] += k;
        i += lowbit(i);
    }
}
int query(int i) {
    int ok = 0;

    while (i) {
        ok += c[i];
        i -= lowbit(i);
    }

    return ok;
}

signed main() {
    IOS;
    cin >> n >> m;
    _rep(i, 1, n) {
        cin >> a[i];
        add(i, a[i] - a[i - 1]);
    }

    while (m--) {
        int op;
        cin >> op;

        if (op == 1) {
            int l, r, x;
            cin >> l >> r >> x;
            add(l, x);
            add(r + 1, -x);
        }

        if (op == 2) {
            int x;
            cin >> x;
            int ok = query(x);
            cout << ok << "\n";
        }
    }


    return 0;
}
上一篇:2021-10-28


下一篇:旧版Vue配置API_ROOT,开发、生产地址切换