Cod sursa(job #3239450)

Utilizator Mihai_OctMihai Octavian Mihai_Oct Data 5 august 2024 17:01:04
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, q, i, l, a[15002];
int op, x, y;

static inline int Ub(int a) {
    return (a & -a);
}

static inline void Add(int poz, int val = 1) {
    for(int i = poz; i <= n; i += Ub(i)) a[i] += val;
}

static inline int Sum(int poz) {
    int sum = 0;
    for(int i = poz; i >= 1; i -= Ub(i)) sum += a[i];
    return sum;
}

int main() {
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n >> q;
    for(i = 1; i <= n; i++) {
        fin >> x;
        Add(i, x);
    }

    while(q--) {
        fin >> op >> x >> y;
        if(op == 0) Add(x, -y);
        else {
            fout << Sum(y) - Sum(x - 1) << "\n";
        }
    }

    return 0;
}