Binary Search - E

public
yeskendir.sultanov Mar 31, 2024 Never 111
Clone
C++ Ebinsearch.cpp 34 lines (29 loc) | 754 Bytes
1
#include <bits/stdc++.h>
2
3
using namespace std;
4
5
int main() {
6
std::ios_base::sync_with_stdio(false);
7
cin.tie(0);
8
cout.tie(0);
9
int n, k;
10
cin >> n >> k;
11
int a[n];
12
for (int i = 0; i < n; ++i) {
13
cin >> a[i];
14
}
15
16
for (int i = 0; i < k; ++i) {
17
int x;
18
cin >> x;
19
// a[j - 1] < x <= a[j]
20
int j = (lower_bound(a + 0, a + n, x) - a);
21
22
if (j == n || x < a[j]) {
23
cout << 0 << endl;
24
} else {
25
int L = j + 1;
26
// a[j - 1] <= x < a[j]
27
j = (upper_bound(a + 0, a + n, x) - a) - 1;
28
int R = j + 1;
29
cout << L << " " << R << endl;
30
}
31
}
32
33
return 0;
34
}
35