Pagini recente » Cod sursa (job #2976961) | Cod sursa (job #3270030) | Cod sursa (job #447325) | Cod sursa (job #1322937) | Cod sursa (job #1912230)
#include <stdio.h>
using namespace std;
#define zeros(x) ( (x ^ (x - 1)) & x )
FILE *fin = fopen("aib.in", "r");
FILE *fout = fopen("aib.out", "w");
int n;
unsigned int aib[100001];
void update(int pos, unsigned int add)
{
int i;
for(i=pos;i<=n;i+=zeros(i))
{
aib[i]+=add;
}
}
int sum(int pos)
{
int i;
unsigned int total = 0;
for(i=pos;i>=1;i-=zeros(i))
{
total += aib[i];
}
return total;
}
int binar(int a)
{
int start = 0;
int adaug = 1<<16;
while(adaug>0)
{
if(start+adaug<=n && aib[start+adaug]<=a)
{
start+=adaug;
a-=aib[start];
}
adaug/=2;
}
if(a!=0) return -1;
return start;
}
int main()
{
int m,op;
fscanf(fin, "%d%d", &n, &m);
for(int i=1;i<=n;i++)
{
int x;
fscanf(fin, "%d", &x);
update(i,x);
}
for(int i=1;i<=m;i++)
{
fscanf(fin, "%d", &op);
if(op==0)
{
int x,y;
fscanf(fin, "%d%d", &x, &y);
update(x,y);
}
if(op==1)
{
int x,y;
fscanf(fin, "%d%d", &x,&y);
fprintf(fout, "%d\n", sum(y) - sum(x-1));
}
if(op==2)
{
int x;
fscanf(fin, "%d", &x);
fprintf(fout, "%d\n", binar(x));
}
}
}