結果

問題 No.1170 Never Want to Walk
コンテスト
ユーザー totori_nyaa
提出日時 2020-10-07 20:24:21
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 24 ms / 2,000 ms
+ 937µs
コード長 1,509 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,372 ms
コンパイル使用メモリ 216,132 KB
実行使用メモリ 9,820 KB
最終ジャッジ日時 2026-10-15 11:48:59
合計ジャッジ時間 4,775 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
#define totori signed
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
ll dx[4]={0,1,-1,0};
ll dy[4]={1,0,0,-1};


class UnionFind{
    public:
    //親の番号を格納する。親だった場合は-(その集合のサイズ)
    vector<int> parent;

    UnionFind(int N){
        parent = vector<int>(N,-1);
    }

    int root(int A){
        if(parent[A] < 0) return A;
        return parent[A]=root(parent[A]);
    }

    int size(int A){
        return -parent[root(A)];
    }

    bool unite(int A, int B) {
        A = root(A), B = root(B);
        if(A == B) return false; 

        if(size(A) < size(B)) swap(A,B);
        parent[A] += parent[B];
        parent[B] = A;
        return true;
    }

    bool same(int A, int B){
        return root(A)==root(B);
    } 
};

totori main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);
    
    int n,a,b;
    cin>>n>>a>>b;
    int x[n];
    for(int i=0;i<n;i++)cin>>x[i];
    UnionFind uni(n);
    int r = 0;
    for(int i=0;i<n;i++){
        chmax(r,i);
        if(r>i && x[r-1]-x[i]>=a && x[r-1]-x[i]>=a)uni.unite(r-1,i);
        while(r<n && x[r]-x[i]<=b){
            if(x[r]-x[i]>=a)uni.unite(i,r);
            r++;
        }
    }
    for(int i=0;i<n;i++){
        cout << uni.size(i) << "\n";
    }
}
0