Cod sursa(job #2916378)

Utilizator daniel23Malanca Daniel daniel23 Data 29 iulie 2022 15:54:07
Problema Datorii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
std::ifstream in("datorii.in");
std::ofstream out("datorii.out");

struct nod {
    int sum, l, r;
    nod *st, *dr;
};

nod *root = new nod;
int n, m, v[1001];

void cons(nod *root, int l, int r) {
    root->l = l;
    root->r = r;
    if (l == r) {
        root->sum = v[l];
    } else {
        root->st = new nod;
        root->dr = new nod;
        cons(root->st, l, (l + r) / 2);
        cons(root->dr, (l + r) / 2 + 1, r);
        root->sum = root->st->sum + root->dr->sum;
    }
}

int sum(nod *root, int l, int r) {
    if (root->r < l || root->l > r) return 0;
    if (r >= root->r && l <= root->l) return root->sum;

    return sum(root->st, l, r) + sum(root->dr, l, r);
}

void update(nod *root, int ind, int diff) {
    root->sum += diff;
    if (root->l == root->r) return;
    if (ind <= (root->l + root->r) / 2)
        update(root->st, ind, diff);
    else
        update(root->dr, ind, diff);
}

int main() {
    in >> n >> m;
    for (int i = 1; i <= n; i++) {
        in >> v[i];
    }

    cons(root, 1, n);

    for (int i = 0; i < m; i++) {
        int c, a, b;
        in >> c >> a >> b;

        if (c == 0) {
            update(root, a, -b);
        } else if (c == 1) {
            out << sum(root, a, b) << '\n';
        }
    }
}