Pagini recente » Cod sursa (job #81787) | Cod sursa (job #841784)
Cod sursa(job #841784)
#include <fstream>
#include <cmath>
using namespace std;
ifstream f("aib.in");
ofstream g("aib.out");
int n,m,v[100005],i,a,b,c,C,S;
void adauga(int poz,int val)
{
C = 0;
while ( poz <= n )
{
v[poz] += val;
while ( !(poz & (1<<C)) ) C++;
poz += (1<<C);
C += 1;
}
}
int suma(int poz)
{
C = 0, S = 0;
while ( poz > 0 )
{
S += v[poz];
while ( !(poz & (1<<C)) ) C++;
poz -= (1<<C);
C += 1;
}
return S;
}
int cauta(int val)
{
int i, step;
for ( step = 1; step < n; step <<= 1 );
for( i = 0; step; step >>= 1 )
{
if( i + step <= n)
{
if( val >= v[i+step] )
{
i += step, val -= v[i];
if ( !val ) return i;
}
}
}
return -1;
}
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
f>>a,adauga(i,a);
while(m)
{
f>>a;
if(a==0)
f>>b>>c,adauga(b,c);
else
if(a==1)
f>>b>>c,g<<suma(c)-suma(b-1)<<'\n';
else
f>>b,g<<cauta(b)<<'\n';
m--;
}
return 0;
}