Cod sursa(job #915510)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int n;
fin >> n;
int a[n+1];
for (int i=1; i<=n; ++i) {
fin >> a[i];
}
int msb=n;//most significant bit
msb |= msb>>1;
msb |= msb>>2;
msb |= msb>>4;
msb |= msb>>8;
msb |= msb>>16;
msb = msb^(msb>>1);
int m;
fin >> m;
for (int i=0, op, val; i<m; ++i) {
fin >> op >> val;
int idx=0;
if (op==2) {
for (int stp=msb; stp!=0; stp>>=1) {
idx+=stp;
if (!(idx <= n && a[idx] < val ))
idx-=stp;
}
++idx;
}
else {
for (int stp=msb; stp!=0; stp>>=1) {
idx+=stp;
if (!(idx <= n && a[idx] <= val ))
idx-=stp;
}
}
if (op==0) fout << (a[idx]==val ?idx :-1);
else fout << idx;
fout << '\n';
}
return 0;
}