#include #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 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 table; std::vector 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{}; 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 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& 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(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(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 std::istream& operator>> (std::istream& is, std::vector& v) { for (auto & x : v) is >> x; return is; } template std::ostream& operator<< (std::ostream& os, const std::vector& v) { auto n = v.size(); os << "{"; for (size_t i = 0; i < n; i++) {os << (i > 0 ? "," : "") << v.at(i);} return os << "}"; } template std::ostream& operator<< (std::ostream& os, const std::pair& pair){ return os << "(" << pair.first << "," << pair.second << ")"; } template std::istream& operator>> (std::iostream& is, std::pair& pair) { return is >> pair.first >> pair.second; } template std::ostream& tuple_output_impl ( std::ostream& os, const std::tuple& tuple, std::integer_sequence ) { os << "("; (void)std::initializer_list{( (void)(os << (Inds > 0 ? "," : "") << std::get(tuple)), 0 )...}; return os << ")"; } template std::ostream& operator<< (std::ostream& os, const std::tuple& tuple) { return tuple_output_impl(os, tuple, std::index_sequence_for()); } template std::istream& tuple_input_impl ( std::istream& is, std::tuple& tuple, std::integer_sequence ) { (void)std::initializer_list{( (void)(is >> std::get(tuple)), 0 )...}; return is; } template std::istream& operator>> (std::istream& is, std::tuple& tuple) { return tuple_input_impl(is, tuple, std::index_sequence_for()); } int main() { std::cin.tie(0); std::cin.sync_with_stdio(false); int n, q; std::cin >> n >> q; std::vector a(n); std::cin >> a; std::vector> 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, 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; }