#line 1 "/Users/sanghabaek/Documents/suisen/library/template.hpp" #include #include namespace suisen { template bool chmin(T& x, const T& y) { return y >= x ? false : (x = y, true); } template bool chmax(T& x, const T& y) { return y <= x ? false : (x = y, true); } template constexpr int pow_m1(T n) { return -(n & 1) | 1; } template constexpr T fld(const T x, const T y) { T q = x / y, r = x % y; return q - ((x ^ y) < 0 and (r != 0)); } template constexpr T cld(const T x, const T y) { T q = x / y, r = x % y; return q + ((x ^ y) > 0 and (r != 0)); } } namespace suisen::macro { #define IMPL_REPITER(cond) auto& begin() { return *this; } auto end() { return nullptr; } auto& operator*() { return _val; } auto& operator++() { return _val += _step, *this; } bool operator!=(std::nullptr_t) { return cond; } template == std::is_signed_v), std::nullptr_t> = nullptr> struct rep_impl { Int _val; const Int _end, _step; rep_impl(Int n) : rep_impl(0, n) {} rep_impl(IntL l, Int r, IntStep step = 1) : _val(l), _end(r), _step(step) {} IMPL_REPITER((_val < _end)) }; template == std::is_signed_v), std::nullptr_t> = nullptr> struct rrep_impl { Int _val; const Int _end, _step; rrep_impl(Int n) : rrep_impl(0, n) {} rrep_impl(IntL l, Int r) : _val(r - 1), _end(l), _step(-1) {} rrep_impl(IntL l, Int r, IntStep step) : _val(l + fld(r - l - 1, step) * step), _end(l), _step(-step) {} IMPL_REPITER((_val >= _end)) }; template struct repinf_impl { Int _val; const Int _step; repinf_impl(Int l, IntStep step = 1) : _val(l), _step(step) {} IMPL_REPITER((true)) }; #undef IMPL_REPITER } #line 1 "/Users/sanghabaek/Documents/suisen/library/io/input_stream.hpp" #line 1 "/Users/sanghabaek/Documents/suisen/library/type_traits/type_traits.hpp" #line 6 "/Users/sanghabaek/Documents/suisen/library/type_traits/type_traits.hpp" #include namespace suisen { template using constraints_t = std::enable_if_t, std::nullptr_t>; template struct bitnum { static constexpr int value = 0; }; template struct bitnum>> { static constexpr int value = std::numeric_limits>::digits; }; template static constexpr int bitnum_v = bitnum::value; template struct is_nbit { static constexpr bool value = bitnum_v == n; }; template static constexpr bool is_nbit_v = is_nbit::value; template struct safely_multipliable { using type = T; }; template struct safely_multipliable, is_nbit>> { using type = long long; }; template struct safely_multipliable, is_nbit>> { using type = __int128_t; }; template struct safely_multipliable, is_nbit>> { using type = unsigned long long; }; template struct safely_multipliable, is_nbit>> { using type = __uint128_t; }; template using safely_multipliable_t = typename safely_multipliable::type; template struct rec_value_type { using type = T; }; template struct rec_value_type> { using type = typename rec_value_type::type; }; template using rec_value_type_t = typename rec_value_type::type; template class is_iterable { template static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_iterable_v = is_iterable::value; template class is_writable { template static auto test(T_ e) -> decltype(std::declval() << e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_writable_v = is_writable::value; template class is_readable { template static auto test(T_ e) -> decltype(std::declval() >> e, std::true_type{}); static std::false_type test(...); public: static constexpr bool value = decltype(test(std::declval()))::value; }; template static constexpr bool is_readable_v = is_readable::value; } // namespace suisen #line 6 "/Users/sanghabaek/Documents/suisen/library/io/input_stream.hpp" namespace suisen::io { template >, std::negation>>>, std::nullptr_t> = nullptr> struct InputStream { private: using istream_type = std::remove_reference_t; IStream is; struct { InputStream* is; template operator T() { T e; *is >> e; return e; } } _reader{ this }; public: template InputStream(IStream_ &&is) : is(std::move(is)) {} template InputStream(IStream_ &is) : is(is) {} template InputStream& operator>>(T& e) { if constexpr (suisen::is_readable_v) is >> e; else _read(e); return *this; } auto read() { return _reader; } template void read(Head& head, Tail &...tails) { ((*this >> head) >> ... >> tails); } istream_type& get_stream() { return is; } private: static __uint128_t _stou128(const std::string& s) { __uint128_t ret = 0; for (char c : s) if ('0' <= c and c <= '9') ret = 10 * ret + c - '0'; return ret; } static __int128_t _stoi128(const std::string& s) { return (s[0] == '-' ? -1 : +1) * _stou128(s); } void _read(__uint128_t& v) { v = _stou128(std::string(_reader)); } void _read(__int128_t& v) { v = _stoi128(std::string(_reader)); } template void _read(std::pair& a) { *this >> a.first >> a.second; } template void _read(std::tuple& a) { if constexpr (N < sizeof...(Args)) *this >> std::get(a), _read(a); } template , std::nullptr_t> = nullptr> void _read(Iterable& a) { for (auto& e : a) *this >> e; } }; template InputStream(IStream &&) -> InputStream; template InputStream(IStream &) -> InputStream; InputStream cin{ std::cin }; auto read() { return cin.read(); } template void read(Head& head, Tail &...tails) { cin.read(head, tails...); } } // namespace suisen::io namespace suisen { using io::read; } // namespace suisen #line 1 "/Users/sanghabaek/Documents/suisen/library/io/output_stream.hpp" #line 6 "/Users/sanghabaek/Documents/suisen/library/io/output_stream.hpp" namespace suisen::io { template >, std::negation>>>, std::nullptr_t> = nullptr> struct OutputStream { private: using ostream_type = std::remove_reference_t; OStream os; public: template OutputStream(OStream_ &&os) : os(std::move(os)) {} template OutputStream(OStream_ &os) : os(os) {} template OutputStream& operator<<(const T& e) { if constexpr (suisen::is_writable_v) os << e; else _print(e); return *this; } void print() { *this << '\n'; } template void print(const Head& head, const Tail &...tails) { *this << head, ((*this << ' ' << tails), ...), *this << '\n'; } template , std::nullptr_t> = nullptr> void print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") { for (auto it = v.begin(); it != v.end();) if (*this << *it; ++it != v.end()) *this << sep; *this << end; } ostream_type& get_stream() { return os; } private: void _print(__uint128_t value) { char buffer[41], *d = std::end(buffer); do *--d = '0' + (value % 10), value /= 10; while (value); os.rdbuf()->sputn(d, std::end(buffer) - d); } void _print(__int128_t value) { if (value < 0) *this << '-'; _print(__uint128_t(value < 0 ? -value : value)); } template void _print(const std::pair& a) { *this << a.first << ' ' << a.second; } template void _print(const std::tuple& a) { if constexpr (N < std::tuple_size_v>) { if constexpr (N) *this << ' '; *this << std::get(a), _print(a); } } template , std::nullptr_t> = nullptr> void _print(const Iterable& a) { print_all(a, " ", ""); } }; template OutputStream(OStream_ &&) -> OutputStream; template OutputStream(OStream_ &) -> OutputStream; OutputStream cout{ std::cout }, cerr{ std::cerr }; template void print(const Args &... args) { cout.print(args...); } template , std::nullptr_t> = nullptr> void print_all(const Iterable& v, const std::string& sep = " ", const std::string& end = "\n") { cout.print_all(v, sep, end); } } // namespace suisen::io namespace suisen { using io::print, io::print_all; } // namespace suisen #line 33 "/Users/sanghabaek/Documents/suisen/library/template.hpp" namespace suisen { template , std::enable_if_t, std::is_invocable_r, std::invoke_result_t>>, std::nullptr_t> = nullptr> auto comparator(const ToKey& to_key, const CompKey& comp_key = std::less<>()) { return [=](const T& x, const T& y) { return comp_key(to_key(x), to_key(y)); }; } template , std::nullptr_t> = nullptr> std::vector sorted_indices(int n, const Compare& compare) { std::vector p(n); return std::iota(p.begin(), p.end(), 0), std::sort(p.begin(), p.end(), compare), p; } template , std::nullptr_t> = nullptr> std::vector sorted_indices(int n, const ToKey& to_key) { return sorted_indices(n, comparator(to_key)); } template auto priority_queue_with_comparator(const Comparator& comparator) { return std::priority_queue, Comparator>{ comparator }; } template , std::nullptr_t> = nullptr> void sort_unique_erase(Iterable& a) { std::sort(a.begin(), a.end()), a.erase(std::unique(a.begin(), a.end()), a.end()); } template struct Dim : std::array { template Dim(const Ints& ...ns) : std::array::array{ static_cast(ns)... } {} }; template Dim(const Ints& ...) -> Dim; template auto ndvec(const Dim &ns, const T& value = {}) { if constexpr (I + 1 < D) { return std::vector(ns[I], ndvec(ns, value)); } else { return std::vector(ns[I], value); } } } namespace suisen { using int128 = __int128_t; using uint128 = __uint128_t; template using min_priority_queue = std::priority_queue, std::greater>; template using max_priority_queue = std::priority_queue, std::less>; } namespace suisen { const std::string Yes = "Yes", No = "No", YES = "YES", NO = "NO"; } #ifdef LOCAL # define debug(...) debug_impl(#__VA_ARGS__, __VA_ARGS__) template void debug_impl(const char* s, const H& h, const Ts&... t) { suisen::io::cerr << "[\033[32mDEBUG\033[m] " << s << ": " << h, ((suisen::io::cerr << ", " << t), ..., (suisen::io::cerr << "\n")); } #else # define debug(...) void(0) #endif #define FOR(e, v) for (auto &&e : v) #define CFOR(e, v) for (const auto &e : v) #define REP(i, ...) CFOR(i, suisen::macro::rep_impl(__VA_ARGS__)) #define RREP(i, ...) CFOR(i, suisen::macro::rrep_impl(__VA_ARGS__)) #define REPINF(i, ...) CFOR(i, suisen::macro::repinf_impl(__VA_ARGS__)) #define LOOP(n) for ([[maybe_unused]] const auto& _ : suisen::macro::rep_impl(n)) #define ALL(iterable) std::begin(iterable), std::end(iterable) #line 1 "/Users/sanghabaek/Documents/suisen/library/datastructure/segment_tree/segment_tree.hpp" #line 6 "/Users/sanghabaek/Documents/suisen/library/datastructure/segment_tree/segment_tree.hpp" #line 1 "/Users/sanghabaek/Documents/suisen/library/util/update_proxy_object.hpp" #line 5 "/Users/sanghabaek/Documents/suisen/library/util/update_proxy_object.hpp" namespace suisen { template > = nullptr> struct UpdateProxyObject { public: UpdateProxyObject(T &v, UpdateFunc update) : v(v), update(update) {} operator T() const { return v; } auto& operator++() && { ++v, update(); return *this; } auto& operator--() && { --v, update(); return *this; } auto& operator+=(const T &val) && { v += val, update(); return *this; } auto& operator-=(const T &val) && { v -= val, update(); return *this; } auto& operator*=(const T &val) && { v *= val, update(); return *this; } auto& operator/=(const T &val) && { v /= val, update(); return *this; } auto& operator%=(const T &val) && { v %= val, update(); return *this; } auto& operator =(const T &val) && { v = val, update(); return *this; } auto& operator<<=(const T &val) && { v <<= val, update(); return *this; } auto& operator>>=(const T &val) && { v >>= val, update(); return *this; } template > = nullptr> auto& apply(F f) && { v = f(v), update(); return *this; } private: T &v; UpdateFunc update; }; } // namespace suisen #line 8 "/Users/sanghabaek/Documents/suisen/library/datastructure/segment_tree/segment_tree.hpp" namespace suisen { template class SegmentTree { public: SegmentTree() : SegmentTree(0) {} explicit SegmentTree(int n) : SegmentTree(std::vector(n, e())) {} SegmentTree(const std::vector &a) : n(a.size()), m(ceil_pow2(n)), data(2 * m, e()) { build(a); } void build(const std::vector &a) { assert(int(a.size()) <= m); std::copy(a.begin(), a.end(), data.begin() + m); for (int k = m - 1; k > 0; --k) update(k); } const T& get(int i) const { assert(0 <= i and i < n); return data[i + m]; } T operator()(int l, int r) const { T res_l = e(), res_r = e(); for (l += m, r += m; l < r; l >>= 1, r >>= 1) { if (l & 1) res_l = op(res_l, data[l++]); if (r & 1) res_r = op(data[--r], res_r); } return op(res_l, res_r); } T prod(int l, int r) const { return (*this)(l, r); } T prefix_prod(int r) const { return (*this)(0, r); } T suffix_prod(int l) const { return (*this)(l, m); } T all_prod() const { return data[1]; } void set(int i, const T &val) { (*this)[i] = val; } auto operator[](int i) { assert(0 <= i and i < n); int k = i + m; return UpdateProxyObject { data[k], [this, k]{ update_from(k); } }; } template > = nullptr> int max_right(int l, const Pred &f) const { assert(0 <= l and l <= n); assert(f(e)); if (l == n) return n; l += m; T sum_l = e; do { while (l % 2 == 0) l >>= 1; if (not f(op(sum_l, data[l]))) { while (l < m) { l = 2 * l; if (f(op(sum_l, data[l]))) sum_l = op(sum_l, data[l++]); } return l - m; } sum_l = op(sum_l, data[l]); l++; } while ((l & -l) != l); return n; } template int max_right(int l) { return max_right(l, f); } template > = nullptr> int min_left(int r, const Pred &f) const { assert(0 <= r && r <= n); assert(f(e)); if (r == 0) return 0; r += m; T sum_r = e; do { r--; while (r > 1 && (r % 2)) r >>= 1; if (not f(op(data[r], sum_r))) { while (r < m) { r = 2 * r + 1; if (f(op(data[r], sum_r))) sum_r = op(data[r--], sum_r); } return r + 1 - m; } sum_r = op(data[r], sum_r); } while ((r & -r) != r); return 0; } template int min_left(int l) { return min_left(l, f); } private: int n, m; std::vector data; static constexpr int ceil_pow2(int n) { int m = 1; while (m < n) m <<= 1; return m; } void update_from(int k) { for (k >>= 1; k; k >>= 1) update(k); } void update(int k) { data[k] = op(data[k * 2], data[k * 2 + 1]); } }; } // namespace suisen #line 3 "/var/folders/49/nmnlgfh97r1frq9j9y4ls8cw0000gn/T/nbundle-pre-8665.cpp" using namespace std; using namespace suisen; struct Node { int open, close, pairs; }; Node op(Node a, Node b) { int m = min(a.open, b.close); return { a.open + b.open - m, a.close + b.close - m, a.pairs + b.pairs + m }; } Node e() { return { 0, 0, 0 }; } int main() { int N, Q; string S; read(N, Q, S); vector init(N); REP(i, N) { init[i] = S[i] == '(' ? Node{ 1, 0, 0 } : Node{ 0, 1, 0 }; } SegmentTree seg(init); while (Q--) { int type, x, y; read(type, x, y); if (type == 1) { --x; S[x] = y == 1 ? '(' : ')'; seg.set(x, S[x] == '(' ? Node{ 1, 0, 0 } : Node{ 0, 1, 0 }); } else { --x; print(2 * seg.prod(x, y).pairs); } } return 0; }