Cod sursa(job #201176)

Utilizator tvladTataranu Vlad tvlad Data 29 iulie 2008 15:23:23
Problema Cautare binara Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <cstdio>

const int N = 100000;

int n,m;
int a[N];

int main() {
	freopen("cautbin.in","rt",stdin);
	freopen("cautbin.out","wt",stdout);
	scanf("%d",&n);
	for (int i = 0; i < n; ++i) scanf("%d",&a[i]);
	scanf("%d",&m);
	for (int i = 0, c, x; i < m; ++i) {
		scanf("%d %d",&c,&x);
		if (c == 0 || c == 1) {
			int p = 0, step = 1 << 16;
			for (; step; step >>= 1)
				if (p + step < n && a[p+step] <= x)
					p += step;
			if (c == 0 && a[p] != x)
				printf("-1\n"); else
				printf("%d\n",p+1);
		} else {
			int p = 0, step = 1 << 16;
			for (; step; step >>= 1)
				if (p + step < n && a[p+step] < x)
					p += step;
			printf("%d\n",p+2);
		}
	}
}