Pagini recente » Cod sursa (job #738869) | Cod sursa (job #674469) | Cod sursa (job #3195930) | Unirea 2007, Clasament pentru clasele IX-X | Cod sursa (job #1706460)
# include <fstream>
# define MAX_N 1000001
int v[MAX_N], n;
//# define v ( v - 1 )
using namespace std;
class ifbuffer {
private:
char text[100000];
int size;
int pos;
bool isDigit[128];
FILE *file;
inline void refill( void ) {
pos = 0;
fread( text, 1, size, file );
}
inline char getc() {
char c = text[pos ++];
if ( pos == size )
refill();
return c;
}
inline int getnr() {
char c = getc();
int nr = 0;
while ( !isDigit[c] )
c = getc();
while ( isDigit[c] ) {
nr = nr * 10 + c - '0';
c = getc();
}
return nr;
}
public:
inline ifbuffer( char * path, int s = 100000 ) {
size = s;
file = fopen( path, "r" );
int i;
for ( i = 0; i < '0'; i ++ )
isDigit[i] = false;
for ( i = '0'; i <= '9'; i ++ )
isDigit[i] = true;
for ( i = '9' + 1; i < 128; i ++ )
isDigit[i] = false;
refill();
}
inline ifbuffer &operator>>( int &v ) {
v = getnr();
return *this;
}
inline ifbuffer &operator>>( char &v ) {
v = getc();
return *this;
}
inline void close() {
fclose( file );
}
};
class ofbuffer {
private:
char text[100000];
int size;
int pos;
bool isDigit[128];
FILE *file;
inline void clear( void ) {
pos = 0;
fwrite( text, 1, size, file );
}
inline void putc( char c) {
text[pos ++] = c;
if ( pos == size )
clear();
}
inline void putnr( int nr ) {
if ( nr < 0 ) {
putc( '-' );
nr =- nr;
}
int p10;
for ( p10 = 1; p10 * 10LL <= nr; p10 *= 10 );
for ( ; p10 > 0; p10 /= 10 )
putc( nr / p10 % 10 + '0' );
}
public:
inline ofbuffer( char * path, int s = 100000 ) {
size = s;
file = fopen( path, "w" );
int i;
for ( i = 0; i < '0'; i ++ )
isDigit[i] = false;
for ( i = '0'; i <= '9'; i ++ )
isDigit[i] = true;
for ( i = '9' + 1; i < 128; i ++ )
isDigit[i] = false;
}
inline ofbuffer &operator<<( int v ) {
fprintf( file, "%d", v );
return *this;
}
inline ofbuffer &operator<<( char v ) {
fputc( v, file );
return *this;
}
inline void close() {
fclose( file );
}
};
int cbin_equal( int val ) {
int pos, pas;
pos = 0;
for ( pas = 20; pas >= 0; pas -- ) {
if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] <= val )
pos += ( 1 << pas );
}
return ( pos != 0 && v[pos] == val ? pos : -1 );
}
int cbin_smaller( int val ) {
int pos, pas;
pos = 0;
for ( pas = 20; pas >= 0; pas -- )
if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] <= val )
pos += ( 1 << pas );
return pos;
}
int cbin_bigger( int val ) {
int pos, pas;
pos = 0;
for ( pas = 20; pas >= 0; pas -- )
if ( pos + ( 1 << pas ) <= n && v[pos + ( 1 << pas )] < val )
pos += ( 1 << pas );
return pos + 1;
}
int main() {
ifbuffer fin( "cautbin.in" );
ofbuffer fout( "cautbin.out" );
int i, m, x, t, p;
fin >> n;
for ( i = 1; i <= n; i ++ )
fin >> v[i];
fin >> m;
for ( i = 1; i <= m; i ++ ) {
fin >> t >> x;
switch( t ) {
case 0:
fout << cbin_equal( x );
break;
case 1:
fout << cbin_smaller( x );
break;
case 2:
fout << cbin_bigger( x );
break;
}
if ( i != m )
fout << '\n';
}
fin.close();
fout.close();
return 0;
}