Cod sursa(job #3247855)

Utilizator AndreiDragosDavidDragos Andrei David AndreiDragosDavid Data 9 octombrie 2024 16:33:04
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <bits/stdc++.h>

using namespace std;

class BIT {
    vector<int> bit;
    int n;

public:
    BIT(int n) : n(n) {
        bit.resize(n + 1, 0);
    }

    void update(int idx, int delta) {
        for (; idx <= n; idx += idx & -idx) {
            bit[idx] += delta;
        }
    }

    int query(int idx) {
        int sum = 0;
        for (; idx > 0; idx -= idx & -idx) {
            sum += bit[idx];
        }
        return sum;
    }

    int query(int l, int r) {
        return query(r) - query(l - 1);
    }
};

int N, M;

int32_t main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    freopen("datorii.in", "r", stdin);
    freopen("datorii.out", "w", stdout);
#endif
    cin.tie(0) -> sync_with_stdio(0);
    cin >> N >> M;

    vector<int> A(N + 1);
    BIT bit(N);

    for(int i=1; i<=N; ++i){
        cin >> A[i];
        bit.update(i, A[i]);
    }

    for(int i=0; i<M; ++i){
        bool opType;
        int x, y;
        cin >> opType >> x >> y;

        if (!opType)
            bit.update(x, -y);
        else if (opType)
            cout << bit.query(x, y) << '\n';
    }

    return 0;
}