Cod sursa(job #1224259)

Utilizator andreas.chelsauAndreas Chelsau andreas.chelsau Data 30 august 2014 12:11:05
Problema Arbori indexati binar Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <iostream>
#include <stdio.h>
using namespace std;

int n,m,sum[100010];


void update(int idx ,int val){
	for(; idx <= n; idx += (idx & -idx)){
		sum[idx] += val;
	}
}
int query(int idx){
	int s = 0;
	for(; idx > 0; idx -= (idx & -idx)){
		s += sum[idx];
	}
	return s;
}
void query2(int start,int end){
	printf("%d\n", query(end) - query(start));
}
void binary_s(int elt){
	int start = 1,end = n;
	while(start <= end){
		int m = start + ((end - start) >> 1);
		if(query(m) > elt)
			end = m - 1;
		else
			if(query(m) < elt)
				start = m + 1;
			else{
				printf("%d\n",m);
				return;
			}

	}
	printf("-1\n");
}
int main(){
	freopen("aib.in","r",stdin);
	freopen("aib.out","w",stdout);
	scanf("%d%d",&n,&m);
	int elt,j;
	for(int i = 1; i <= n; i++){
		scanf("%d",&elt);
		update(i,elt);
	}
	while(m--){
		int cmd,elt,x;
		scanf("%d%d",&cmd,&elt);
		switch(cmd){
			case 0: scanf("%d",&x); update(elt,x); break;
			case 1: scanf("%d",&x);  query2(elt - 1,x); break;
			case 2: binary_s(elt);
		}
	}

	return 0;
}