#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 c1 = max(x, y) - min(x, y);
	int c2 = 2 * n - c1;
	int z = min(c1, c2);
	return z;
}
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);
		auto tt = lower_bound(v.begin(), v.end(), x) ;
		if (tt != v.begin()) {
			tt--;
			int e;
			if(*tt < n) e= *tt+n;
			else e= *tt-n;
			ans = min({ ans, f(x, y), f(x, *tt) + f(e, y)+1 } );
		}
		if (t != INT_MAX) {
			int e;
			if(t < n) e= t+n;
			else e= t-n;
			ans = min( { ans, f(x, y), f(x, t) + f(e, y)+1});
		}
		swap(x,y);
		
		
		cout << ans << endl;
//		cout << min(ans1, 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... |