이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n,M,Q;
set <int> s;
int dist(int x,int y)
{
    return min(abs(x-y),n*2-abs(x-y));
}
int shc(int x, int y)
{
    int vst,vdr;
    if(upper_bound(s.begin(),s.end(),x)==s.end())
        vdr=*s.begin();
    else
        vdr=*upper_bound(s.begin(),s.end(),x);
    
    if(lower_bound(s.begin(),s.end(),x)!=s.end())
    {
        if(*lower_bound(s.begin(),s.end(),x)==x)
            vst=x;
        else
        {   
            if(lower_bound(s.begin(),s.end(),x)==s.begin())
                vst=*s.rbegin();
            else
                vst=*--upper_bound(s.begin(),s.end(),x);
        }
    }
    else
    {
        auto it=lower_bound(s.begin(),s.end(),x);
        --it;
        vst=*lower_bound(s.begin(),s.end(),x);
    }
    int prst,prdr;
    if(vst>4)
        prst=vst-n;
    else
        prst=vst+n;
    if(vdr>4)
        prdr=vdr-n;
    else
        prdr=vdr+n;    
    int d1=dist(x,y);
    int d2=dist(x,vst)+1+dist(prst,y);
    int d3=dist(x,vdr)+1+dist(prdr,y);
    return min(d1,min(d2,d3));
}
int main()
{
    cin>>n>>M>>Q;
    for(int i=1;i<=M;++i)
    {
        int x;
        cin>>x;
        if(x>n)
            s.insert(2*n-x);
        else
            s.insert(x+n);
        s.insert(abs(x));
    }
    for(int i=1;i<=Q;++i)
    {
        int a,b;
        cin>>a>>b;
        cout<<shc(a,b)<<'\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... |