Cod sursa(job #3254799)

Utilizator Luca_georgescuLucageorgescu Luca_georgescu Data 8 noiembrie 2024 20:47:48
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("datorii.in");
ofstream g("datorii.out");

int n, m, c, a, b, v[15005], t[60020];

void update(int nod, int a, int b, int poz, int val) {
    if (a == b) {
        t[nod] = val;
        return;
    }
    int mij = (a + b) / 2;
    if (mij >= poz)
        update(2 * nod, a, mij, poz, val);
    else
        update(2 * nod + 1, mij + 1, b, poz, val);
    t[nod] = t[2 * nod] + t[2 * nod + 1];
}

int query(int nod, int a, int b, int p, int q) {
    if (a > q || b < p)
        return 0;
    if (a >= p && b <= q)
        return t[nod];
    int mij = (a + b) / 2;
    return query(2 * nod, a, mij, p, q) + query(2 * nod + 1, mij + 1, b, p, q);
}

void read() {
    f >> n >> m;
    for (int i = 1; i <= n; i++) {
        f >> v[i];
        update(1, 1, n, i, v[i]);
    }
}

int main() {
    read();
    for (int i = 1; i <= m; i++) {
        f >> c >> a >> b;
        if (c == 1)
            g << query(1, 1, n, a, b) << '\n';
        else {
            v[a] -= b;
            update(1, 1, n, a, v[a]);
        }
    }
    return 0;
}