#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i,N) for(ll i = 0; i < N; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
ll N;
inline ll dist(ll x, ll y) {
    if (x > y) swap(x,y);
    return min(y-x, 2*N-(y-x));
}
signed main()  {
    cin.tie(0); ios::sync_with_stdio(false);
    ll M, Q;
    cin >> N >> M >> Q;
    vector<signed char> oppPass(2*N);
    FOR(j, M) {
        ll k;
        cin >> k;
        oppPass[k] = 1;
        oppPass[k+N] = 1;
    }
    vector<pair<ll,ll>> cL(2*N), cR(2*N); // closest person on either side (incl self) with opposite pass
    ll last = -1;
    for(ll i = 0; i < 2*N; i++) {
        if (oppPass[i]) {
            last = i;
        }
        cL[i] = {last, dist(last, i)};
    }
    for(ll i = 0; i < 2*N; i++) {
        if (oppPass[i]) {
            last = i;
        }
        cL[i] = {last, dist(last, i)};
    }
    last = -1;
    for(ll i = 2*N-1; i >= 0; i--) {
        if (oppPass[i]) {
            last = i;
        }
        cR[i] = {last, dist(last, i)};
    }
    for(ll i = 2*N-1; i >= 0; i--) {
        if (oppPass[i]) {
            last = i;
        }
        cR[i] = {last, dist(last, i)};
    }
    // FOR(i, 2*N) cout << cL[i].F << ' ' << cL[i].S << ' ' << cR[i].F << ' ' << cR[i].S << endl;
    FOR(i, Q) {
        ll x, y;
        cin >> x >> y;
        if (x > y) swap(x, y);
        ll res = dist(x, y);
        ll oppL = (cL[x].F + N) % (2*N), oppR = (cR[x].F + N) % (2*N);
        res = min(res, 1 + cL[x].S + dist(oppL, y));
        res = min(res, 1 + cR[x].S + dist(oppR, y));
        // cout << "c: " << cL[x].F << ' ' << cL[x].S << ' ' << cR[x].F << ' ' << cR[x].S << endl;
        // cout << "opp: " << oppL << ' ' << oppR << endl;
        // cout << "direct: " << dist(x,y) << endl;
        // cout << "left: " << dist(oppL, y) << endl;
        // cout << "right: " << dist(oppR, y) << endl;
        cout << res << endl;
    }
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |