結果

問題 No.2292 Interval Union Find
ユーザー sten_sansten_san
提出日時 2023-05-05 22:43:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 6,833 bytes
コンパイル時間 1,934 ms
コンパイル使用メモリ 208,984 KB
実行使用メモリ 15,952 KB
最終ジャッジ日時 2023-08-15 05:35:00
合計ジャッジ時間 10,718 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 57 ms
4,380 KB
testcase_05 AC 96 ms
4,384 KB
testcase_06 AC 54 ms
4,380 KB
testcase_07 AC 84 ms
4,380 KB
testcase_08 AC 90 ms
4,384 KB
testcase_09 AC 92 ms
4,384 KB
testcase_10 AC 77 ms
4,380 KB
testcase_11 AC 72 ms
4,384 KB
testcase_12 AC 92 ms
4,388 KB
testcase_13 AC 64 ms
4,384 KB
testcase_14 AC 67 ms
4,380 KB
testcase_15 AC 70 ms
4,384 KB
testcase_16 AC 76 ms
4,380 KB
testcase_17 AC 81 ms
4,380 KB
testcase_18 AC 181 ms
15,852 KB
testcase_19 AC 174 ms
15,952 KB
testcase_20 AC 174 ms
15,848 KB
testcase_21 AC 147 ms
10,716 KB
testcase_22 AC 152 ms
10,712 KB
testcase_23 AC 155 ms
10,700 KB
testcase_24 AC 146 ms
10,844 KB
testcase_25 AC 150 ms
10,760 KB
testcase_26 AC 147 ms
10,744 KB
testcase_27 AC 145 ms
10,708 KB
testcase_28 AC 146 ms
10,700 KB
testcase_29 AC 147 ms
10,848 KB
testcase_30 AC 151 ms
10,688 KB
testcase_31 AC 157 ms
10,700 KB
testcase_32 AC 145 ms
10,840 KB
testcase_33 AC 155 ms
10,716 KB
testcase_34 AC 154 ms
10,760 KB
testcase_35 AC 158 ms
10,696 KB
testcase_36 AC 152 ms
10,688 KB
testcase_37 AC 150 ms
10,768 KB
testcase_38 AC 149 ms
10,972 KB
testcase_39 AC 148 ms
10,716 KB
testcase_40 AC 171 ms
10,964 KB
testcase_41 AC 71 ms
4,384 KB
testcase_42 AC 63 ms
4,384 KB
testcase_43 AC 49 ms
4,384 KB
testcase_44 AC 51 ms
4,384 KB
testcase_45 AC 50 ms
4,380 KB
testcase_46 AC 53 ms
4,380 KB
testcase_47 AC 65 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct iofast_t {
    iofast_t() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    }
} iofast;

struct uns_t {} uns;
template <typename Element, typename Head, typename ...Args>
auto vec(Element init, Head arg, Args ...args) {
    if constexpr (sizeof...(Args) == 0) return vector(arg, init);
    else return vector(arg, vec(init, args...));
}
template <typename Element, typename Head, typename ...Args>
auto vec(uns_t, Head arg, Args ...args) {
    return vec(Element(), arg, args...);
}

template <typename Container>
auto distance(const Container &c, decltype(begin(c)) iter) {
    return distance(begin(c), iter);
}

template <typename RIter, typename Compare = less<typename iterator_traits<RIter>::value_type>>
auto isort(RIter first, RIter last, Compare comp = Compare()) {
    vector<int> i(distance(first, last));
    iota(begin(i), end(i), 0);
    sort(begin(i), end(i), [&](auto x, auto y) {
        return comp(*(first + x), *(first + y));
    });
    return i;
}

template <typename, template <typename> typename, typename = void_t<>>
struct detect : false_type {};
template <typename T, template <typename> typename Check>
struct detect<T, Check, void_t<Check<T>>> : true_type {};
template <typename T, template <typename> typename Check>
constexpr inline bool detect_v = detect<T, Check>::value;

template <typename T>
using has_member_sort = decltype(declval<T>().sort());

template <typename Container, typename Compare = less<typename Container::value_type>>
auto sorted(Container c, Compare comp = Compare()) {
    if constexpr (detect_v<Container, has_member_sort>) {
        c.sort(comp);
        return c;
    }
    else {
        sort(begin(c), end(c), comp);
        return c;
    }
}

template <typename Container, typename Compare = equal_to<typename Container::value_type>>
auto uniqued(Container c, Compare comp = Compare()) {
    c.erase(unique(begin(c), end(c), comp), end(c));
    return c;
}

