Pagini recente » Monitorul de evaluare | Cod sursa (job #2598507) | Cod sursa (job #635754) | Cod sursa (job #1642862) | Cod sursa (job #1910392)
#include <cstdio>
int length, operations, task, BIT[100001], a, b;
void Update(int position, int value){
for(int index = position; index <= length; index += (index & (-index))){
BIT[index] += value;
}
}
int Querry(int position){
int sum = 0;
for(int index = position; index > 0; index -= (index & (-index))){
sum += BIT[index];
}return sum;
}
int Search(int sum){
int left = 1, right = length, position = -1;
while(left <= right){
int middle = (left + right) / 2;
int guess = Querry(middle);
if(sum < guess){
right = middle - 1;
}else if(sum > guess){
left = middle + 1;
}else{
position = middle;
break;
}
}return position;
}
int main(){
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
scanf("%d %d", &length, &operations);
for(int i = 1; i <= length; i++){
scanf("%d", &a);
Update(i, a);
}
while(operations--){
scanf("%d", &task);
switch(task){
case 0: scanf("%d %d", &a, &b);
Update(a, b);
break;
case 1: scanf("%d %d", &a, &b);
printf("%d\n", Querry(b) - Querry(a - 1));
break;
case 2: scanf("%d", &a);
printf("%d\n", Search(a));
}
}
return 0;
}