#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
int n, m, q;
cin >> n >> m >> q;
vector <int> friends;
for (int i = 0; i < m; i++){
int x;
cin >> x;
friends.push_back(x);
}
if (m != 0) friends.insert(friends.begin(), friends[friends.size() - 1] - n);
if (m != 0){
friends.push_back(friends[1] + n);
}
for (int i = 0; i < q; i++){
int x, y;
cin >> x >> y;
int ans = min(2 * n - abs(x - y), abs(x - y));
auto z = lower_bound(friends.begin(), friends.end(), x % n);
int left, right, lefts, rights;
if (*z == x % n){
left = x; right = x;
}
else{
left = *(z - 1), right = *(z);
}
if (x >= n){
lefts = (left + 2 * n) % (2 * n);
rights = right;
left += n;
right += n;
right %= 2 * n;
}
else{
lefts = left + n;
rights = (right + n) % (2 * n);
}
int q = min(abs(left - x), 2 * n - abs(left - x)) + min(abs(lefts - y), 2 * n - abs(lefts - y));
q = min(q, min(abs(right - x), 2 * n - abs(right - x)) + min(abs(rights - y), 2 * n - abs(rights - y)));
cout << min(ans, q + 1) <<endl;
}
}
# | 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... |