#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, q;
std::cin >> n >> m >> q;
std::vector <int> arr;
for(int i=0; i<m; i++){
int x;
std::cin >> x;
arr.push_back(x);
arr.push_back(x+n);
arr.push_back(x+2*n);
arr.push_back(x+3*n);
}
sort(arr.begin(), arr.end());
for(int i=0; i<q; i++){
int a, b;
std::cin >> a >> b;
if(a>b) swap(a, b);
int ans=min(b-a, 2*n+a-b);
//else if theres like smth between both of these that can +n then try it ig
auto mid=lower_bound(arr.begin(), arr.end(), a);//check if <b ltr
int tele=*mid;
ans=min(ans, abs(tele+n-b)+abs(tele-a)+1);
auto aft=upper_bound(arr.begin(), arr.end(), a+2*n);
aft--;
tele=*aft;
int opp=(tele+n)%(2*n);
ans=min(ans, abs(a+2*n-tele)+abs(b-opp)+1);
std::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... |