Pagini recente » Cod sursa (job #415428) | Cod sursa (job #2949856) | Cod sursa (job #2922265) | Cod sursa (job #2176948) | Cod sursa (job #220979)
Cod sursa(job #220979)
#include <fstream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int c[100001],n,m,poz;
void update(int locat,int val)
{int loc=locat;
poz=0;
while(loc<=n)
{c[loc]+=val;
loc+=((loc^(loc-1))&loc); }}
int interogare(int st)
{int x=st; poz=0;
int sum1=0;
while(x>0)
{sum1+=c[x];
x-=((x^(x-1))&x);
}
return sum1;
}
int search(int val)
{
int j, step;
for ( step = 1; step < n; step <<= 1 );
for( j = 0; step; step >>= 1 )
{
if( j + step <= n)
{
if( val >= c[j+step] )
{
j += step, val -= c[j];
if ( !val ) return j;
}
}
}
return -1;
}
int main()
{ ifstream in("aib.in");
ofstream out("aib.out");
in>>n;in>>m; int k,x,y,aux;
for(int i=1;i<=n;i++)
{in>>aux;update(i,aux);}
for(;m;m--)
{in>>k;
if(k<2)
{in>>x>>y;
if(k==0) update(x,y);
else out<<(interogare(y)-interogare(x-1))<<'\n';}
else {in>>x;out<<search(x)<<'\n';}
}
return 0;
}