結果

問題 No.2292 Interval Union Find
ユーザー t98slidert98slider
提出日時 2023-03-30 01:59:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 5,000 ms
コード長 1,926 bytes
コンパイル時間 2,976 ms
コンパイル使用メモリ 176,992 KB
実行使用メモリ 12,996 KB
最終ジャッジ日時 2023-10-22 01:03:51
合計ジャッジ時間 17,607 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 55 ms
4,348 KB
testcase_05 AC 85 ms
4,348 KB
testcase_06 AC 54 ms
4,348 KB
testcase_07 AC 83 ms
4,348 KB
testcase_08 AC 84 ms
4,348 KB
testcase_09 AC 86 ms
4,348 KB
testcase_10 AC 75 ms
4,348 KB
testcase_11 AC 72 ms
4,348 KB
testcase_12 AC 91 ms
4,348 KB
testcase_13 AC 65 ms
4,348 KB
testcase_14 AC 69 ms
4,348 KB
testcase_15 AC 72 ms
4,348 KB
testcase_16 AC 68 ms
4,348 KB
testcase_17 AC 86 ms
4,348 KB
testcase_18 AC 128 ms
12,948 KB
testcase_19 AC 182 ms
12,948 KB
testcase_20 AC 173 ms
12,996 KB
testcase_21 AC 142 ms
9,132 KB
testcase_22 AC 136 ms
9,140 KB
testcase_23 AC 140 ms
9,124 KB
testcase_24 AC 138 ms
9,132 KB
testcase_25 AC 136 ms
9,136 KB
testcase_26 AC 136 ms
9,136 KB
testcase_27 AC 137 ms
9,128 KB
testcase_28 AC 132 ms
9,132 KB
testcase_29 AC 134 ms
9,136 KB
testcase_30 AC 136 ms
9,132 KB
testcase_31 AC 138 ms
9,140 KB
testcase_32 AC 135 ms
9,132 KB
testcase_33 AC 137 ms
9,128 KB
testcase_34 AC 138 ms
9,140 KB
testcase_35 AC 143 ms
9,128 KB
testcase_36 AC 139 ms
9,128 KB
testcase_37 AC 134 ms
9,136 KB
testcase_38 AC 140 ms
9,140 KB
testcase_39 AC 139 ms
9,140 KB
testcase_40 AC 141 ms
9,136 KB
testcase_41 AC 40 ms
4,348 KB
testcase_42 AC 41 ms
4,348 KB
testcase_43 AC 44 ms
4,348 KB
testcase_44 AC 53 ms
4,348 KB
testcase_45 AC 51 ms
4,348 KB
testcase_46 AC 54 ms
4,348 KB
testcase_47 AC 60 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

struct Problem_D{
    set<pair<int,int>> S;
    Problem_D(int N) {
        S.insert({N + 1, N + 1});
    }
    void insert(int L, int R){
        int l, r;
        auto it = S.lower_bound({L, -1});
        while(it->second <= R){
            tie(r, l) = *it;
            S.erase(it);
            L = min(L, l);
            R = max(R, r);
            it = S.lower_bound({L, -1});
        }
        S.insert({R, L});
    }
    void erase(int L, int R){
        int l, r;
        auto it = S.lower_bound({L + 1, -1});
        while(it->second < R){
            tie(r, l) = *it;
            S.erase(it);
            if(l < L) S.insert({L, l});
            if(R < r) S.insert({r, R});
            it = S.lower_bound({L + 1, -1});
        }
    }
    int size(int v){
        auto it = S.lower_bound({v, -1});
        if(it->second <= v && v <= it->first) return it->first - it->second + 1;
        return 1;
    }
    bool same(int v, int u){
        if(v == u) return true;
        if(u < v) swap(u, v);
        auto it = S.lower_bound({v, -1});
        return (it->second <= v && u <= it->first);
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, Q;
    cin >> N >> Q;

    assert(2 <= N && N <= 1000000000);
    assert(1 <= Q && Q <= 200000);

    Problem_D uf(N);

    int type, L, R, v, u;
    while(Q--){
        cin >> type;
        if(type <= 2){
            cin >> L >> R;
            assert(1 <= L && L <= N);
            assert(1 <= R && R <= N);
            if(type == 1) uf.insert(L, R);
            else uf.erase(L, R);
        }else if(type == 3){
            cin >> u >> v;
            assert(1 <= u && u <= N);
            assert(1 <= v && v <= N);
            cout << (uf.same(u, v) ? 1 : 0) << '\n';
        }else{
            cin >> v;
            assert(1 <= v && v <= N);
            cout << uf.size(v) << '\n';
        }
    }
}
0