Pagini recente » Cod sursa (job #14282) | Cod sursa (job #2722) | clasa_9_nationala_2012-2016 | Cod sursa (job #715073) | Cod sursa (job #609319)
Cod sursa(job #609319)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
vector<int> A (1<<15, 0);
int N, M;
void updateNode(int val, int pos) {
int real_pos = (1 << 14) + pos;
A[real_pos] += val;
while (real_pos != 1) {
real_pos >>= 1;
A[real_pos] += val;
}
}
int get_element(int pos) {
int real_pos = (1 << 14) + pos;
return A[real_pos];
}
int get_sum(int pos) {
int virt = 1 << 13, sum = 0, current = 1;
while (virt) {
if (virt & pos ) {
current = (current << 1) + 1;
sum += A[current - 1];
} else {
current = (current << 1);
}
virt >>= 1;
}
return sum;
}
int get_pos(int sum) {
int current = 2;
if (!sum)
return 0;
for (; current < (1 << 15); current = current << 1) {
if (A[current] < sum) {
sum -= A[current];
current++;
}
}
current >>= 1;
if (sum != A[current])
return -1;
else
return current - (1 << 14);
}
int main () {
int val, type, a , b;
freopen("aib.in", "r", stdin);
freopen("aib.out", "w", stdout);
scanf("%d %d", &N, &M);
for (int i = 0; i < N ; i++) {
scanf("%d", &val);
updateNode(val, i);
}
for (int i = 0; i < M; i++) {
scanf("%d", &type);
switch(type) {
case 0:
scanf("%d %d", &a, &b);
updateNode(b, a - 1);
break;
case 1:
scanf("%d %d", &a, &b);
printf("%d\n", get_sum(b - 1) - get_sum(a - 1) + get_element(b - 1));
break;
case 2:
scanf("%d", &a);
printf("%d\n", get_pos(a) + 1);
break;
default:
break;
}
}
return 0;
}