Cod sursa(job #2811088)

Utilizator GeorgeStreyStresna George GeorgeStrey Data 1 decembrie 2021 10:10:54
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, m, aib[100005];

void build(int pos, int nr) {
    for (int i = pos; i <= n; i += i & (-i))
        aib[i] += nr;
    return;
}

void update(int pos, int nr) {
    for (int i = pos; i <= n; i += i & (-i))
        aib[i] -= nr;
    return;
}

int query(int pos) {
    int rez = 0;
    for (int i = pos; i > 0; i -= i & (-i))
        rez += aib[i];
    return rez;
}

int main() {
    fin >> n >> m;
    for (int i = 1, el; i <= n; ++i) {
        fin >> el;
        build(i, el);
    }
    while (m--) {
        int a, b, op;
        fin >> op >> a >> b;
        if (op == 0)
            update(a, b);
        else if (op == 1)
            fout << query(b) - query(a - 1) << "\n";
    }
    return 0;
}