Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <numeric>
- #include <cassert>
- #include <vector>
- typedef long long llong;
- const int MAXN = 100000 + 10;
- const llong INF = 1e18;
- const int INTINF = 1e9;
- int n, q;
- int a[MAXN];
- void solve()
- {
- std::sort(a + 1, a + 1 + n);
- for (int i = 1 ; i <= q ; ++i)
- {
- int value;
- std::cin >> value;
- int l = 0, r = n + 1, mid; //при интервал [start, end], l = start - 1, r = end + 1 за хващане на гранични случаи. В самото binary не се изчисляват неща за start - 1 и end + 1
- while (l < r - 1)
- {
- mid = (l + r) / 2;
- if (a[mid] <= value) l = mid;
- else r = mid;
- }
- if (l == 0) std::cout << -1 << '\n';
- else std::cout << a[l] << '\n';
- }
- }
- void input()
- {
- std::cin >> n >> q;
- for (int i = 1 ; i <= n ; ++i)
- {
- std::cin >> a[i];
- }
- }
- void fastIOI()
- {
- std::ios_base :: sync_with_stdio(0);
- std::cout.tie(nullptr);
- std::cin.tie(nullptr);
- }
- int main()
- {
- fastIOI();
- input();
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement