Pagini recente » Cod sursa (job #1185477) | Cod sursa (job #1504869) | Cod sursa (job #768837) | Cod sursa (job #858550) | Cod sursa (job #794178)
Cod sursa(job #794178)
#include <fstream>
using namespace std;
const int Nmax = 100010;
int N,M;
int AIB[Nmax];
int Type,a,b;
inline int Bit( int x )
{
return ( x ^ (x-1) ) & x;
}
void Add(int a,int b)
{
for (int i=a;i<=N;i+=Bit(i))
AIB[i] += b;
}
int Query(int a)
{
int Val = 0;
for (int i=a;i;i-=Bit(i))
Val += AIB[i];
return Val;
}
int Sum(int a,int b)
{
return Query(b)-Query(a-1);
}
int Find(int Val)
{
int Step = 1;
for ( ; Step < N ; Step<<=1 );
for ( int Act=0 ; Step ; Step>>=1 )
if ( Act+Step <= N )
if ( Val >= AIB[Act+Step] )
{
Act+=Step; Val -= AIB[Act];
if ( !Val ) return Act;
}
return -1;
}
ifstream F("aib.in");
ofstream G("aib.out");
int main()
{
F>>N>>M;
for (int i=1;i<=N;++i)
{
F>>b;
Add( i , b );
}
while ( M -- )
{
F>>Type;
switch ( Type )
{
case 0:
{
F>>a>>b;
Add( a , b );
break;
}
case 1:
{
F>>a>>b;
G<<Sum( a , b )<<'\n';
break;
}
case 2:
{
F>>b;
G<<Find( b )<<'\n';
break;
}
}
}
}