Hashiryo's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hashiryo/Library

:heavy_check_mark: test/sample_test/code_festival_exhibition_b.RBST.test.cpp

Depends on

Code

// competitive-verifier: STANDALONE

// https://atcoder.jp/contests/code-festival-2014-exhibition/tasks/code_festival_exhibition_b
// split3, prod
#include <sstream>
#include <string>
#include <cassert>
#include "src/Math/ModInt.hpp"
#include "src/DataStructure/RandomizedBinarySearchTree.hpp"
using namespace std;
bool test(int (*solve)(stringstream&, stringstream&), string in, string expected) {
 stringstream scin(in), scout;
 solve(scin, scout);
 return scout.str() == expected;
}
namespace TEST {
struct M {
 using T= array<int, 2>;
 static T op(const T& l, const T& r) {
  if (l[1] > r[0]) return {l[0], r[1] + l[1] - r[0]};
  else return {l[0] + r[0] - l[1], r[1]};
 }
};
signed main(stringstream& scin, stringstream& scout) {
 using RBST= RandomizedBinarySearchTree<M>;
 int Q;
 scin >> Q;
 RBST rbst;
 {
  string S;
  scin >> S;
  int N= S.length();
  vector<typename M::T> v(N);
  for (int i= N; i--;) v[i]= {S[i] == ')', S[i] == '('};
  rbst= RBST(v);
 }
 while (Q--) {
  char x;
  int y, z;
  scin >> x >> y >> z;
  --y;
  if (x == '(') {
   rbst.insert(y, {0, 1});
  } else if (x == ')') {
   rbst.insert(y, {1, 0});
  } else if (x == 'D') {
   rbst.erase(y);
  } else {
   auto [l, r]= rbst.prod(y, z);
   scout << l + r << '\n';
  }
 }
 return 0;
}
}
signed main() {
 assert(test(TEST::main, "5\n(()\nD 1 0\n( 3 0\nQ 1 2\n) 1 0\nQ 2 4\n", "0\n1\n"));
 assert(test(TEST::main,
             "11\n"
             "(()()(()\n"
             "( 1 0\n"
             "( 1 0\n"
             "D 4 0\n"
             "Q 2 6\n"
             ") 5 0\n"
             "D 8 0\n"
             "Q 1 9\n"
             "( 3 0\n"
             ") 8 0\n"
             "D 10 0\n"
             "Q 5 10\n",
             "1\n1\n4\n"));
 return 0;
}
#line 1 "test/sample_test/code_festival_exhibition_b.RBST.test.cpp"
// competitive-verifier: STANDALONE

// https://atcoder.jp/contests/code-festival-2014-exhibition/tasks/code_festival_exhibition_b
// split3, prod
#include <sstream>
#include <string>
#include <cassert>
#line 2 "src/Math/ModInt.hpp"
#include <iostream>
#line 2 "src/Math/mod_inv.hpp"
#include <utility>
#include <type_traits>
#line 5 "src/Math/mod_inv.hpp"
template <class Uint> constexpr inline Uint mod_inv(Uint a, Uint mod) {
 std::make_signed_t<Uint> x= 1, y= 0, z= 0;
 for (Uint q= 0, b= mod, c= 0; b;) z= x, x= y, y= z - y * (q= a / b), c= a, a= b, b= c - b * q;
 return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod;
}
#line 2 "src/Internal/Remainder.hpp"
namespace math_internal {
using namespace std;
using u8= unsigned char;
using u32= unsigned;
using i64= long long;
using u64= unsigned long long;
using u128= __uint128_t;
struct MP_Na {  // mod < 2^32
 u32 mod;
 constexpr MP_Na(): mod(0) {}
 constexpr MP_Na(u32 m): mod(m) {}
 constexpr inline u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; }
 constexpr inline u32 set(u32 n) const { return n; }
 constexpr inline u32 get(u32 n) const { return n; }
 constexpr inline u32 norm(u32 n) const { return n; }
 constexpr inline u32 plus(u64 l, u32 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u32 diff(u64 l, u32 r) const { return l-= r, l >> 63 ? l + mod : l; }
};
template <class u_t, class du_t, u8 B> struct MP_Mo {  // mod < 2^32, mod < 2^62
 u_t mod;
 constexpr MP_Mo(): mod(0), iv(0), r2(0) {}
 constexpr MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {}
 constexpr inline u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); }
 constexpr inline u_t set(u_t n) const { return mul(n, r2); }
 constexpr inline u_t get(u_t n) const { return n= reduce(n), n >= mod ? n - mod : n; }
 constexpr inline u_t norm(u_t n) const { return n >= mod ? n - mod : n; }
 constexpr inline u_t plus(u_t l, u_t r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u_t diff(u_t l, u_t r) const { return l-= r, l >> (B - 1) ? l + (mod << 1) : l; }
private:
 u_t iv, r2;
 static constexpr u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; }
 constexpr inline u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); }
};
using MP_Mo32= MP_Mo<u32, u64, 32>;
using MP_Mo64= MP_Mo<u64, u128, 64>;
struct MP_Br {  // 2^20 < mod <= 2^41
 u64 mod;
 constexpr MP_Br(): mod(0), x(0) {}
 constexpr MP_Br(u64 m): mod(m), x((u128(1) << 84) / m) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); }
 static constexpr inline u64 set(u64 n) { return n; }
 constexpr inline u64 get(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 norm(u64 n) const { return n >= mod ? n - mod : n; }
 constexpr inline u64 plus(u64 l, u64 r) const { return l+= r, l < (mod << 1) ? l : l - (mod << 1); }
 constexpr inline u64 diff(u64 l, u64 r) const { return l-= r, l >> 63 ? l + (mod << 1) : l; }
private:
 u64 x;
 constexpr inline u128 quo(const u128 &n) const { return (n * x) >> 84; }
 constexpr inline u64 rem(const u128 &n) const { return n - quo(n) * mod; }
};
template <class du_t, u8 B> struct MP_D2B1 {  // mod < 2^63, mod < 2^64
 u64 mod;
 constexpr MP_D2B1(): mod(0), s(0), d(0), v(0) {}
 constexpr MP_D2B1(u64 m): mod(m), s(__builtin_clzll(m)), d(m << s), v(u128(-1) / d) {}
 constexpr inline u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; }
 constexpr inline u64 set(u64 n) const { return n; }
 constexpr inline u64 get(u64 n) const { return n; }
 constexpr inline u64 norm(u64 n) const { return n; }
 constexpr inline u64 plus(du_t l, u64 r) const { return l+= r, l < mod ? l : l - mod; }
 constexpr inline u64 diff(du_t l, u64 r) const { return l-= r, l >> B ? l + mod : l; }
