#include <bits/stdc++.h>
#include <fstream>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define pb push_back
#define INF 200000000
#define fi first
#define se second
//#define cin fin
//#define cout fout
using namespace std;
//double const EPS = 1e-14;
typedef long long ll;
//const ll P = 10007;
//const ll mod = 1e9 + 7;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // find_by_order, order_of_key
const int Max = 5e5 + 5;
ll n, m, q;
vector<ll> vv;
ll fr[Max];
ll fin(ll x, ll y) {
ll sum = min(abs(x-y),(n*2ll)-abs(x-y));
return sum;
}
ll sol(ll id, ll x, ll y) {
ll a = vv[id], b;
if(a >= n) b= vv[id]-n;
else b = vv[id]+n;
ll dis1 = fin(x,a)+fin(y,b)+1;
ll dis2 = fin(x,b)+fin(y,a)+1;
ll best = n + 10;
best = min(best,min(dis1,dis2));
return best;
}
int main()
{
ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
cin >> n >> m >> q;
for(int i = 0; i < m; i++) {cin >> fr[i]; vv.push_back(fr[i]); vv.push_back(fr[i]+n);}
sort(vv.begin(),vv.end());
ll siz = m*2;
while(q--) {
ll x, y; cin >> x >> y;
ll ans = fin(x,y);
ll id2 = siz-1, id1 = lower_bound(vv.begin(),vv.end(),x)-vv.begin();
if(id1 != 0) id2 = id1-1;
//cout << id1 << ' ' << id2 << ' ' << id3 << 'a' << endl;
if(id1 < siz) ans = min(ans,sol(id1,x,y));
ans = min(ans,sol(id2,x,y));
cout << ans << endl;
}
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... |