Cod sursa(job #193600)

Utilizator andrei.12Andrei Parvu andrei.12 Data 5 iunie 2008 11:58:35
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
#include<stdio.h>

int n, m, i, x, tip, poz, a, b, v[100005];

void update(int poz, int val){
	while (poz <= n){
		v[poz] += val;

		poz += (poz^(poz-1)) & poz;
	}
}
int query(int poz){
	int rez = 0;

	while (poz > 0){
		rez += v[poz];

		poz -= (poz^(poz-1)) & poz;
	}

	return rez;
}
int bs(int val){
	int li = 1, ls = n, x, gs;

	while (li <= ls){
		x = (li+ls) / 2;

		a = query(x);
		if (a >= val){
			if (a == val)
				gs = x;
			ls = x-1;
		}
		else
			li = x+1;
	}

	return gs;
}

int main()
{
	freopen("aib.in", "rt", stdin);
	freopen("aib.out", "wt", stdout);

	scanf("%d%d", &n, &m);
	for (i = 1; i <= n; i ++){
		scanf("%d", &x);

		update(i, x);
	}

	for (i = 1; i <= m; i ++){
		scanf("%d", &tip);

		if (tip == 0){
			scanf("%d%d", &poz, &x);

			update(poz, x);
			continue;
		}
		if (tip == 1){
			scanf("%d%d", &a, &b);

			printf("%d\n", query(b) - query(a-1));
		}
		if (tip == 2){
			scanf("%d", &a);

			printf("%d\n", bs(a));
		}
	}

	return 0;
}