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 int long long
#define ff first
#define ss second
#define pb push_back
const int N=3e5+5;
vector<int>ans;
int n,m,q;
int fun(int x, int y){
int h=abs(y-x);
return min(h,2*n-h);
}
signed main(){
cin>>n>>m>>q;
vector<int>v={-1,INT_MAX};
for(int i=1;i<=m;i++){
int a;
cin>>a;
v.pb(a);
v.pb(a+n);
}
sort(v.begin(),v.end());
while(q--){
int a,b;
cin>>a>>b;
auto it=upper_bound(v.begin(),v.end(),a);
int x=*it;
it--;
int y= *it;
int p=fun(a,b);
if(x!=INT_MAX){
int z;
if(x<n)z=n+x;
else z=x-n;
p=min(p,fun(a,x)+fun(b,z)+1);
}
if(y!=-1){
int z;
if(y<n)z=n+y;
else z=y-n;
p=min(p,fun(a,y)+fun(b,z)+1);
}
it--;
y=*it;
if(y!=-1){
int z;
if(y<n)z=n+y;
else z=y-n;
p=min(p,fun(a,y)+fun(b,z)+1);
}
ans.pb(p);
}
for(auto a:ans)cout<<a<<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... |