This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define cmin(a, b) a=min(a, b)
#define cmax(a, b) a=max(a, b)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define int long long
void solve();
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
return 0;
}
int n, m, q;
vector<int> a;
int dist(int x, int y) {
if (x>y) swap(x, y);
return min(y-x, 2*n-(y-x));
}
void solve() {
cin >> n >> m >> q; a.clear(), a.resize(m);
for (auto &u: a) cin >> u;
set<int> st; for (auto &u: a) st.insert(u), st.insert(u+n);
while (q--) {
int x, y; cin >> x >> y;
if (x>y) swap(x, y);
if (x>=n) x-=n, y-=n;
int ans=dist(x, y);
auto lb=st.lower_bound(x); if (lb==st.end()) lb=st.begin();
cmin(ans, dist(*lb, x)+dist((*lb+n)%(2*n), y)+1);
lb=st.lower_bound(y); if (lb==st.end()) lb=st.begin();
cmin(ans, dist(*lb, y)+dist((*lb+n)%(2*n), x)+1);
lb=st.lower_bound(x); if (lb==st.begin()) lb=st.end();
lb--;
cmin(ans, dist(*lb, x)+dist((*lb+n)%(2*n), y)+1);
lb=st.lower_bound(y); if (lb==st.begin()) lb=st.end();
lb--;
cmin(ans, dist(*lb, y)+dist((*lb+n)%(2*n), x)+1);
cout << ans << endl;
}
}
# | 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... |