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;
long long a,b,c,t,n,x,y,m,q;
set<long long>s;
long long dist(long long d,long long e){
d=d%(2*n);
e=e%(2*n);
//cout<<d<<" "<<e<<" ";
return min(abs(d-e),abs(2*n-abs(e-d)));
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>q;
for(long long i=0;i<m;i++){
cin>>a;
s.insert(a);
s.insert(a+n);
}
for(long long i=0;i<q;i++){
// cout<<s.size()<<" ";
cin>>x>>y;
long long pom1,pom2;
if(s.lower_bound(x)!=s.end() && s.lower_bound(x)!=s.begin()){//x,x-1
pom1=*s.lower_bound(x);
if(pom1==x)
pom2=x;
else
pom2=*--s.lower_bound(x);
//cout<<x;
}
else{
pom1=*s.begin();
if(pom1==x)
pom2=x;
else
pom2=*--s.end();
}
cout<<min(min(dist(x,y),1+dist(pom1+n,y)+dist(x,pom1)),1+dist(pom2+n,y)+dist(x,pom2))<<"\n";
//cout<<pom1<<" "<<pom2<<"\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... |