結果

問題 No.1950 片道きゃっちぼーる
ユーザー HIcoderHIcoder
提出日時 2023-07-17 16:38:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,144 ms / 3,000 ms
コード長 3,444 bytes
コンパイル時間 3,285 ms
コンパイル使用メモリ 139,248 KB
実行使用メモリ 166,996 KB
最終ジャッジ日時 2023-10-18 00:27:08
合計ジャッジ時間 27,055 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1,024 ms
140,320 KB
testcase_04 AC 1,058 ms
140,476 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 774 ms
88,740 KB
testcase_07 AC 1,337 ms
166,996 KB
testcase_08 AC 2,144 ms
166,996 KB
testcase_09 AC 1,070 ms
126,980 KB
testcase_10 AC 1,072 ms
116,188 KB
testcase_11 AC 1,135 ms
116,188 KB
testcase_12 AC 1,090 ms
116,188 KB
testcase_13 AC 906 ms
88,868 KB
testcase_14 AC 815 ms
75,276 KB
testcase_15 AC 1,780 ms
134,140 KB
testcase_16 AC 799 ms
79,500 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1,178 ms
144,232 KB
testcase_19 AC 1,716 ms
129,124 KB
testcase_20 AC 1,133 ms
116,188 KB
testcase_21 AC 842 ms
80,556 KB
testcase_22 AC 829 ms
80,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set> 
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<ll,ll> P;
typedef pair<int,P> PP;
const ll MOD=1e9+7;


class UnionFind{

public:
    std::vector<int> par;
    std::vector<int> rank;
    std::vector<int> sz;//sz[i]で,頂点iが含まれているグループのサイズ
    std::vector<int> root;

    UnionFind(int size){
        rank=std::vector<int>(size+1,0);
        
        par = std::vector<int>(size+1,0);
        std::iota(par.begin(),par.end(),0);//#include<numeric>
        sz = std::vector<int>(size+1,1);
        root=std::vector<int>(size+1);
        std::iota(root.begin(),root.end(),0);
    }  

    

    ~UnionFind(){
        std::vector<int>().swap(rank);
        std::vector<int>().swap(par);
        std::vector<int>().swap(sz);
        std::vector<int>().swap(root);
    }  


    int find(int x){
        if(par[x]==x)return x;
        else return par[x] = find(par[x]);
    }

    bool issame(int u,int v){
        return find(u)==find(v);
    }


    void merge(int u,int v){
        u = find(u);
        v = find(v);
        if(u==v) return;

       
        if(rank[u]<rank[v]) std::swap(u,v);
        //uの傘下へvが入る
        
        //rank[u]>=rank[v]
        par[v]=u;
        sz[u]+=sz[v];
        if(rank[u]==rank[v])rank[u]++;

    }
    

    int size(int u){//頂点uが属すグループの大きさを表す.
        u=find(u);
        return sz[u];
    }

    std::map<int,std::vector<int>> element(){
        std::map<int,std::vector<int>> mp;
        for(int i=0;i<par.size();i++){
            root[i]=find(i);
            mp[root[i]].push_back(i);
        }
        return mp;

    }
};


int main(){
    int N;
    cin>>N;

  
    vector<ll> X(N);
    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>X[i];
    }
    for(int i=0;i<N;i++){
        cin>>A[i];
    }

    vector<ll> d;

    for(int i=0;i<N;i++){
        //mp[X[i]]=i;//X[i]に人iがいる
        d.push_back(X[i]);
        d.push_back(X[i]+A[i]);
        d.push_back(X[i]-A[i]);
    }

    
    set<ll> setd(d.begin(),d.end());

    map<ll,ll> mpd;
    int sz=0;
    for(ll val:setd){
        mpd[val]=sz;
        sz++;
    }

    
    map<ll,ll> rev;
    for(auto [val,idx]:mpd){
        rev[idx]=val;
    }
    



    vector<vector<int>> G(sz),rG(sz);

    
    set<pair<int,int>> edges;

    for(int i=0;i<N;i++){
        //k番目から辺を張る

        int k=mpd[X[i]];

            {
            int j=mpd[X[i]+A[i]];
            
            G[j].push_back(k);
            rG[k].push_back(j);
          
            }

            {
            int j=mpd[X[i]-A[i]];
            G[j].push_back(k);
            rG[k].push_back(j);
            }
           
            
         
    }


    vector<bool> vis(sz,false);
    vector<ll> ans(sz);

    for(int i=0;i<sz;i++){
        ans[i]=rev[i];
    }

    auto dfs=[&](auto f,int now)->void{
        vis[now]=true;
        for(int to:G[now]){
            if(vis[to]) continue;
            ans[to]=max(ans[to],ans[now]);
            f(f,to);
        }
    };

    for(int i=sz-1;i>=0;i--){
        if(!vis[i]){
            dfs(dfs,i);
        }
    }

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

    }



}

    
0