#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
#define ull unsigned long long
#define llll __int128
#define ld long double
#define pll pair<ll, ll>
#define pii pair<int, int>
#define coutpair(p) cout << p.first << " " << p.second << "|"
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll n, m, q;
ll dist(ll x, ll y){ //minimum distance
return min(abs(x-y), 2*n-abs(x-y));
}
ll frd(ll x){
if (x >= n) x -= n;
else x += n;
return x;
}
ll mndist(ll x, ll y, ll pos1, ll pos2){
return min(dist(x, pos1)+dist(y, frd(pos1)), dist(x, pos2)+dist(y, frd(pos2)))+1;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
vector<ll> k;
for (int i=0; i<m; i++){
ll c; cin >> c;
k.push_back(c);
k.push_back(c+n);
}
sort(k.begin(), k.end());
for (int i=0; i<q; i++){
ll x, y; cin >> x >> y;
ll ans = 0;
ll pos = upper_bound(k.begin(), k.end(), x)-k.begin(); //closest person with a best friend
ll ans1 = dist(x, y); //only passing to those adjacent for the entire process
ll ans2 = 0;
if (pos == 0) ans2 = mndist(x, y, k[pos], k.back());
else if (pos == 2*m){
pos--;
ans2 = mndist(x, y, k[pos], k.front());
}
else ans2 = mndist(x, y, k[pos], k[pos-1]);
//cout << "pos " << pos << " ";
ans = min(ans1, ans2);
//cout << "ans1 " << ans1 << " first " << fi << " second " << se << "\n";
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... |