結果

問題 No.3072 Speedrun Query
ユーザー makichan
提出日時 2025-03-21 21:40:18
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 159 ms / 2,500 ms
コード長 2,107 bytes
コンパイル時間 6,297 ms
コンパイル使用メモリ 333,168 KB
実行使用メモリ 6,660 KB
最終ジャッジ日時 2025-03-21 21:40:29
合計ジャッジ時間 10,660 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using mint = atcoder::static_modint<998244353>;
// using mint = atcoder::static_modint<1000000007>;
using ld = long double;
using ll = long long;
#define mp(a,b) make_pair(a,b)
#define rep(i,s,n) for(int i=s; i<(int)n; i++)
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
const vector<int> dx{1,0,-1,0},dy{0,1,0,-1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;cin >> n;
    int a,b;cin >> a >> b;
    vector<int> da(n+1,-1);
    {
        queue<int> Q;
        rep(i,0,a){
            int x;cin >> x;
            da[x]=0;
            Q.push(x);
        }
        while(Q.size()){
            auto x=Q.front();Q.pop();
            int d=da[x];

            x--;
            if(1<=x && x<=n && da[x]==-1){
                Q.push(x);
                da[x]=d+1;
            }
            x++;
            x++;
            if(1<=x && x<=n && da[x]==-1){
                Q.push(x);
                da[x]=d+1;
            }
        }
    }
    vector<int> db(n+1,-1);
    {
        queue<int> Q;
        rep(i,0,b){
            int x;cin >> x;
            db[x]=0;
            Q.push(x);
        }
        while(Q.size()){
            auto x=Q.front();Q.pop();
            int d=db[x];

            x--;
            if(1<=x && x<=n && db[x]==-1){
                Q.push(x);
                db[x]=d+1;
            }
            x++;
            x++;
            if(1<=x && x<=n && db[x]==-1){
                Q.push(x);
                db[x]=d+1;
            }
        }
    }
    // rep(i,1,n+1)cout << da[i] << " ";
    // cout << "\n";
    // rep(i,1,n+1)cout << db[i] << " ";
    // cout << "\n";
    int dab=1e9;
    rep(i,1,n+1)dab=min(dab,da[i]+db[i]);
    int Q;cin >> Q;
    rep(q,0,Q){
        int s,t;cin >> s >> t;
        int ans=abs(s-t);
        ans=min(ans,da[s]+da[t]);
        ans=min(ans,db[s]+db[t]);
        ans=min(ans,da[s]+dab+db[t]);
        ans=min(ans,da[t]+dab+db[s]);
        cout << ans << "\n";
    }
}
0