Pagini recente » Cod sursa (job #660925) | Cod sursa (job #3032479) | Cod sursa (job #963802) | Cod sursa (job #2058556) | Cod sursa (job #2110547)
#include<fstream>
using namespace std;
ifstream fin("cautbin.in");
ofstream fout("cautbin.out");
int a[100001], n, nr, m, p;
int zero(int x) {
int st, dr, mij;
dr = n+1;
st = 0;
while (st != dr - 1) {
mij = (st + dr) / 2;
if (a[mij] == x)
{
while (a[mij + 1] == a[mij])
mij++;
return mij;
}
if (a[mij] < x) st = mij;
if (a[mij] > x) dr = mij;
}
return -1;
}
int unu(int x) {
int st, dr, mij;
dr = n + 1;
st = 0;
while (st != dr-1) {
mij = (st + dr) / 2;
if (a[mij] == x)
{
while (a[mij] == a[mij + 1])
mij++;
return mij;
}
if (a[mij] < x) st = mij;
if (a[mij] > x) dr = mij;
}
return st;
}
int doi(int x) {
int st, dr, mij;
dr = n + 1;
st = 0;
while (st != dr - 1) {
mij = (st + dr) / 2;
if (a[mij] == x)
{
while (a[mij] == a[mij - 1])
mij--;
return mij;
}
if (a[mij] < x) st = mij;
if (a[mij] > x) dr = mij;
}
return dr;
}
int main() {
fin >> n;
for (int i = 1;i <= n;i++) {
fin >> a[i];
}
fin >> m;
for (int i = 1;i <= m;i++) {
fin >> p >> nr;
if (p == 0) fout << zero(nr)<<'\n';
if (p == 1) fout << unu(nr)<<'\n';
if (p == 2) fout << doi(nr)<<'\n';
}
}