Submission #1356711

#TimeUsernameProblemLanguageResultExecution timeMemory
1356711hsuan._.0528Circle Passing (EGOI24_circlepassing)C++20
14 / 100
161 ms33444 KiB
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 5e5+10;

int n, m, q;
map<int, int> mp;
set<int> st;



int f(int x, int y){
  //cout<<x<<" "<<y<<"? : \n";
    auto it = st.lower_bound(x);
    int p=0, d=1e9;
    if(it==st.end()){
        p = *prev(it);
        d = abs(x-p) + min( abs(y-p-n), 2*n - abs(y-p-n)) + 1;
      //  cout<<" ==end "<<p<<" / "<<abs(x-p)<<" "<<min( abs(y-p-n), 2*n - abs(y-p-n))<<" +1 = "<<d<<"\n";
    }else if(it==st.begin()){
        p = *it;
        d = abs(x-p) + min( abs(y-p-n), 2*n - abs(y-p-n)) + 1;
       // cout<<" ==beg "<<p<<" / "<<abs(x-p)<<" "<<min( abs(y-p-n), 2*n - abs(y-p-n))<<" +1 = "<<d<<"\n";
    }else{
        p = *it;
        d = abs(x-p) + min( abs(y-p-n), 2*n - abs(y-p-n)) + 1;
       // cout<<" >= "<<p<<" / "<<abs(x-p)<<" "<<min( abs(y-p-n), 2*n - abs(y-p-n))<<" +1 = "<<d<<"\n";
        p = *prev(it);
        d = min( d, abs(x-p) + min( abs(y-p-n), 2*n - abs(y-p-n)) + 1 );
       // cout<<" < "<<p<<" / "<<abs(x-p)<<" "<<min( abs(y-p-n), 2*n - abs(y-p-n))<<" +1 = "<<d<<"\n";
    }
    return d;
}

signed main(){
    ios_base::sync_with_stdio(0);  cin.tie(0);

    cin>>n>>m>>q;
    while(m--){
        int a;  cin>>a;
        st.insert(a);
        mp[a]=1;
    }
  
    while(q--){
        int x, y;  cin>>x>>y;
        
        if(x>y)  swap(x, y);
        if(y-x <= n/2 + (n%2)){
          //  cout<<n/2<<" !  ";
            cout<< y-x <<"\n";
        }
        else if(abs(x + 2*n - y) <= n/2 + (n%2)){
         //   cout<<n/2<<" +n  ";
            cout << abs(x + 2*n - y) << "\n";
        }
        else{
         // cout<<x<<" "<<y<<" : \n";
            int d=1e9;
            if(x >= n and y >= n){
          //    cout<<">>\n";
                d = min( f(x%n, y%n), f(y%n, x%n) );
            }
            else if(y >= n){
            //  cout<<"<>\n";
             // cout<<x<<" "<<y<<"- : \n";
                d = min( f(x, y), f(y%n, x+n) );
            }
            else{
            //  cout<<"<<\n";
                d = min( f(x, y), f(y, x) );
            }
            cout<<d<<"\n";
        }
        
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...