Cod sursa(job #2564792)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 2 martie 2020 10:26:13
Problema Arbori indexati binar Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
using namespace std;

ifstream fin("aib.in");
ofstream fout("aib.out");

int n, m, aib[100005];

void update(int x, int poz) {
    while(poz <= n) {
        aib[poz] += x;
        poz += (poz & -poz);
    }
}

int query(int poz) {
    int res = 0;
    while(poz > 0) {
        res += aib[poz];
        poz -= (poz & -poz);
    }
    return res;
}

void citire() {
    fin >> n >> m;
    int x;
    for(int i = 1; i <= n; i++) {
        fin >> x;
        update(x, i);
    }
}

int main() {
    citire();
    int t, a, b;
    while(m--) {
        fin >> t;
        if(t == 2) {
            fin >> a;
            fout << 1 << '\n';
        } else {
            fin >> a >> b;
            if(t == 0)
                update(b, a);
            else
                fout << query(b)-query(a-1) << '\n';
        }
    }
}