結果

問題 No.1170 Never Want to Walk
ユーザー poohbearpoohbear
提出日時 2020-08-15 07:57:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 1,913 bytes
コンパイル時間 2,479 ms
コンパイル使用メモリ 200,568 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-18 23:41:48
合計ジャッジ時間 8,568 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 3 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 4 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 358 ms
7,040 KB
testcase_28 AC 351 ms
7,168 KB
testcase_29 AC 369 ms
7,168 KB
testcase_30 AC 366 ms
7,040 KB
testcase_31 AC 353 ms
6,912 KB
testcase_32 AC 359 ms
7,168 KB
testcase_33 AC 358 ms
6,912 KB
testcase_34 AC 360 ms
7,040 KB
testcase_35 AC 364 ms
7,296 KB
testcase_36 AC 351 ms
7,168 KB
testcase_37 AC 357 ms
7,040 KB
testcase_38 AC 368 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<P,ll> PP;
using Graph = vector<vector<ll> >;
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 = 1e18;

//UnionFind
struct UnionFind {
    vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2

    UnionFind(int n) : par(n, -1) { }

    int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
        if (par[x] < 0) return x;
        return par[x] = root(par[x]);
    }

    bool merge(int x, int y) {//xの木とyの木を結合する
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) {//sizeの取得
        return -par[root(x)];
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};


//input
ll n,a,b;
vector<ll>x;

int main(){
    cin >> n >> a >> b;
    x.resize(n);
    rep(i,n)cin>>x[i];
    UnionFind u(n+1);
    vector<ll>seg(n+1,0);
    for(int i=n-1;i>=0;i--){
        ll r = lower_bound(x.begin(),x.end(),x[i]+b+1)-x.begin();
        ll l = lower_bound(x.begin(),x.end(),x[i]+a)-x.begin();
        if(l==n)continue;
        if(l==r)continue;
        u.merge(i,l);
        u.merge(i,r-1);
        seg[l]++;
        seg[r-1]--;
    }
    rep(i,n){
        seg[i+1]+=seg[i];
    }
    rep(i,n){
        if(seg[i])u.merge(i,i+1);
    }
    rep(i,n){
        cout << u.size(i) << endl;
    }

    return 0;
}
0