結果

問題 No.1170 Never Want to Walk
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2020-08-15 00:09:36
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 863 ms / 2,000 ms
コード長 1,751 bytes
コンパイル時間 860 ms
コンパイル使用メモリ 128,576 KB
実行使用メモリ 9,472 KB
最終ジャッジ日時 2024-04-18 23:23:27
合計ジャッジ時間 13,301 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 6 ms
6,940 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 4 ms
6,944 KB
testcase_17 AC 5 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 5 ms
6,944 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 5 ms
6,944 KB
testcase_25 AC 6 ms
6,940 KB
testcase_26 AC 5 ms
6,944 KB
testcase_27 AC 838 ms
9,088 KB
testcase_28 AC 781 ms
9,216 KB
testcase_29 AC 841 ms
9,344 KB
testcase_30 AC 816 ms
9,344 KB
testcase_31 AC 825 ms
9,216 KB
testcase_32 AC 863 ms
9,216 KB
testcase_33 AC 835 ms
9,216 KB
testcase_34 AC 860 ms
9,344 KB
testcase_35 AC 858 ms
9,344 KB
testcase_36 AC 824 ms
9,216 KB
testcase_37 AC 860 ms
9,216 KB
testcase_38 AC 845 ms
9,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

class UnionFind {
private:
    vector<long long> par,_size,_right;
public:
    UnionFind(long long n) : par(n+1),_size(n+1,1),_right(n+1) {
        iota(par.begin(), par.end(), 0);
        iota(_right.begin(), _right.end(), 0);
    }

    long long root(long long x) {
        if (x == par[x]) return x;
        else return par[x] = root(par[x]);
    }

    bool isSame(long long x, long long y) {
        return root(x) == root(y);
    }

    bool merge(long long x, long long y) {
        x = root(x); y = root(y);
        if (isSame(x,y)) return false;
        if (_size[x] < _size[y]) swap(x,y);
        par[y] = x;
        _size[x] += _size[y];
        _right[x] = max(_right[x], _right[y]);
        return true;
    }

    void range_merge(long long x, long long y) {
        if (x >= y) return;
        while(this->right(x) < y) {
            this->merge(this->right(x), this->right(x)+1);
        }
    }

    long long size(long long x) {
        return _size[root(x)];
    }

    long long right(long long x) {
        return _right[root(x)];
    }
};

int main() {
    int n,a,b;
    cin >> n >> a >> b;
    vector x(n,0);
    for (int i=0; i<n; i++) cin >> x[i];
    UnionFind uni(n);

    vector imos(n+1, 0);
    for (int i=0; i<n-1; i++) {
        auto l = lower_bound(x.begin(), x.end(), x[i]+a);
        auto r = upper_bound(x.begin(), x.end(), x[i]+b);
        if (l >= r) continue;
        int lindex = l - x.begin();
        int rindex = r - x.begin();
        uni.merge(i, lindex);
        uni.range_merge(lindex, rindex-1);
        cerr<<i<<endl;
    }

    for (int i=0; i<n; i++) {
        cout << uni.size(i) << endl;
    }
}
0