Cod sursa(job #2467599)

Utilizator The_one_and_onlyMironica Vasile The_one_and_only Data 4 octombrie 2019 17:53:59
Problema Arbori indexati binar Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
using namespace std;
 
ifstream cin("aib.in");
ofstream cout("aib.out");
 
int a[100001], n, t;
 
void update(int i, int x) {
	while(i <= n) {
		a[i] += x;
		i += (i & -i);
	}
}
 
int query(int i){
	int s = 0;
	
	while(i > 0) {
		s += a[i];
		i -= (i & -i);
	}
	return s;
}
 
int bin(int s) {
	int step = 1;
	while(step < n)
		step <<= 1;
 
	for(int i = 0; step; step >>= 1)
		if(i + step <= n && s >= a[i + step]) {
			i += step;
			s -= a[i];
			
			if(!s)
				return i;
		}
 
	return -1;
}
 
int main() {
	cin >> n >> t;
	for(int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		update(i, x);
	}
	
	while(t--) {
		int cer, st;
		cin >> cer >> st;
		
		if(cer == 2)
			cout << bin(st) << '\n';
		else {
			int dr;
			cin >> dr;
			if(!cer)
				update(st, dr);
			else
				cout << query(dr) - query(st - 1) << '\n';
		}
	}
}