Cod sursa(job #1414409)

Utilizator irimiecIrimie Catalin irimiec Data 2 aprilie 2015 16:30:26
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
#include <bits/stdc++.h>

using namespace std;

#define     mp              make_pair
#define     fs              first
#define     sc              second
#define     pob             pop_back
#define     pub             push_back
#define     eps             1E-7
#define     sz(a)           a.size()
#define     count_one       __builtin_popcount;
#define     count_onell     __builtin_popcountll;
#define     fastIO          ios_base::sync_with_stdio(false)
#define     PI              (acos(-1.0))
#define     linf            (1LL<<62)//>4e18
#define     inf             (0x7f7f7f7f)//>2e9

#ifndef ONLINE_JUDGE
ifstream fin("aib.in");
ofstream fout("aib.out");
#endif

typedef long long ll;

inline int lsb(int x) {
    return x & (-x);
}

const int MAXN = 100010;
int n, m;
int arb[MAXN];

void update(int pos, int val) {
    for(; pos <= n; pos += lsb(pos))
        arb[pos] += val;
}

ll query(int b) {
    ll sum = 0;
    for(; b; b -= lsb(b)) {
        sum += arb[b];
    }

    return sum;
}

int src(ll sum) {
    int step;
    for(step = 1; step < n; step <<= 1);

    for(int i = 0; step; step >>= 1)
    if(i + step <= n && sum >= arb[i + step]) {
        i += step;
        sum -= arb[i];

        if(!sum)
            return i;
    }

    return -1;
}

void read() {
	fin >> n >> m;

	int pos, val;
	for(int i = 1; i <= n; ++i) {
        fin >> val;
        update(i, val);
	}

	int t, a, b;
	ll sum;
	while(m--) {
        fin >> t;
        if(t == 0) {
            fin >> pos >> val;
	        update(pos, val);
        }

        if(t == 1) {
            fin >> a >> b;
            fout << query(b) - query(a - 1) << "\n";
        }

        if(t == 2) {
            fin >> sum;
            fout << src(sum) << "\n";
        }
	}
}

int main() {
	read();

    return 0;
}