Pagini recente » Cod sursa (job #2518153) | Cod sursa (job #341362)
Cod sursa(job #341362)
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
ifstream in;
ofstream out;
vector<int> v;
vector<int>::const_iterator it;
pair< vector<int>::const_iterator, vector<int>::const_iterator > p;
int main()
{int N,x,q;
in.open("cautbin.in");
in>>N;
while( N-- )
{
in>>x;
v.push_back(x);
}
in>>N;
out.open("cautbin.out");
while( N-- )
{
in>>q>>x;
p=equal_range( v.begin(), v.end(), x );
if( p.first != p.second )
{
if( q <= 1 ) out<<( p.second-v.begin() );
else out<<( p.first-v.begin()+1 );
}
else {
if( 0 == q ) out<<"-1";
else if( 1 == q )
{
it=lower_bound( v.begin(), v.end(), x );
out<<( it-v.begin() );
}
else if( 2 == q )
{
it=upper_bound( v.begin(), v.end(), x );
out<<( it-v.begin()+1 );
}
}
out<<'\n';
}
return 0;
}