private:
 u8 s;
 u64 d, v;
 constexpr inline u64 rem(const u128 &u) const {
  u128 q= (u >> 64) * v + u;
  u64 r= u64(u) - (q >> 64) * d - d;
  if (r > u64(q)) r+= d;
  if (r >= d) r-= d;
  return r;
 }
};
using MP_D2B1_1= MP_D2B1<u64, 63>;
using MP_D2B1_2= MP_D2B1<u128, 127>;
template <class u_t, class MP> constexpr u_t pow(u_t x, u64 k, const MP &md) {
 for (u_t ret= md.set(1);; x= md.mul(x, x))
  if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret;
}
}
#line 3 "src/Internal/modint_traits.hpp"
namespace math_internal {
struct m_b {};
struct s_b: m_b {};
}
template <class mod_t> constexpr bool is_modint_v= std::is_base_of_v<math_internal::m_b, mod_t>;
template <class mod_t> constexpr bool is_staticmodint_v= std::is_base_of_v<math_internal::s_b, mod_t>;
#line 6 "src/Math/ModInt.hpp"
namespace math_internal {
template <class MP, u64 MOD> struct SB: s_b {
protected:
 static constexpr MP md= MP(MOD);
};
template <class U, class B> struct MInt: public B {
 using Uint= U;
 static constexpr inline auto mod() { return B::md.mod; }
 constexpr MInt(): x(0) {}
 template <class T, typename= enable_if_t<is_modint_v<T> && !is_same_v<T, MInt>>> constexpr MInt(T v): x(B::md.set(v.val() % B::md.mod)) {}
 constexpr MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {}
 constexpr MInt operator-() const { return MInt() - *this; }
#define FUNC(name, op) \
 constexpr MInt name const { \
  MInt ret; \
  return ret.x= op, ret; \
 }
 FUNC(operator+(const MInt & r), B::md.plus(x, r.x))
 FUNC(operator-(const MInt & r), B::md.diff(x, r.x))
 FUNC(operator*(const MInt & r), B::md.mul(x, r.x))
 FUNC(pow(u64 k), math_internal::pow(x, k, B::md))
#undef FUNC
 constexpr MInt operator/(const MInt &r) const { return *this * r.inv(); }
 constexpr MInt &operator+=(const MInt &r) { return *this= *this + r; }
 constexpr MInt &operator-=(const MInt &r) { return *this= *this - r; }
 constexpr MInt &operator*=(const MInt &r) { return *this= *this * r; }
 constexpr MInt &operator/=(const MInt &r) { return *this= *this / r; }
 constexpr bool operator==(const MInt &r) const { return B::md.norm(x) == B::md.norm(r.x); }
 constexpr bool operator!=(const MInt &r) const { return !(*this == r); }
 constexpr bool operator<(const MInt &r) const { return B::md.norm(x) < B::md.norm(r.x); }
 constexpr inline MInt inv() const { return mod_inv<U>(val(), B::md.mod); }
 constexpr inline Uint val() const { return B::md.get(x); }
 friend ostream &operator<<(ostream &os, const MInt &r) { return os << r.val(); }
 friend istream &operator>>(istream &is, MInt &r) {
  i64 v;
  return is >> v, r= MInt(v), is;
 }
private:
 Uint x;
};
template <u64 MOD> using MP_B= conditional_t < (MOD < (1 << 30)) & MOD, MP_Mo32, conditional_t < MOD < (1ull << 32), MP_Na, conditional_t<(MOD < (1ull << 62)) & MOD, MP_Mo64, conditional_t<MOD<(1ull << 41), MP_Br, conditional_t<MOD<(1ull << 63), MP_D2B1_1, MP_D2B1_2>>>>>;
template <u64 MOD> using ModInt= MInt < conditional_t<MOD<(1 << 30), u32, u64>, SB<MP_B<MOD>, MOD>>;
}
using math_internal::ModInt;
#line 2 "src/DataStructure/RandomizedBinarySearchTree.hpp"
#include <vector>
#line 4 "src/DataStructure/RandomizedBinarySearchTree.hpp"
#include <array>
#include <tuple>
#line 7 "src/DataStructure/RandomizedBinarySearchTree.hpp"
#include <cstddef>
#line 3 "src/Internal/detection_idiom.hpp"
#define _DETECT_BOOL(name, ...) \
 template <class, class= void> struct name: std::false_type {}; \
 template <class T> struct name<T, std::void_t<__VA_ARGS__>>: std::true_type {}; \
 template <class T> static constexpr bool name##_v= name<T>::value
#define _DETECT_TYPE(name, type1, type2, ...) \
 template <class T, class= void> struct name { \
  using type= type2; \
 }; \
 template <class T> struct name<T, std::void_t<__VA_ARGS__>> { \
  using type= type1; \
 }
