-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe.cpp
85 lines (61 loc) · 1.42 KB
/
e.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <fstream>
#include <vector>
#include <utility>
int bin_search_l(const std::vector<int>& v, const int& x) {
if (v[0] > x || v[v.size() - 1] < x) {
return -1;
}
int left = -1;
int right = v.size();
while (right - left > 1) {
int mid = (left + right) / 2;
if (x > v[mid])
left = mid;
else
right = mid;
}
if (v[right] == x) {
return right + 1;
}
return -1;
}
int bin_search_r(const std::vector<int>& v, const int& x) {
if (v[0] > x || v[v.size() - 1] < x) {
return -1;
}
int left = -1;
int right = v.size();
while (right - left > 1) {
int mid = (left + right) / 2;
if (x >= v[mid])
left = mid;
else
right = mid;
}
if (v[right - 1] == x) {
return right;
}
return -1;
}
int main() {
std::ifstream fin("binsearch.in");
std::ofstream fout("binsearch.out");
int n;
fin >> n;
std::vector<int> a(n);
for (auto& e : a) {
fin >> e;
}
int m;
fin >> m;
std::vector<int> b(m);
for (auto& e : b) {
fin >> e;
}
for (const auto& search_element : b) {
auto p = std::make_pair(bin_search_l(a, search_element), bin_search_r(a, search_element));
fout << p.first << ' ' << p.second << std::endl;
}
fin.close();
fout.close();
}