結果
問題 | No.2292 Interval Union Find |
ユーザー | sten_san |
提出日時 | 2023-05-05 22:43:16 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 254 ms / 5,000 ms |
コード長 | 6,833 bytes |
コンパイル時間 | 2,387 ms |
コンパイル使用メモリ | 209,820 KB |
実行使用メモリ | 15,872 KB |
最終ジャッジ日時 | 2024-11-23 10:14:23 |
合計ジャッジ時間 | 12,522 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 44 |
ソースコード
#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; }