Cod sursa(job #2560087)

Utilizator Bulboaca_EugenBulboaca Alexandru Eugen Bulboaca_Eugen Data 27 februarie 2020 19:15:56
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.11 kb
#include <bits/stdc++.h>
using namespace std;
#define zero(x) (x & (-x))
ifstream fin("aib.in");
ofstream fout("aib.out");
const int  MAXN = 1e5 + 3;
int aib[MAXN], v[MAXN], n, m;
void update(int pos, int val){
    for(int i = pos; i <= n; i += zero(i))
        aib[i] += val;
}
int query(int pos){
    int s = 0;
    for(int i = pos; i >= 1; i -= zero(i))
        s += aib[i];
    return s;
}
int findd(int pos){
    int current = 0;
    for(int power = 30; power >= 0; --power){
        int k = current + (1 << power);
        if(k <= n and query(k) < pos)
            current += (1 << power);
    }
    if(query(current + 1) == pos) return current + 1;
    return -1;
}
int main(){
    fin >> n >> m;
    for(int i = 1; i <= n; ++i){
        fin >> v[i];
        update(i, v[i]);
    }
    for(int i = 1; i <= m; ++i){
        int type, x, y;
        fin >> type >> x;
        if(type == 2) fout << findd(x) << '\n';
        else if(type == 1){
            fin >> y ;
            fout << query(y) - query(x - 1) << '\n';
        }
        else fin >> y, update(x, y);
    }

    return 0;
}