Cod sursa(job #3182433)

Utilizator Traian_7109Traian Mihai Danciu Traian_7109 Data 8 decembrie 2023 22:41:11
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <iostream>
#include <fstream>

using namespace std;

const int nmax = 15000;
int a[5 + nmax];

struct Aib {
    int bit[5 + nmax], n;

    Aib(int _n) {
        n = _n;
        for (int i = 0; i <= n; i++) {
            bit[i] = 0;
        }
    }

    int lsb(int x) {
        return x & -x;
    }

    void update(int pos, int val) {
        if (pos == 0) {
            return;
        }
        for (int i = pos; i <= n; i += lsb(i)) {
            bit[i] += val;
        }
    }

    int query(int pos) {
        int ans = 0;
        for (int i = pos; i > 0; i -= lsb(i)) {
            ans += bit[i];
        }
        return ans;
    }
    
    int query(int left, int right) {
        return query(right) - query(left - 1);
    }
};

signed main() {
    ifstream fin("datorii.in");
    ofstream fout("datorii.out");
    int n, m;
    fin >> n >> m;
    for (int i = 1; i <= n; i++) {
        fin >> a[i];
    }
    Aib aib(n);
    for (int i = 1; i <= n; i++) {
        aib.update(i, a[i]);
    }
    while (m--) {
        int type;
        fin >> type;
        if (type == 0) {
            int pos, val;
            fin >> pos >> val;
            aib.update(pos, -val);
        } else {
            int left, right;
            fin >> left >> right;
            fout << aib.query(left, right) << '\n';
        }
    }
    return 0;
}