Pagini recente » Cod sursa (job #240128) | Cod sursa (job #1189200) | Cod sursa (job #146821) | Cod sursa (job #954326) | Cod sursa (job #340461)
Cod sursa(job #340461)
#include <fstream>
#include <map>
#include <utility>
using namespace std;
ifstream in;
ofstream out;
map< long, pair< long, long > >v;
map< long, pair< long, long > >::const_iterator ibegin,iend;
pair< map< long, pair< long, long > >::const_iterator, map< long, pair< long, long > >::const_iterator > s;
int main()
{long i,N,number,q,x;
in.open("cautbin.in");
in>>N;
for( i=1; i<=N; ++i )
{
in>>number;
if( !v[number].first || v[number].first > i ) v[number].first=i;
if( !v[number].second || v[number].second < i ) v[number].second=i;
}
in>>N;
ibegin=v.begin(); //--ibegin;
iend=v.end(); //++iend;
out.open("cautbin.out");
while( N-- )
{
in>>q>>x;
s=v.equal_range(x);
if( s.second == s.first )
{
if( 0 == q ) out<<"-1";
else {
v[x].first=v[x].second=0;
s=v.equal_range(x);
if( 1 == q )
{
--s.first;
if( s.first != ibegin ) out<<s.first->second.second;
else out<<"Error";
}
else if( 2 == q )
{
if( s.second != iend ) out<<s.second->second.first;
else out<<"Error";
}
v.erase(x);
}
}
else {
if( q <= 1 ) out<<v[x].second;
else out<<v[x].first;
}
out<<'\n';
}
return 0;
}