import scala.io.StdIn.readLine import scala.reflect.ClassTag class BinaryIndexedTree[T: Numeric: ClassTag](val size: Int): private val array = Array.fill(size){summon[Numeric[T]].zero} def get(position: Int): T = var result = summon[Numeric[T]].zero var pos = position while pos >= 0 do result = summon[Numeric[T]].plus(result, array(pos)) pos -= ~pos & (pos + 1) result def add(position: Int, value: T) = var pos = position while pos < size do array(pos) = summon[Numeric[T]].plus(array(pos), value) pos += ~pos & (pos + 1) @main def main = val Array(n, q) = readLine().split(' ').map(_.toInt) val s = readLine().toCharArray val queries = Array.fill(q){ readLine().split(' ').map(_.toInt) } val bit = BinaryIndexedTree[Int](n) for i <- 1 until n do if s(i - 1) == '(' && s(i) == ')' then bit.add(i, 1) for query <- queries do query match case Array(1, _i) => val i = _i - 1 s(i) match case '(' => s(i) = ')' if i > 0 && s(i - 1) == '(' then bit.add(i, 1) if i + 1 < n && s(i + 1) == ')' then bit.add(i + 1, -1) case ')' => s(i) = '(' if i > 0 && s(i - 1) == '(' then bit.add(i, -1) if i + 1 < n && s(i + 1) == ')' then bit.add(i + 1, 1) case _ => ??? case Array(2, _l, _r) => val l = _l - 1 val r = _r - 1 val sum = bit.get(r) - bit.get(l) println(sum)