#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 500007;
const int INF = LLONG_MAX;
int N;
int distance(int x, int y) {
int dist = abs(x-y);
dist = min(dist, 2*N-dist);
return dist;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
int M,Q;
cin >> N >> M >> Q;
int best_friend[M]; //other best_friend is +N
int left_bridge[2*N];
int right_bridge[2*N];
fill(left_bridge,left_bridge+2*N,INF);
fill(right_bridge,right_bridge+2*N,INF);
for (int i=0;i<M;i++) {
cin >> best_friend[i];
left_bridge[best_friend[i]]=best_friend[i];
right_bridge[best_friend[i]]=best_friend[i];
left_bridge[best_friend[i]+N] = best_friend[i]+N;
right_bridge[best_friend[i]+N] = best_friend[i]+N;
}
int right_current = best_friend[M-1] + N;
for (int i=0;i<2*N;i++) {
if (right_bridge[i] != INF) {
right_current = right_bridge[i];
continue;
}
right_bridge[i] = right_current;
//cout << i << " " << right_bridge[i] << endl;
}
int left_current = best_friend[0];
for (int i=2*N-1;i>=0;i--) {
if (left_bridge[i] != INF) {
left_current = left_bridge[i];
continue;
}
left_bridge[i] = left_current;
//cout << i << " " << left_bridge[i] << endl;
}
int bridge_is_totally_useless_radius = (N+1)/2;
int bridge_is_useless_radius = N/2-1;
int x, y;
for (int i=0;i<Q;i++) {
cin >> x >> y;
if (1==9) {
cout << distance(x,y) << endl;
} else {
int shortest_path = distance(x,y);
int x_l = left_bridge[x];
int x_r = right_bridge[x];
int y_l = left_bridge[y];
int y_r = right_bridge[y];
if (x_l % N== y_l % N) {
shortest_path = min(shortest_path,distance(x_l,x) + distance(y_l,y) + 1);
}
if (x_r % N== y_l %N) {
shortest_path = min(shortest_path,distance(x_r,x) + distance(y_l,y) + 1);
}
if (x_l % N == y_r % N) {
shortest_path = min(shortest_path,distance(x_l,x) + distance(y_r,y) + 1);
}
if (x_r % N == y_r % N) {
shortest_path = min(shortest_path,distance(x_r,x) + distance(y_r,y) + 1);
}
cout << shortest_path << 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... |