Pagini recente » Rating Eric Paturan (Eric_Paturan) | Cod sursa (job #1116690) | Cod sursa (job #628511) | Cod sursa (job #692910) | Cod sursa (job #710584)
Cod sursa(job #710584)
#include <fstream>
#include <cstdlib>
#define N_MAX 100011
using namespace std;
int v[N_MAX];
inline int log2( int N )
{
#ifdef __GNUC__ || __GNUG__
return 8*sizeof(int) - __builtin_clz(N) - 1;
#else
int l=-1;
for( ; N; N>>=1, ++l );
return l;
#endif
}
void Update( int xIndex, const int& xValue, const int& N )
{
for( ; xIndex <= N; xIndex += xIndex & -xIndex )
v[xIndex]+=xValue;
}
int Sum( int xIndex )
{
int s=0;
for( ; xIndex > 0; xIndex -= xIndex & -xIndex )
s+=v[xIndex];
return s;
}
int Search( int S, const int& N )
{
int index, tIndex, bitMask=1<<log2(N);
for( index=0; bitMask; bitMask >>= 1 )
{
tIndex=index+bitMask;
if( tIndex <= N )
{
if( S == v[tIndex] )
return tIndex;
if( S > v[tIndex] )
{
S-=v[tIndex];
index=tIndex;
}
}
}
return -1;
}
int main()
{
int N, M, i, x, op, a, b;
ifstream in( "aib.in" );
ofstream out( "aib.out" );
in>>N>>M;
for( i=1; i <= N; ++i )
{
in>>x;
Update( i, x, N );
}
for( ; M; --M )
{
in>>op>>a;
if( 0 == op )
{
in>>b;
Update( a, b, N );
}
else if( 1 == op )
{
in>>b;
out<<( Sum(b) - Sum(a-1) )<<'\n';
}
else out<<Search(a, N)<<'\n';
}
return EXIT_SUCCESS;
}