-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinsearch.cpp
More file actions
68 lines (61 loc) · 1.29 KB
/
binsearch.cpp
File metadata and controls
68 lines (61 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// This code was created with the support of Sergo.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
using namespace std;
void file() {
freopen("binsearch.in", "r", stdin);
freopen("binsearch.out", "w", stdout);
}
// void ...
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
file();
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
a.push_back(1e9 + 1);
int t;
cin >> t;
while (t--) {
int x;
cin >> x;
int l = -1, r = (int)a.size() + 1;
while (r - l > 1) {
int m = (r + l) / 2;
if (a[m] >= x) {
r = m;
} else {
l = m;
}
}
if (a[r] != x) {
cout << "-1 -1" << endl;
continue;
}
cout << r + 1 << " ";
x++;
l = -1, r = (int)a.size() + 1;
while (r - l > 1) {
int m = (r + l) / 2;
if (a[m] >= x) {
r = m;
} else {
l = m;
}
}
cout << l + 1 << endl;
}
}