結果
問題 | No.876 Range Compress Query |
ユーザー | ngtkana |
提出日時 | 2019-09-06 21:58:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 9,422 bytes |
コンパイル時間 | 2,419 ms |
コンパイル使用メモリ | 217,052 KB |
実行使用メモリ | 9,216 KB |
最終ジャッジ日時 | 2024-06-24 17:48:59 |
合計ジャッジ時間 | 5,065 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
ソースコード
#include <bits/stdc++.h> #define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++) #define rep(i, begin, end) for(int i = int(begin); i < int(end); i++) #define all(v) v.begin(), v.end() #define lint long long auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;}; auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;}; void debug_impl() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail){ std::cerr << " " << head; debug_impl(tail...); } #define debug(...)\ std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\ debug_impl(__VA_ARGS__);\ std::cerr << std::noboolalpha; template< typename Value1, typename Value2, typename BinaryOp1, typename BinaryOp2, typename BinaryOp3, typename UnaryOp1, typename UnaryOp2 > class lazy_segment_tree { struct node { int id, l, r; node (int id, int l, int r): id(id), l(l), r(r) {}; auto size () const {return r - l;} auto left_child () const { assert(size() > 1); return node(id * 2, l, (l + r) / 2); } auto right_child () const { assert(size() > 1); return node(id * 2 + 1, (l + r) / 2, r); } }; int n, N; BinaryOp1 op1; BinaryOp2 op2; BinaryOp3 op3; Value1 id1; Value2 id2; UnaryOp1 expand; UnaryOp2 shrink; std::vector<Value1> table; std::vector<Value2> lazy; node initial_node; auto& op1_eq (Value1& x, Value1 y) {return x = op1(x, y);} auto& op2_eq (Value1& x, Value2 y) {return x = op2(x, y);} auto& op3_eq (Value2& x, Value2 y) {return x = op3(x, y);} void cal (int u) { table.at(u) = op1(table.at(2 * u), table.at(2 * u + 1)); } auto chain (int u) const { auto ret = std::vector<int>{}; for (auto i = u; i > 0; i /= 2) { ret.emplace_back(i); } std::reverse(ret.begin(), ret.end()); return ret; } auto prop (int u) { op2_eq(table.at(u), lazy.at(u)); if (u < n) { op3_eq(lazy.at(2 * u), shrink(lazy.at(u))); op3_eq(lazy.at(2 * u + 1), shrink(lazy.at(u))); } lazy.at(u) = id2; return table.at(u); } auto query_base (int l, int r, Value2 val, const node& now) { prop(now.id); if (now.r <= l || r <= now.l) return id1; else if (l <= now.l && now.r <= r) { op3_eq(lazy.at(now.id), val); return prop(now.id); } else { auto ret =op1( query_base(l, r, shrink(val), now.left_child()), query_base(l, r, shrink(val), now.right_child()) ); cal(now.id); return ret; } } public: lazy_segment_tree ( int size, BinaryOp1 op1, BinaryOp2 op2, BinaryOp3 op3, Value1 id1, Value2 id2, UnaryOp1 expand, UnaryOp2 shrink ): n (std::pow(2, int(std::log2(size)) + 1)), N(n * 2), op1(std::move(op1)), op2(std::move(op2)), op3(std::move(op3)), id1(id1), id2(id2), expand(std::move(expand)), shrink(std::move(shrink)), table(N, id1), lazy(N, id2), initial_node(1, 0, n) { std::mt19937 mt(std::random_device{}()); std::uniform_int_distribution<int> dist(-1'000'000, 1'000'000); for (int i = 0; i < 20; i++) { Value1 ex1 = std::make_tuple(dist(mt), dist(mt), dist(mt)), ex1_ = std::make_tuple(dist(mt), dist(mt), dist(mt)); Value2 ex2 = dist(mt); assert(op1(ex1, id1) == ex1); assert(op2(ex1, id2) == ex1); assert(op3(ex2, id2) == ex2); assert(shrink(expand(ex2)) == ex2); assert(op2(op1(ex1, ex1_), expand(ex2)) == op1(op2(ex1, ex2), op2(ex1_, ex2))); } } void build (const Value1 x) { std::fill(table.begin(), table.end(), x); for (int i = n - 1; i >= 0; i--) cal(i); } void build (const std::vector<Value1>& v) { assert(int(v.size()) <= n); std::move(v.begin(), v.end(), table.begin() + n); for (int i = n - 1; i >= 0; i--) cal(i); } void act (int l, int r, Value2 val) { for (int i = 1; i < n; i *= 2) { val = expand(val); } query_base(l, r, val, initial_node); } auto query (int l, int r) { return query_base(l, r, id2, initial_node); } auto quiet_at (int i) const { i += n; auto actor = id2; for (auto j : chain(i)) { actor = shrink(actor); actor = op3(actor, lazy.at(j)); } return op2(table.at(i), actor); } auto quiet_collect () const { auto ret = std::vector<Value1>(n); for (auto i = 0; i < n; i++) { ret.at(i) = quiet_at(i); } return ret; } auto& at (int i) { i += n; for (auto j : chain(i)) { prop(j); } return table.at(i); } auto collect () { for (int i = 0; i < N; i++) { prop(i); } auto ret = std::vector<Value1>(n); for (auto i = 0; i < n; i++) { ret.at(i) = table.at(i + n); } return ret; } }; template< typename Value1, typename Value2, typename BinaryOp1, typename BinaryOp2, typename BinaryOp3, typename UnaryOp1, typename UnaryOp2 > auto make_lazy_segment_tree ( int size, BinaryOp1 op1, BinaryOp2 op2, BinaryOp3 op3, Value1 id1, Value2 id2, UnaryOp1 expand, UnaryOp2 shrink ) { return lazy_segment_tree< Value1, Value2, BinaryOp1, BinaryOp2, BinaryOp3, UnaryOp1, UnaryOp2 > ( size, std::move(op1), std::move(op2), std::move(op3), id1, id2, std::move(expand), std::move(shrink) ); } template< typename Value1, typename Value2, typename BinaryOp1, typename BinaryOp2, typename BinaryOp3 > auto make_lazy_segment_tree ( int size, BinaryOp1 op1, BinaryOp2 op2, BinaryOp3 op3, Value1 id1, Value2 id2 ) { auto f = [](auto x){return x;}; return make_lazy_segment_tree ( size, std::move(op1), std::move(op2), std::move(op3), id1, id2, std::move(f), std::move(f) ); } template <typename T> std::istream& operator>> (std::istream& is, std::vector<T>& v) { for (auto & x : v) is >> x; return is; } template <typename T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) { auto n = v.size(); os << "{"; for (size_t i = 0; i < n; i++) {os << (i > 0 ? "," : "") << v.at(i);} return os << "}"; } template <typename T, typename U> std::ostream& operator<< (std::ostream& os, const std::pair<T, U>& pair){ return os << "(" << pair.first << "," << pair.second << ")"; } template <typename T, typename U> std::istream& operator>> (std::iostream& is, std::pair<T, U>& pair) { return is >> pair.first >> pair.second; } template <typename... Args, std::size_t ... Inds> std::ostream& tuple_output_impl ( std::ostream& os, const std::tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...> ) { os << "("; (void)std::initializer_list<int>{( (void)(os << (Inds > 0 ? "," : "") << std::get<Inds>(tuple)), 0 )...}; return os << ")"; } template <typename... Args> std::ostream& operator<< (std::ostream& os, const std::tuple<Args...>& tuple) { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); } template <typename... Args, std::size_t ... Inds> std::istream& tuple_input_impl ( std::istream& is, std::tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...> ) { (void)std::initializer_list<int>{( (void)(is >> std::get<Inds>(tuple)), 0 )...}; return is; } template <typename... Args> std::istream& operator>> (std::istream& is, std::tuple<Args...>& tuple) { return tuple_input_impl(is, tuple, std::index_sequence_for<Args...>()); } int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n, q; std::cin >> n >> q; std::vector<int> a(n); std::cin >> a; std::vector<std::tuple<int, int, int>> tuples(n); rep(i, 0, n) { tuples.at(i) = std::make_tuple(1, a.at(i), a.at(i)); } auto segtree = make_lazy_segment_tree<std::tuple<int, int, int>, int>( n, [](auto tuple0, auto tuple1) { int k0, x0, y0; std::tie(k0, x0, y0) = tuple0; int k1, x1, y1; std::tie(k1, x1, y1) = tuple1; if (k0 == -1) return tuple1; if (k1 == -1) return tuple0; return std::make_tuple(k0 + k1 - (y0 == x1), x0, y1); }, [](auto tuple0, auto x) { if (x == -1) return tuple0; return std::make_tuple(1, x, x); }, [](auto x, auto y) { return y == -1 ? x : y; }, std::make_tuple(-1, -1, -1), -1 ); segtree.build(tuples); // debug(segtree.collect()); loop(q) { int c; std::cin >> c; if (c == 1) { int l, r, x; std::cin >> l >> r >> x; l--; segtree.act(l, r, x); // debug(segtree.collect()); } else { int l, r; std::cin >> l >> r; l--; auto ret = std::get<0>(segtree.query(l, r)); std::cout << ret << std::endl; } } return 0; }