#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
set<int> bt;
for (int i = 0; i < m; i++) {
int b;
cin >> b;
bt.insert(b);
bt.insert(b + n);
}
while (q--) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
auto it = bt.lower_bound(x);
auto prev_it = it;
if (it == bt.begin()) {
prev_it = bt.end();
prev_it--;
} else {
prev_it--;
}
int ans = y - x;
ans = min(ans, 2 * n - y + x);
if (*it <= n && *prev_it < n) {
int c1 = (*it - x) + 1 + abs((*it + n) - y);
int c2 = (x - *prev_it) + 1 + abs((*prev_it + n) - y);
ans = min(ans, min(c1, c2));
} else if (*it <= n && (*prev_it >= n && *prev_it < 2 * n)) {
int c1 = (*it - x) + 1 + abs((*it + n) - y);
int c2 = (x + (2 * n - *prev_it)) + 1 + abs((*prev_it - n) - y);
ans = min(ans, min(c1, c2));
} else if ((*it > n && *it < 2 * n) && (*prev_it >= n && *prev_it < 2 * n)) {
int c1 = (*it - x) + 1 + abs((*it - n) - y);
int c2 = (x - *prev_it) + 1 + abs((*prev_it - n) - y);
ans = min(ans, min(c1, c2));
} else if ((*it > n && *it < 2 * n) && (*prev_it < n)) {
int c1 = (*it - x) + 1 + abs((*it - n) - y);
int c2 = (x - *prev_it) + 1 + abs((*prev_it + n) - y);
ans = min(ans, min(c1, c2));
}
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... |