#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));
}
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 cur = 0, nxt = 0;
if (pos == 0){
if (dist(x, k[pos]) < dist(x, k.back())) cur = k[pos];
else cur = k.back();
}
else if (pos == 2*m){
pos--;
if (dist(x, k[pos]) < dist(x, k.front())) cur = k[pos];
else cur = k.front();
}
else{
if (dist(x, k[pos]) < dist(x, k[pos-1])) cur = k[pos];
else cur = k[pos-1];
}
if (cur >= n) nxt = cur-n;
else nxt = cur+n;
//cout << "pos " << pos << " cur " << cur << " " << "nxt " << nxt << " ";
ll fi = dist(x, cur), se = dist(y, nxt);
ans = min(ans1, (fi+se+1));
//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... |