Pagini recente » Cod sursa (job #1158935) | Cod sursa (job #2808967) | Cod sursa (job #2926277)
#include <cstdio>
#include <cmath>
using namespace std;
FILE *fin, *fout;
int n;
const int NMAX = 100000;
int BIT[NMAX + 5], v[NMAX + 5];
void update(int pos, int value)
{
while(pos <= n)
{
BIT[pos] += value;
pos += (pos & (-pos));
}
}
int query(int pos)
{
int ans = 0;
while(pos > 0)
{
ans += BIT[pos];
pos -= (pos & (-pos));
}
return ans;
}
int query2(int value)
{
int max2 = log2(n), poz_c = 0, sum = 0;
for(int exp = max2; exp >= 0; exp--)
{
int poz_cand = poz_c + (1 << exp);
if(poz_cand <= n and BIT[poz_cand] + sum <= value)
{
sum += BIT[poz_cand];
poz_c = poz_cand;
}
}
if(sum == 0 or poz_c == 0)
return -1;
if(sum == value)
return poz_c;
return -1;
}
int main()
{
fin = fopen("aib.in", "r");
fout = fopen("aib.out", "w");
int m;
fscanf(fin, "%d%d", &n, &m);
for(int i = 1; i <= n; i++)
fscanf(fin, "%d", &v[i]);
int op, a, b;
for(int i = 1; i <= n; i++)
update(i, v[i]);
/*for(i = 1; i <= n; i++)
fprintf(fout , "%d " , BIT[i]);
fprintf(fout , "\n");*/
for(int i = 1; i <= m; i++)
{
fscanf(fin, "%d", &op);
if(op == 0 or op == 1)
{
fscanf(fin, "%d%d", &a, &b);
if(op == 0)
update(a, b);
else fprintf(fout, "%d\n", query(b) - query(a - 1));
}
else
{
fscanf(fin, "%d", &a);
fprintf(fout, "%d\n", query2(a));
}
}
fclose(fin);
fclose(fout);
return 0;
}