This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<int> v(m), v2;
for(int &x : v) cin >> x;
sort(v.begin(), v.end());
auto D = [&](int x, int y) { return min(max(x, y) - min(x, y), 2 * n - max(x, y) + min(x, y)); };
while(q--) {
int x, y;
cin >> x >> y;
int ans = D(x, y);
ans = min(ans, D(x, v[0]) + D(v[0]+n, y) + 1);
ans = min(ans, D(x, v.back()+n) + D(v.back(), y) + 1);
int p = lower_bound(v.begin(), v.end(), x) - v.begin();
if(p != m) ans = min(ans, D(x, v[p]) + D(v[p]+n, y) + 1);
if(p > 0) ans = min(ans, D(x, v[p-1]) + D(v[p-1]+n, y) + 1);
p = lower_bound(v.begin(), v.end(), x-n) - v.begin();
if(p != m) ans = min(ans, D(x, v[p]+n) + D(v[p], y) + 1);
if(p > 0) ans = min(ans, D(x, v[p-1]+n) + D(v[p-1], y) + 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... |