Cod sursa(job #2890789)

Utilizator BeilandArnoldArnold Beiland BeilandArnold Data 16 aprilie 2022 16:46:30
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.11 kb
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

void update(int n, vector<int> &aib, int pos, int increment)
{
    while (pos <= n) {
        aib[pos] += increment;
        pos += (pos & -pos);
    }
}

int query(const vector<int> &aib, int pos)
{
    int s = 0;
    while (pos != 0) {
        s += aib[pos];
        pos -= (pos & -pos);
        // pos &= (pos - 1);
    }
    return s;
}

int main()
{
    ifstream fin("datorii.in");
    ofstream fout("datorii.out");

    int n, m;
    fin >> n >> m;

    vector<int> aib(n+1, 0);

    for (int i = 1; i <= n; ++i) {
        int x;
        fin >> x;
        update(n, aib, i, x);
    }

    for (int i = 0; i < m; ++i) {
        int op;
        fin >> op;

        if (op == 0) {
            int t, v;
            fin >> t >> v;
            update(n, aib, t, -v);
        }
        else if (op == 1) {
            int p, q;
            fin >> p >> q;

            if (p == 1)
                fout << query(aib, q) << "\n";
            else {
                fout << query(aib, q) - query(aib, p-1) << "\n";
            }
        }
    }

    return 0;
}