#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb(a) push_back(a)
#define pp pop_back
#define mp(a, b) make_pair(a, b)
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m, q, k, x, y;
cin >> n >> m >> q;
set <int> s;
for (int i=0; i<m; i++) {cin >> k; s.insert(k);}
for (int i=0; i<q; i++) {
cin >> x >> y;
int a=x%n, b=y%n, ans=min(abs(x-y), 2*n-abs(x-y));
if (a>b) swap(a, b);
auto it=s.lower_bound(a), it2=s.begin();
ans=min(ans, min(abs(a-*it), n-abs(a-*it))+min(abs(b-*it), n-abs(b-*it))+1);
if (it==it2) {it=s.upper_bound(a+n); it--;}
else it--;
ans=min(ans, min(abs(a-*it), n-abs(a-*it))+min(abs(b-*it), n-abs(b-*it))+1);
cout << ans << "\n";
}
return 0;
}
# | 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... |