#include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, m, q;
cin>>n>>m>>q;
vector<int> friends;
for(int i=0; i<m; i++){
int k;
cin>>k;
friends.push_back(k);
friends.push_back(n+k);
}
sort(friends.begin(), friends.end());
auto getDist = [&](int x, int y){
if(x > y){
swap(x, y);
}
return min(y-x, 2*n-y+x);
};
auto getOpposite = [&](int x){
if(x < n){
return x + n;
}else{
return x - n;
}
};
for(int i=0; i<q; i++){
int x, y;
cin>>x>>y;
x--;
y--;
if(x > y) swap(x, y);
int ans = getDist(x, y);
auto ptr = lower_bound(friends.begin(), friends.end(), x);
int friendRight = (ptr == friends.end()) ? *friends.begin() : *ptr;
ans = min(ans, getDist(x, friendRight) + getDist(getOpposite(friendRight), y) + 1);
int friendLeft = (ptr == friends.begin()) ? *friends.rend() : *(ptr-1);
ans = min(ans, getDist(x, friendLeft) + getDist(getOpposite(friendLeft), y) + 1);
cout<<ans<<"\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... |