#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int n, me, q;
cin>>n>>me>>q;
vector<int> bff;
int pivot = 0;
map<int, bool> m;
vector<int> store;
for(int i = 0; i < me; i++)
{
int a; cin>>a;
bff.push_back(a);
m[a] = 1;
m[a+n] = 1;
store.push_back(a);
store.push_back(a+n);
}
//st1
//int diff = pivot;
sort(store.begin(), store.end());
int new_target = 0;
while(q--)
{
int x, y; cin>>x>>y;
pivot = x;
if(y > x){
new_target = y-pivot;
}else if(y < x)
{
new_target = 2*n-(x-y);
}
//cout<<"new_target: "<<new_target<<endl;
int answer = 1e9;
//cout<<"new_target: "<<new_target<<" y: "<<y<<endl;
//last segment to decide and compare if it is better to not use any bridge
if(new_target < n)
{
answer = min(answer, new_target);
}
else if(new_target > n)
{
answer = min(answer, 2*n-new_target);
}else if(new_target == n)
{
answer = min(answer, n);
}
//cout<<endl;
//cout<<"ans: "<<answer<<endl;
auto mark = lower_bound(store.begin(), store.end(), x);
auto mark1 = mark-1;
if(mark == store.end())
{
mark1 = mark-1;
mark=store.begin();
}else if(mark == store.begin())
{
mark1 = store.end()-1;
}
for(int i = 0; i < 2; i++)
{
int hole;
if(i==0) hole = *mark;
else if(i==1) hole = *mark1;
//int hole = store[i];
int hole_new = hole-x;
if(hole_new<0) hole_new = 2*n+hole_new;
//cout<<"hole: "<<hole<<" "<<"new_hole: "<<hole_new<<endl;
//left side
if(hole_new > n)
{
int temp = 2*n-hole_new + 1 + abs((hole_new-n)-new_target);
//cout<<"temp: "<<temp<<endl;
answer = min(answer, temp);
//cout<<"answer: "<<answer<<endl;
}
//right side
else if(hole_new<n)
{
int temp = hole_new + 1 + abs((hole_new+n)-new_target);
//cout<<"temp: "<<temp<<endl;
answer = min(answer, temp);
}else if(hole_new == n && new_target == n)
{
int h = 1;
if(m[(n+x)%2*n] == 1) answer = min(answer, h);
else answer = min(answer, n);
}
}
cout<<answer<<'\n';
}
}
# | 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... |