#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<ll> oppPass;
    FOR(j, M) {
        ll k;
        cin >> k;
        oppPass.push_back(k);
        oppPass.push_back(k+N);
    }
    sort(all(oppPass));
    
    FOR(i, Q) {
        ll x, y;
        cin >> x >> y;
        if (x > y) swap(x, y);
        ll cL, cR;
        auto p = lower_bound(all(oppPass), x);
        if (*p == x) cL = cR = *p;
        else {
            if (p == oppPass.begin() || p == oppPass.end()) {
                cL = oppPass[oppPass.size()-1];
            } else cL = *(p - 1);
            if (p == oppPass.end()) {
                cR = oppPass[0];
            } else cR = *p;
        }
        ll oppL = (cL+N) % (2*N), oppR = (cR+N) % (2*N);
        ll res = dist(x, y);
        res = min(res, 1 + dist(x, cL) + dist(oppL, y));
        res = min(res, 1 + dist(x, cR) + dist(oppR, y));
        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... |