Pagini recente » Cod sursa (job #2497453) | Cod sursa (job #2145381) | Cod sursa (job #2396464) | Cod sursa (job #2752088) | Cod sursa (job #583860)
Cod sursa(job #583860)
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
#define maxn 150000
int N, M, aib[maxn];
// adds val to position
void update(int pos, int val)
{
for (; pos <= N; pos += (pos & -pos))
{
aib[pos] += val;
}
}
int search(int val)
{
int i = 0, j = 0, step = 0;
for (step = 1; step < N; step <<= 1);
for (; step > 0; step >>= 1)
{
if (i + step <= N && val >= aib[i+step])
{
i += step;
val -= aib[i];
if (!val)
return i;
}
}
return -1;
}
int sum(int pos)
{
int s = 0;
for (; pos>0; pos -= (pos & -pos))
{
s += aib[pos];
}
return s;
}
int main()
{
int i=0, a=0, b=0, op=0;
FILE *fin = fopen("aib.in", "rt"), *fout = fopen("aib.out", "wt");
fscanf(fin, "%d %d", &N, &M);
for (i=0; i<N; i++)
{
fscanf(fin, "%d", &a);
update(i+1, a);
}
for (i=0; i<M; i++)
{
fscanf(fin, "%d", &op);
if (op < 2)
{
fscanf(fin, "%d %d", &a, &b);
if (op == 0)
update(a, b);
else
{
int final_sum = sum(b) - sum(a-1);
fprintf(fout, "%d\n", final_sum);
}
}
else
{
fscanf(fin, "%d", &a);
// you come on just like special k
int SPECIAL_K = search(a);
fprintf(fout, "%d\n", SPECIAL_K);
}
}
fclose(fin), fclose(fout);
return 0;
}