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 <iostream>
#include <set>
using namespace std;
int n;
set <int> k;
inline const int fastest(const int &x, const int &y) {
return min(abs(x - y), 2 * n - abs(x - y));
}
inline void calc(int &ans, int &x, int &y, const int &z) {
ans = min(ans, min(fastest(x, z) + fastest(z + n, y), fastest(y, z) + fastest(z + n, x)) + 1);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int m, q;
cin >> n >> m >> q;
while (m--) {
int x;
cin >> x;
k.insert(x);
}
while (q--) {
int x, y, ans;
cin >> x >> y;
ans = fastest(x, y);
if (y < x) swap(x, y);
if (x >= n) {
x -= n;
y = (y + n) % (n << 1);
}
set<int>::iterator z = k.lower_bound(x), w = k.lower_bound(y % n);
if (z == k.end()) z = k.begin();
if (w == k.end()) w = k.begin();
calc(ans, x, y, *z);
calc(ans, x, y, *w);
if (z == k.begin()) z = k.end();
if (w == k.begin()) w = k.end();
z--; w--;
calc(ans, x, y, *z);
calc(ans, x, y, *w);
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... |