#line 2 "src/Misc/rng.hpp"
#include <random>
#include <cstdint>
uint64_t rng() {
 static uint64_t x= 10150724397891781847ULL * std::random_device{}();
 return x^= x << 7, x^= x >> 9;
}
uint64_t rng(uint64_t lim) { return rng() % lim; }
int64_t rng(int64_t l, int64_t r) { return l + rng() % (r - l); }
#line 11 "src/DataStructure/RandomizedBinarySearchTree.hpp"
template <class M, bool reversible= false> class RandomizedBinarySearchTree {
 _DETECT_BOOL(semigroup, typename T::T, decltype(&T::op));
 _DETECT_BOOL(dual, typename T::T, typename T::E, decltype(&T::mp), decltype(&T::cp));
 _DETECT_BOOL(commute, typename T::commute);
 _DETECT_TYPE(nullptr_or_E, typename T::E, std::nullptr_t, typename T::E);
 _DETECT_TYPE(myself_or_T, typename T::T, T, typename T::T);
 using T= typename myself_or_T<M>::type;
 using E= typename nullptr_or_E<M>::type;
 using RBST= RandomizedBinarySearchTree;
 template <class D> struct NodeB {
  T val;
  D *l= nullptr, *r= nullptr;
  size_t sz= 0;
 };
 template <class D, bool du> struct NodeD: NodeB<D> {};
 template <class D> struct NodeD<D, 1>: NodeB<D> {
  E laz;
 };
 template <class D, bool sg, bool rev, bool com> struct NodeS: NodeD<D, dual_v<M>> {};
 template <class D, bool rev, bool com> struct NodeS<D, 1, rev, com>: NodeD<D, dual_v<M>> {
  T sum;
 };
 template <class D> struct NodeS<D, 1, 1, 0>: NodeD<D, dual_v<M>> {
  T sum, rsum;
 };
 struct Node: NodeS<Node, semigroup_v<M>, reversible, commute_v<M>> {
  size_t size() const {
   if constexpr (dual_v<M> || reversible) return this->sz & 0x3fffffff;
   else return this->sz;
  }
 };
 using np= Node *;
 np rt;
 template <bool sz= 1> static inline void update(np t) {
  if constexpr (sz) t->sz= 1;
  if constexpr (semigroup_v<M>) {
   t->sum= t->val;
   if constexpr (reversible && !commute_v<M>) t->rsum= t->sum;
  }
  if (t->l) {
   if constexpr (sz) t->sz+= t->l->size();
   if constexpr (semigroup_v<M>) {
    t->sum= M::op(t->l->sum, t->sum);
    if constexpr (reversible && !commute_v<M>) t->rsum= M::op(t->rsum, t->l->rsum);
   }
  }
  if (t->r) {
   if constexpr (sz) t->sz+= t->r->size();
   if constexpr (semigroup_v<M>) {
    t->sum= M::op(t->sum, t->r->sum);
    if constexpr (reversible && !commute_v<M>) t->rsum= M::op(t->r->rsum, t->rsum);
   }
  }
 }
 static inline void map(T &v, E x, int sz) {
  if constexpr (std::is_invocable_r_v<void, decltype(M::mp), T &, E, int>) M::mp(v, x, sz);
  else M::mp(v, x);
 }
 static inline void propagate(np t, const E &x) {
  if (!t) return;
  if (t->sz >> 31) M::cp(t->laz, x);
  else t->laz= x;
  if constexpr (semigroup_v<M>) {
   map(t->sum, x, t->size());
   if constexpr (reversible && !commute_v<M>) map(t->rsum, x, t->size());
  }
  map(t->val, x, 1), t->sz|= 0x80000000;
 }
 static inline void toggle(np t) {
  if (!t) return;
  if constexpr (semigroup_v<M> && !commute_v<M>) std::swap(t->sum, t->rsum);
  std::swap(t->l, t->r), t->sz^= 0x40000000;
 }
 static inline void push_prop(np t) {
  if (t->sz >> 31) propagate(t->l, t->laz), propagate(t->r, t->laz), t->sz^= 0x80000000;
 }
 static inline void push_tog(np t) {
  if (t->sz & 0x40000000) toggle(t->l), toggle(t->r), t->sz^= 0x40000000;
 }
 template <class S> static inline np build(size_t bg, size_t ed, const S &val) {
  if (bg == ed) return nullptr;
  size_t mid= bg + (ed - bg) / 2;
  np t= new Node;
  if constexpr (std::is_same_v<S, T>) t->val= val;
  else t->val= val[mid];
  return t->l= build(bg, mid, val), t->r= build(mid + 1, ed, val), update(t), t;
 }
 static inline void dump(typename std::vector<T>::iterator itr, np t) {
  if (!t) return;
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t sz= t->l ? t->l->size() : 0;
  *(itr + sz)= t->val, dump(itr, t->l), dump(itr + sz + 1, t->r);
 }
 static inline np merge(np l, np r) {
  if (!l) return r;
  if (!r) return l;
  if (size_t lsz= l->size(), rsz= r->size(); rng(lsz + rsz) < lsz) {
   if constexpr (dual_v<M>) push_prop(l);
   if constexpr (reversible) push_tog(l);
   return l->r= merge(l->r, r), update(l), l;
  } else {
   if constexpr (dual_v<M>) push_prop(r);
   if constexpr (reversible) push_tog(r);
   return r->l= merge(l, r->l), update(r), r;
  }
 }
 static inline std::pair<np, np> split(np t, size_t k) {
  if (!t) return {nullptr, nullptr};
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  if (size_t sz= t->l ? t->l->size() : 0; k <= sz) {
   auto [l, r]= split(t->l, k);
   t->l= r, update(t);
   return {l, t};
  } else {
   auto [l, r]= split(t->r, k - sz - 1);
   t->r= l, update(t);
   return {t, r};
  }
 }
 static inline T erase(np &t, size_t k) {
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t sz= t->l ? t->l->size() : 0;
  if (k == sz) {
   T v= t->val;
   return t= merge(t->l, t->r), v;
  } else {
   T v= k < sz ? erase(t->l, k) : erase(t->r, k - sz - 1);
   return update(t), v;
  }
 }
 static inline T prod(np t, size_t a, size_t b) {
  if (!a && b == t->size()) return t->sum;
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t l= t->l ? t->l->size() : 0, k= l + 1;
  if (b < k) return prod(t->l, a, b);
  if (a > l) return prod(t->r, a - k, b - k);
  T ret= t->val;
  if (a < l) ret= M::op(prod(t->l, a, l), ret);
  if (b > k) ret= M::op(ret, prod(t->r, 0, b - k));
  return ret;
 }
 static inline void apply(np t, size_t a, size_t b, const E &x) {
  if (!a && b == t->size()) return propagate(t, x);
  if constexpr (reversible) push_tog(t);
  push_prop(t);
  size_t l= t->l ? t->l->size() : 0, k= l + 1;
  if (b < k) apply(t->l, a, b, x);
  else if (a > l) apply(t->r, a - k, b - k, x);
  else {
   map(t->val, x, 1);
   if (a < l) apply(t->l, a, l, x);
   if (b > k) apply(t->r, 0, b - k, x);
  }
  if constexpr (semigroup_v<M>) update<0>(t);
 }
 static inline void set_val(np t, size_t k, const T &v) {
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t l= t->l ? t->l->size() : 0;
  if (k < l) set_val(t->l, k, v);
  else if (k > l) set_val(t->r, k - l - 1, v);
  else t->val= v;
  if constexpr (semigroup_v<M>) update<0>(t);
 }
 static inline void mul_val(np t, size_t k, const T &v) {
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t l= t->l ? t->l->size() : 0;
  if (k < l) mul_val(t->l, k, v);
  else if (k > l) mul_val(t->r, k - l - 1, v);
  else t->val= M::op(t->val, v);
  update<0>(t);
 }
 static inline T get_val(np t, size_t k) {
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t l= t->l ? t->l->size() : 0;
  if (k < l) return get_val(t->l, k);
  if (k > l) return get_val(t->r, k - l - 1);
  return t->val;
 }
 static inline T &at_val(np t, size_t k) {
  if constexpr (dual_v<M>) push_prop(t);
  if constexpr (reversible) push_tog(t);
  size_t l= t->l ? t->l->size() : 0;
  if (k < l) return at_val(t->l, k);
  if (k > l) return at_val(t->r, k - l - 1);
  return t->val;
 }
 static inline RBST np_to_rbst(np t) {
  RBST ret;
  return ret.rt= t, ret;
 }
public:
 RandomizedBinarySearchTree(): rt(nullptr) {}
 RandomizedBinarySearchTree(size_t n, T val= T()): rt(n ? build(0, n, val) : nullptr) {}
 RandomizedBinarySearchTree(const T *bg, const T *ed): rt(bg == ed ? nullptr : build(0, ed - bg, bg)) {}
 RandomizedBinarySearchTree(const std::vector<T> &ar): RandomizedBinarySearchTree(ar.data(), ar.data() + ar.size()) {}
 size_t size() const { return rt ? rt->size() : 0; }
 void clear() { rt= nullptr; }
 std::vector<T> dump() {
  if (!rt) return std::vector<T>();
  std::vector<T> ret(size());
  return dump(ret.begin(), rt), ret;
 }
 static std::string which_unavailable() {
  std::string ret= "";
  if constexpr (semigroup_v<M>) ret+= "\"at\" ";
  else ret+= "\"prod\" ";
  if constexpr (!semigroup_v<M> || !commute_v<M>) ret+= "\"mul\" ";
  if constexpr (!dual_v<M>) ret+= "\"apply\" ";
  if constexpr (!reversible) ret+= "\"reverse\" ";
  return ret;
 }
 RBST &operator+=(RBST r) { return rt= merge(rt, r.rt), *this; }
 RBST operator+(RBST r) const { return RBST(*this)+= r; }
 std::pair<RBST, RBST> split(size_t k) {
  assert(k <= size());
  auto [l, r]= split(rt, k);
  return {np_to_rbst(l), np_to_rbst(r)};
 }
 std::tuple<RBST, RBST, RBST> split3(size_t a, size_t b) {
  assert(a < b), assert(b <= size());
  auto [tmp, r]= split(rt, b);
  auto [l, c]= split(tmp, a);
  return {np_to_rbst(l), np_to_rbst(c), np_to_rbst(r)};
 }
 void push_back(const T &v) {
  np t= new Node;
  t->val= v, update(t), rt= merge(rt, t);
 }
 void push_front(const T &v) {
  np t= new Node;
  t->val= v, update(t), rt= merge(t, rt);
 }
 void insert(size_t k, const T &v) {
  auto [l, r]= split(rt, k);
  rt= new Node, rt->val= v, update(rt), rt= merge(merge(l, rt), r);
 }
 T pop_back() {
  auto [l, t]= split(rt, size() - 1);
  return rt= l, t->val;
 }
 T pop_front() {
  auto [t, r]= split(rt, 1);
  return rt= r, t->val;
 }
 T erase(size_t k) { return assert(k < size()), erase(rt, k); }
 void set(size_t k, const T &val) { set_val(rt, k, val); }
 void mul(size_t k, const T &val) {
  static_assert(semigroup_v<M> && commute_v<M>, "\"mul\" is not available\n");
  mul_val(rt, k, val);
 }
 T get(size_t k) { return get_val(rt, k); }
 T &at(size_t k) {
  static_assert(!semigroup_v<M>, "\"at\" is not available\n");
  return at_val(rt, k);
 }
 template <class L= M> std::enable_if_t<semigroup_v<L>, T> operator[](size_t k) { return get(k); }
 template <class L= M> std::enable_if_t<!semigroup_v<L>, T> &operator[](size_t k) { return at(k); }
 T prod(size_t a, size_t b) {
  static_assert(semigroup_v<M>, "\"prod\" is not available\n");
  return prod(rt, a, b);
 }
 void apply(size_t a, size_t b, E x) {
  static_assert(dual_v<M>, "\"apply\" is not available\n");
  apply(rt, a, b, x);
 }
 void reverse() {
  static_assert(reversible, "\"reverse\" is not available\n");
  toggle(rt);
 }
 void reverse(size_t a, size_t b) {
  static_assert(reversible, "\"reverse\" is not available\n");
  assert(a < b), assert(b <= size());
  auto [tmp, r]= split(rt, b);
  auto [l, c]= split(tmp, a);
  toggle(c), rt= merge(merge(l, c), r);
 }
};
#line 10 "test/sample_test/code_festival_exhibition_b.RBST.test.cpp"
using namespace std;
bool test(int (*solve)(stringstream&, stringstream&), string in, string expected) {
 stringstream scin(in), scout;
 solve(scin, scout);
 return scout.str() == expected;
}
namespace TEST {
struct M {
 using T= array<int, 2>;
 static T op(const T& l, const T& r) {
  if (l[1] > r[0]) return {l[0], r[1] + l[1] - r[0]};
  else return {l[0] + r[0] - l[1], r[1]};
 }
};
signed main(stringstream& scin, stringstream& scout) {
 using RBST= RandomizedBinarySearchTree<M>;
 int Q;
 scin >> Q;
 RBST rbst;
 {
  string S;
  scin >> S;
  int N= S.length();
  vector<typename M::T> v(N);
  for (int i= N; i--;) v[i]= {S[i] == ')', S[i] == '('};
  rbst= RBST(v);
 }
 while (Q--) {
  char x;
  int y, z;
  scin >> x >> y >> z;
  --y;
  if (x == '(') {
   rbst.insert(y, {0, 1});
  } else if (x == ')') {
   rbst.insert(y, {1, 0});
  } else if (x == 'D') {
   rbst.erase(y);
  } else {
   auto [l, r]= rbst.prod(y, z);
   scout << l + r << '\n';
  }
 }
 return 0;
}
}
signed main() {
 assert(test(TEST::main, "5\n(()\nD 1 0\n( 3 0\nQ 1 2\n) 1 0\nQ 2 4\n", "0\n1\n"));
 assert(test(TEST::main,
             "11\n"
             "(()()(()\n"
             "( 1 0\n"
             "( 1 0\n"
             "D 4 0\n"
             "Q 2 6\n"
             ") 5 0\n"
             "D 8 0\n"
             "Q 1 9\n"
             "( 3 0\n"
             ") 8 0\n"
             "D 10 0\n"
             "Q 5 10\n",
             "1\n1\n4\n"));
 return 0;
}
Back to top page