Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 days ago
651 B
1
Indexable
Never
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    set<int> a;
    for (int i=0; i<n; i++) {
        int x;
        cin >> x;
        a.insert(x);
    }
    
    while (m--) {
        int x;
        cin >> x;
        auto rb = a.lower_bound(x);
        if (rb == a.end()) cout << *prev(rb) << endl;
        else if (rb != a.begin()) {
            
            auto lb = prev(rb);
            cout << ((*rb - x < x - *lb) ? *rb : *lb) << endl;
        }
        else cout << *rb << endl;
    }
}
Leave a Comment