Pagini recente » Cod sursa (job #679875) | Cod sursa (job #1482332) | Cod sursa (job #2635170) | Cod sursa (job #1086098) | Cod sursa (job #880034)
Cod sursa(job #880034)
#include<fstream>
using namespace std;
ifstream f("cautbin.in");
ofstream g("cautbin.out");
int main(){
int a,b,o,w,m,n,i,flg,h[100001],x=-1,y=-1,z;
f>>n>>m;
for(i=1;i<=n;i++)
f>>h[i];
for(i=1;i<=m;i++){
f>>o>>w;
x=-1; y=-1; z=1+n;
if(o%3==0){
a=1;b=n;
flg=(a+b)/2;
while(a<b || a==b){
if(h[flg]==w){
if(flg>x)x=flg;
a=flg+1;}
else{
if(h[flg]<w)a=flg+1;
else b=flg-1;}
flg=(a+b)/2;}
g<<x<<'\n';
}
else{
if(o%3==1){
a=1; b=n;
flg=(a+b)/2;
while(a<=b) {
if(h[flg]<=w) { if(flg>y)y=flg; a=flg+1;}
else {if(h[flg]<w)a=flg+1;else b=flg-1;
}
flg=(a+b)/2;}
g<<y<<'\n';}
else{
a=1; b=n;
flg=(a+b)/2;
while(a<=b){
if(h[flg]>=w){if(flg<z)z=flg;b=flg-1;}
else a=flg+1;
flg=(a+b)/2;
}
g<<z<<'\n';}
}
}
f.close();
g.close();
return 0;
}