#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
vector<int> tel;
for (int i = 0; i < m; ++i) {
int a;
cin >> a;
tel.push_back(a);
tel.push_back(a + n);
}
sort(begin(tel), end(tel));
auto calc = [&](int x, int y) -> int {
if (x >= y) swap(x, y);
return min(abs(y - x), abs(2 * n + x - y));
};
auto nxt = [&](int x) -> int {
return (x + n) % (2 * n);
};
while (q--) {
int a, b;
cin >> a >> b;
int res = calc(a, b);
{
int l = lower_bound(begin(tel), end(tel), a) - begin(tel);
if (l == 0) {
if (tel[0] == a) l = a;
else l = tel.back();
} else {
if (l < (int)tel.size()) l -= (tel[l] != a);
else l -= 1;
l = tel[l];
}
int now = calc(a, l);
int na = l;
res = min(res, now + calc(na, b));
na = nxt(na);
res = min(res, now + 1 + calc(na, b));
}
{
int r = lower_bound(begin(tel), end(tel), a) - begin(tel);
if (r == (int)tel.size()) {
r = tel[0];
} else {
r = tel[r];
}
int now = calc(a, r);
int na = r;
res = min(res, now + calc(na, b));
na = nxt(na);
res = min(res, now + 1 + calc(na, b));
}
cout << res << "\n";
}
}
# | 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... |