template <typename T, typename Compare = less<T>>
T &chmin(T &l, T r, Compare &&f = less<T>()) { return l = min(l, r, f); }
template <typename T, typename Compare = less<T>>
T &chmax(T &l, T r, Compare &&f = less<T>()) { return l = max(l, r, f); }

template <typename F>
constexpr auto fix(F &&f) noexcept {
    return [f = std::tuple<F>(std::forward<F>(f))](auto &&...args) mutable {
        return std::get<0>(f)(fix(std::get<0>(f)), std::forward<decltype(args)>(args)...);
    };
}

template <typename Int = int64_t>
struct segment_set {
    using int_type = Int;

    segment_set(): seg_() {
    }

public:
    void insert(int_type l, int_type r, const bool connect_adjacent_segment = false) {
        if (r <= l) {
            return;
        }

        auto iter1 = seg_.upper_bound(l);
        auto iter2 = seg_.lower_bound(r + connect_adjacent_segment);

        if (iter1 != std::begin(seg_)) {
            std::advance(iter1, -1);
            if (l < iter1->second + connect_adjacent_segment) {
                l = iter1->first;
            }
            std::advance(iter1, +1);
        }

        if (iter2 != std::begin(seg_)) {
            std::advance(iter2, -1);
            if (r <= iter2->second) {
                r = iter2->second;
            }
            std::advance(iter2, +1);
        }

        seg_.erase(iter1, iter2);

        seg_[l] = r;
    }

    void split(int_type p) {
        auto iter = seg_.upper_bound(p);

        if (iter != std::begin(seg_)) {
            std::advance(iter, -1);
        }

        if (iter == std::end(seg_) || p < iter->first) {
            return;
        }

        if (iter->second <= p) {
            return;
        }

        auto [l, r] = *iter;
        seg_.erase(iter);
        seg_[l] = p;
        seg_[p] = r;
    }

    void overwrite(int_type l, int_type r) {
        if (r <= l) {
            return;
        }

        auto iter = seg_.lower_bound(l);

        if (iter != std::begin(seg_)) {
            std::advance(iter, -1);
        }

        if (iter == std::end(seg_)) {
            return;
        }

        if (iter->first < l && r < iter->second) {
            auto [l1, r1] = *iter;
            seg_.erase(iter);
            seg_[l1] = l;
            seg_[r] = r1;
            return;
        }

        if (iter->first < l && l < iter->second) {
            auto [l1, r1] = *iter;
            seg_.erase(iter);
            seg_[l1] = l;
        }

        iter = seg_.lower_bound(l);

        while (iter != std::end(seg_) && iter->first < r) {
            if (iter->second <= r) {
                iter = seg_.erase(iter);
                continue;
            }

            auto [l1, r1] = *iter;
            seg_.erase(iter);
            seg_[r] = r1;

            break;
        }
    }

    void remove_covered(int_type l, int_type r) {
        if (r <= l) {
            return;
        }

        auto iter = seg_.upper_bound(l);

        if (iter != std::begin(seg_)) {
            std::advance(iter, -1);
        }

        while (iter != std::end(seg_) && iter->first < r) {
            if (l < iter->second) {
                iter = seg_.erase(iter);
            }
            else {
                std::advance(iter, +1);
            }
        }
    }

    void remove_covered(int_type x) {
        remove_covered(x, x + 1);
    }

    pair<int_type, int_type> covered(int_type x) const {
        auto iter = seg_.upper_bound(x);

        if (iter == std::begin(seg_)) {
            return { x, x };
        }
        std::advance(iter, -1);

        if (iter->second <= x) {
            return { x, x };
        }

        return *iter;
    }

    int_type covered_size(int_type x) const {
        auto [l, r] = covered(x);
        return r - l;
    }

    bool is_covered(int_type x) const {
        return 0 < covered_size(x);
    }

    bool same(int_type x, int_type y) const {
        auto [l, r] = covered(x);
        return l <= y && y < r;
    }

private:
    std::map<int_type, int_type> seg_;
};

int main() {
    int n, q; cin >> n >> q;

    segment_set<int64_t> set;

    while (q--) {
        int t; cin >> t;

        if (t == 1) {
            int l, r; cin >> l >> r; ++r;

            set.insert(l, r);
        }

        if (t == 2) {
            int l, r; cin >> l >> r;

            if (l + 1 < r) {
                set.overwrite(l + 1, r);
            }
            else {
                set.split(l + 1);
            }
        }

        if (t == 3) {
            int u, v; cin >> u >> v;

            if (u != v) {
                cout << set.same(u, v) << '\n';
            }
            else {
                cout << 1 << endl;
            }
        }

        if (t == 4) {
            int u; cin >> u;

            cout << max<int64_t>(1, set.covered_size(u)) << '\n';
        }
    }

    cout << flush;
}

0