Submission #1330137

#TimeUsernameProblemLanguageResultExecution timeMemory
1330137AMel0nCircle Passing (EGOI24_circlepassing)C++20
100 / 100
83 ms8744 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + 7;


signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    int N, M, Q;
    cin >> N >> M >> Q;
    vector<ll> phriend;
    for(int i = 0; i < M; i++) {
        int k; cin >> k;
        phriend.push_back(k), phriend.push_back(k+N);
    }
    sort(phriend.begin(), phriend.end());
    function<int(int, int)> dist = [&](int a, int b) {
        if (a > b) swap(a, b);
        return min(b-a, 2*N-b+a);
    };
    for(int i = 0; i < Q; i++) {
        int x, y;
        cin >> x >> y;
        if (x > y) swap(x, y);
        auto it = lower_bound(phriend.begin(), phriend.end(), x);
        int Lx, Rx;
        if (it != phriend.end()) Rx = *it;
        else Rx = *(phriend.begin());
        if (it - phriend.begin()) Lx = *(--it);
        else Lx = *(--phriend.end());
        it = lower_bound(phriend.begin(), phriend.end(), y);
        int Ly, Ry;
        if (it != phriend.end()) Ry = *it;
        else Ry = *(phriend.begin());
        if (it - phriend.begin()) Ly = *(--it);
        else Ly = *(--phriend.end());
        // cout << Lx << ' ' << Rx << ' ' << Ly << ' ' << Ry << endl;
        cout << min({dist(x,y), 1+dist(x,Lx)+dist((Lx+N)%(2*N),y), 1+dist(x,Rx)+dist((Rx+N)%(2*N),y), 1+dist(y,Ly)+dist((Ly+N)%(2*N),x), 1+dist(y,Ry)+dist((Ry+N)%(2*N),x)}) << '\n';
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...