Pagini recente » Cod sursa (job #1001102) | Rating zero unu (zeR0_) | Cod sursa (job #895014) | Cod sursa (job #1257474) | Cod sursa (job #2182711)
#include <cstdio>
using namespace std;
#define NMAX 100005
int n, m, aib[NMAX], a[NMAX];
void update(int poz, int val) {
for(; poz<=n; poz += poz&-poz)
aib[poz] += val;
}
int querry(int poz) {
int s = 0;
for(; poz>0; poz-=poz&-poz)
s += aib[poz];
return s;
}
int pozitie(int x) {
int i;
for(i=0; (1 << i) <=n; i++)
if(aib[ (1 << i) ] == x)
return (1 << i);
return -1;
}
int main()
{
int i, test, x, y;
FILE *fin, *fout;
fin = fopen("aib.in", "r");
fout = fopen("aib.out", "w");
fscanf(fin, "%d %d", &n, &m);
for(i=1; i<=n; i++) {
fscanf(fin, "%d ", &a[i]);
update(i, a[i]);
}
for(i=1; i<=m; i++) {
fscanf(fin, "%d ", &test);
if(test == 0) {
fscanf(fin, "%d %d", &x, &y);
update(x, y);
}
else
if(test == 1) {
fscanf(fin, "%d %d", &x, &y);
fprintf(fout, "%d\n", querry(y) - querry(x-1));
}
else {
fscanf(fin, "%d", &x);
fprintf(fout, "%d\n", pozitie(x));
}
}
return 0;
}