Cod sursa(job #3132426)

Utilizator omaclearuMacelaru Octavian Andrei omaclearu Data 22 mai 2023 19:15:41
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.49 kb
#include <fstream>
#define N 15008

using namespace std;

class Datorii {
private:
    int n, q;
    int v[N];
    int T[4 * N];

    void update(int left, int right, int nod, int poz, int x) {
        if (left == right)
            T[nod] += x;
        else {
            int pivot = (left + right) / 2;
            if (poz <= pivot)
                update(left, pivot, nod << 1, poz, x);
            else
                update(pivot + 1, right, nod << 1 | 1, poz, x);

            T[nod] = T[nod << 1] + T[nod << 1 | 1];
        }
    }

    int query(int left, int right, int nod, int i, int j) {
        if (i <= left && right <= j)
            return T[nod];

        int a = 0, b = 0;
        int pivot = (left + right) / 2;
        if (i <= pivot)
            a = query(left, pivot, nod << 1, i, j);
        if (j > pivot)
            b = query(pivot + 1, right, nod << 1 | 1, i, j);

        return a + b;
    }
public:
    void run(){
        ifstream fin("datorii.in");
        ofstream fout("datorii.out");
        fin >> n >> q;
        for (int i = 1; i <= n; i++) {
            fin >> v[i];
            update(1, n, 1, i, v[i]);
        }
        for (int i = 1; i <= q; i++) {
            int task, x, y;
            fin >> task >> x >> y;
            if (!task)
                update(1, n, 1, x, -y);
            else
                fout << query(1, n, 1, x, y) << "\n";
        }
        fin.close();
        fout.close();
    }
};


int main() {
    Datorii datorii;
    datorii.run();
    return 0;
}