Cod sursa(job #1897008)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 1 martie 2017 08:53:40
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <iostream>
#define maxs 15005
#define zeros(x) ( (x ^ (x - 1)) & x )
using namespace std;

ifstream f("aib.in");
ofstream g("aib.out");

int AIB[maxs], v, n, m;

void Add(int x, int quantity){
    int i;
    for (i = x; i <= n; i += zeros(i))
        AIB[i] += quantity;
}

int Compute(int x){
    int i, ret = 0;
    for (i = x; i > 0; i -= zeros(i))
        ret += AIB[i];
    return ret;
}

int main(){
    f >> n >> m;
    for(int i = 1; i <= n; ++i){
        f >> v, Add(i, v);
    }
    for(int i = 1; i <= m; ++i){
        int k, x, y;
        f >> k;
        if(!k){
            f >> x >> y;
            Add(x, y);
        }
        else if(k == 1){
            f >> x >> y;
            g << Compute(y) - Compute(x-1) << '\n';
        }
        else if(k == 2){
            f >> x;
            for(int j = 1; j <= n; ++j){
                if(Compute(j) == x){
                    g << j << '\n';
                    break;
                }
            }
        }
    }


}