結果

問題 No.1950 片道きゃっちぼーる
ユーザー srjywrdnprkt
提出日時 2023-06-29 16:42:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,804 ms / 3,000 ms
コード長 1,304 bytes
コンパイル時間 2,440 ms
コンパイル使用メモリ 215,040 KB
最終ジャッジ日時 2025-02-15 03:07:07
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

map<ll, ll> mp;
vector<ll> ans;
vector<bool> vst;
vector<vector<ll>> E;

template <typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

void dfs(int from){
    vst[from] = 1;
    for (auto to : E[from]){
        if (vst[to]) continue;
        ans[to] = max(ans[to], ans[from]);
        dfs(to);
    }
}

int main(){

    ll N, M;
    cin >> N;
    vector<ll> X(N), A(N), v;
    for (int i=0; i<N; i++) cin >> X[i];
    for (int i=0; i<N; i++) cin >> A[i];
    for (int i=0; i<N; i++){
        v.push_back(X[i]);
        v.push_back(A[i]+X[i]);
        v.push_back(X[i]-A[i]);
    }
    map<ll, ll> mp = compress(v), rev;
    for (auto [x, y] : mp) rev[y] = x;
    M = mp.size();
    E.resize(M);
    ans.resize(M, -2e9);
    vst.resize(M);

    for (int i=0; i<M; i++) ans[i] = rev[i];

    for (int i=0; i<N; i++){
        E[mp[X[i]+A[i]]].push_back(mp[X[i]]);
        E[mp[X[i]-A[i]]].push_back(mp[X[i]]);
    }
    for (int i=M-1; i>=0; i--){
        if (!vst[i]) dfs(i);
    }

    for (int i=0; i<N; i++) cout << ans[mp[X[i]]] -X[i] << endl;

    return 0;
}
0