Cod sursa(job #1888620)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 22 februarie 2017 11:39:33
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>

using namespace std;

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

int arb[100005];
int n, m, i, j, x, q, t,y;

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

int sum(int poz) {
    int s = 0;
    while (poz>0) {
        s+= arb[poz];
        poz -= (poz&-poz);
    }
    return s;
}

int main() {
    f >> n >> m;
    for (i = 1; i <= n; i++) {
        f >> x;
        update(i, x);
    }
    while (m--) {
        f >> t >> x;
        if (t <2) {
            f>>y;
            if (t==1)
                g<<sum(y)-sum(x-1)<<'\n';
            else update(x,y);
            continue;
        }
        if (t==2) {
            int p = 1;
            while (p < n)
                p *= 2;
            j = n+1;
            for (;p;p/=2) {
                if (j-p <=0)
                    continue;
                y = sum(j-p);
                if (y>=x)
                    j -= p;
            }
            g << j << '\n';
            continue;
        }
    }
    //g << sum(n);
    return 0;
}