#include <bits/stdc++.h>
#define pb push_back
#define SS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define int long long
#define all(v) v.begin(),v.end()
using namespace std;
const int N = 1e4, inf = 1e9 + 1, mod = 998244353;
vector<int> g[N];
void solve(){
int n, m, q;
cin >> n >> m >> q;
set<int> st;
for(int i = 1; i <= m; i++){
int x;
cin >> x;
st.insert(x);
st.insert(x + n);
}
n += n;
auto dist = [&](int x, int y){
return min((x - y + n) % n, (y - x + n) % n);
};
while(q--){
int s, t;
cin >> s >> t;
int ans = dist(s, t);
auto it = st.lower_bound(s);
if(it != st.end()) ans = min(ans, dist(s, *it) + dist(*it + n / 2, t) + 1);
if(it != st.begin()){
it--;
ans = min(ans, dist(s, *it) + dist(*it + n / 2, t) + 1);
}
cout << ans << '\n';
}
}
signed main(){
SS
// freopen("trains.in", "r", stdin);
// freopen("trains.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--){
solve();
}
}
| # | 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... |