#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
#define int long long
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
int n, m, t;
int f(int x, int y) {
	int ans = max(x, y) - min(x, y);
	ans =min(ans,  n - x + y + 1);
	int x1 = x;
	int x2 = x + n;
	int x3 = x + n + n;
	int y1 = y;
	int y2 = y + n;
	int y3 = y + n + n;
	ans =min(ans,  abs(x1 - y2) + 1);
	ans =min(ans,  abs(x1 - y3) + 1);
	ans =min(ans,  abs(x2 - y1) + 1);
	ans =min(ans,  abs(x2 - y3) + 1);
	ans =min(ans,  abs(x3 - y1));
	ans =min(ans,  abs(x3 - y2) + 1);
	return ans;
}
signed main() {
	cin >> n >> m >> t;
	vector<int> v(m);
	for (int i = 0; i < m; i++) {
		cin >> v[i];
	}
	v.push_back(INT_MAX);
	sort(v.begin(), v.end());
	while (t--) {
		int ans = 1e9;
		int x, y;	cin >> x >> y;
		int t = *lower_bound(v.begin(), v.end(), x);
		if(y < x) {
			swap(x, y);
		}
		ans = min(f(x, y), f(x, t) + f(t, y) + 1);
//		cout << " * ";
		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... |