結果

問題 No.1170 Never Want to Walk
ユーザー kyoprounokyoprouno
提出日時 2020-08-14 22:19:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 3,041 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 183,236 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-04-18 22:10:37
合計ジャッジ時間 6,522 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 164 ms
9,216 KB
testcase_28 AC 160 ms
9,344 KB
testcase_29 AC 168 ms
9,344 KB
testcase_30 AC 167 ms
9,472 KB
testcase_31 AC 161 ms
9,216 KB
testcase_32 AC 150 ms
9,216 KB
testcase_33 AC 157 ms
9,344 KB
testcase_34 AC 156 ms
9,472 KB
testcase_35 AC 155 ms
9,344 KB
testcase_36 AC 150 ms
9,216 KB
testcase_37 AC 152 ms
9,344 KB
testcase_38 AC 157 ms
9,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}

const ll INF=1e9+7;

const ll mod=1e9+7;

class DisjointSet{
    public:
    vector<ll> rank,p,sz;
    DisjointSet(){}
    DisjointSet(ll size){ //作られうる木の頂点数の最大値を入れる。
        rank.resize(size,0);
        p.resize(size,0);
        sz.resize(size,1);
        rep(i,size) makeSet(i);
    }
    void makeSet(ll x){ //xだけが属する木を作る。
        p[x]=x;
        rank[x]=0;
    }
    bool same(ll x,ll y){ //xとyが同じ木に属するかどうか
        return findSet(x)==findSet(y);
    }
    void unite(ll x, ll y){
        link(findSet(x),findSet(y));
    }
    void link(ll x, ll y){ //rankが大きい方の根に小さい方の根をつける。
        if(rank[x]>rank[y]){
            p[y]=x;
        }
        else{
            p[x]=y;
            if(rank[x]==rank[y]){
                rank[y]++; //xとyの木のrankが同じであれば、統合するとrankが1増える。
            }
        }
        sz[x]+=sz[y];
        sz[y]=sz[x];
    }
    ll findSet(ll x){ //xが属する木の根の番号を返す
        if(x!=p[x]){
            p[x]=findSet(p[x]);
        }
        return p[x];
    }
};

int main() {
    ll n,a,b;
    cin >> n >> a >> b;
    DisjointSet ds=DisjointSet(n+1);
    vector<ll> x(n);
    rep(i,n){
        cin >> x[i];
    }
    rep(i,n){
        auto it1=lower_bound(x.begin(),x.end(),x[i]+a)-x.begin();
        auto it2=lower_bound(x.begin(),x.end(),x[i]+b)-x.begin();
        auto it3=n;
        if(i<n-1){
            it3=lower_bound(x.begin(),x.end(),x[i+1]+a)-x.begin();
        }
        ll p=min({it2+1LL,it3,n});
        for(ll j=it1;j<p;j++){
            //cout << i << " " << j <<endl;
            if(!ds.same(i,j) && abs(x[i]-x[j])>=a && abs(x[i]-x[j])<=b) ds.unite(i,j);
        }
    }
    for(ll i=n-1;i>=0;i--){
        auto it1=upper_bound(x.begin(),x.end(),x[i]-a)-x.begin();
        auto it2=lower_bound(x.begin(),x.end(),x[i]-b)-x.begin();
        it1--;
        auto it3=0LL;
        if(0<i){
            it3=upper_bound(x.begin(),x.end(),x[i-1]-a)-x.begin();
            it3--;
        }
        ll p=max(it2-1LL,it3);
        p=max(p,0LL);
        for(ll j=it1;j>=p;j--){
            //cout << i << " " << j <<endl;
            if(!ds.same(i,j) && abs(x[i]-x[j])>=a && abs(x[i]-x[j])<=b) ds.unite(i,j);
        }
    }
    rep(i,n){
        cout << ds.sz[ds.findSet(i)] << endl;
    }
    return 0;
}
0