Pagini recente » 2SAT | Cod sursa (job #1553316)
#include <fstream>
using namespace std;
const int NMAX= 100000;
ifstream in( "cautbin.in" );
ofstream out( "cautbin.out" );
int v[NMAX+1];
int main( )
{
int N, N2;
in >> N;
for( N2=1; N2<=N; N2*=2 ){
}
N2/=2;
for( int i= 1; i<=N; ++i )
{
in >> v[i];
}
int Q;
in >> Q;
for( int q= 1; q<=Q; ++q )
{
int a, x;
in >> a >> x;
if( a==1 )
{
int sol= 0;
for( int step= N2; step>0; step/=2 )
{
if( sol+step <= N && v[sol+step]<=x ) sol+= step;
}
out << sol << '\n';
}
if( a==0 )
{
int sol= 0;
for( int step= N2; step>0; step/=2 )
{
if( sol+step <= N && v[sol+step]<=x ) sol+= step;
}
if( v[sol]==x ) out << sol << '\n';
else out << "-1" << '\n';
}
if( a==2 )
{
int sol= N+1;
for( int step= N2; step>0; step/=2 )
{
if( sol-step >= 1 && v[sol-step]>=x ) sol-= step;
}
out << sol << '\n';
}
}
return 0;
}