Cod sursa(job #2908048)

Utilizator LeperBearMicu Alexandru LeperBear Data 1 iunie 2022 11:12:16
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

ifstream in("datorii.in");
ofstream out("datorii.out");
#define cin in
#define cout out
#define NMAX 15005

int s[NMAX], n, m;

void update(int pos, int val) {
    while (pos <= n) {
        s[pos] += val;
        pos += pos & -pos;
    }
}

int query(int pos) {
    int sum = 0;
    while (pos) {
        sum += s[pos];
        pos -= pos & -pos;
    }
    return sum;
}

void range_query(int l, int r) {
    cout << query(r) - query(l - 1) << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        update(i, x);
    }
    
    for (int i = 1; i <= m; i++) {
        int cmd, x, y;
        cin >> cmd >> x >> y;
        if (cmd)
            range_query(x, y);
        else
            update(x, -y);
    }

    return 0;
}