Cod sursa(job #389130)

Utilizator SzabiVajda Szabolcs Szabi Data 1 februarie 2010 00:31:48
Problema Arbori indexati binar Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <stdio.h>
unsigned long  n=0,m=0;
unsigned long  tree[100001];


unsigned  long read(  long idx){
unsigned long  sum=0;
while (idx>0){

sum+=tree[idx];
idx-=(idx & -idx);
}
return sum;

}

void update(  long idx,unsigned  long val){

	while(idx<=n){

tree[idx]+=val;
idx+= (idx & -idx);
	}

}

 long  elso(unsigned long a){
unsigned  long i=1;
bool jo=false;

while((!jo)&&(i<=n)){
	if (read(i)==a){jo=true;}else{i++;}

}

if (jo) {return i;}else {return -1;}
}

int main(void){
freopen("aib.in","r",stdin);
freopen("aib.out","w",stdout);
tree[0]=0;
unsigned  long i=0,temp,a,b;

scanf("%Ld %Ld",&n,&m);

for(i=1;i<=n;i++){

scanf("%Ld",&temp);
update(i,temp);
}

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

switch(temp){

case 0:
	scanf("%Ld %Ld",&a,&b);
	update(a,b);
	break;
case 1:
	scanf("%Ld %Ld",&a,&b);
	printf("%Ld\n",read(b)-read(a-1));

	break;
case 2:
scanf("%Ld",&a);
printf("%Ld\n",elso(a));
	break;

}

}
return 0;
}