fn main() { let stdin = std::io::read_to_string(std::io::stdin().lock()).unwrap(); let mut stdin = stdin.split_ascii_whitespace(); unsafe { read!(stdin -> (_n: u32, q: u32, s: String, queries: Vec[(char, u32, u32); q])); write!(output(solve(s, queries))); } } fn solve(s: String, queries: Vec<(char, u32, u32)>) -> Vec { let mut st = mylib::SegmentTree::::from( s.bytes() .map(|s| match s == b'(' { true => (0, 1), false => (1, 0), }) .collect::>(), ); queries .into_iter() .filter_map(|(tp, l, r)| { if tp == '1' { st.update( (l - 1) as usize, match r == 1 { true => (0, 1), false => (1, 0), }, ); None } else { let (rem_l, rem_r) = st.range_pick_up((l - 1) as usize, r as usize); Some(r - (l - 1) - rem_l - rem_r) } }) .collect() } fn output(ans: Vec) -> String { format_vec!(ans, "\n", (x) -> ("{}", x)) } struct S; impl mylib::Monoid for S { type T = (u32, u32); const DEFAULT: Self::T = (0, 0); fn op(a: &Self::T, b: &Self::T) -> Self::T { match a.1 >= b.0 { true => (a.0, a.1 - b.0 + b.1), false => (a.0 + b.0 - a.1, b.1), } } } mod mylib { pub struct SegmentTree { container: Vec>, } pub trait Monoid { type T: Clone; const DEFAULT: Self::T; fn op(a: &Self::T, b: &Self::T) -> Self::T; } impl SegmentTree { pub fn update(&mut self, index: usize, value: S::T) { self.container[0][index] = value; for layer in 1..self.container.len() { self.container[layer][index >> layer] = S::op( &self.container[layer - 1][(index >> layer) << 1], &self.container[layer - 1][((index >> layer) << 1) | 1], ); } } pub fn range_pick_up(&self, mut l: usize, mut r: usize) -> S::T { let mut ans_l = S::DEFAULT; let mut ans_r = S::DEFAULT; for layer in 0..self.container.len() { if l >= r { break; } if (l & 1) == 1 { ans_l = S::op(&ans_l, &self.container[layer][l]); l += 1; } if (r & 1) == 1 { ans_r = S::op(&self.container[layer][r - 1], &ans_r); // r -= 1; } l >>= 1; r >>= 1; } S::op(&ans_l, &ans_r) } #[allow(unused)] pub fn len(&self) -> usize { self.container.first().unwrap_or(&vec![]).len() } #[allow(unused)] pub fn layer(&self) -> usize { self.container.len() } } impl From> for SegmentTree { fn from(mut value: Vec) -> Self { let mut st = Self { container: Vec::>::with_capacity(30), }; if value.is_empty() { return st; } value.resize(value.len().next_power_of_two(), S::DEFAULT); st.container.push(value); while st.container.last().unwrap().len() > 1 { st.container.push( st.container .last() .unwrap() .chunks_exact(2) .map(|x| S::op(&x[0], &x[1])) .collect(), ); } st } } impl Into> for SegmentTree { fn into(mut self) -> Vec { self.container.swap_remove(0) } } impl std::ops::Index for SegmentTree { type Output = S::T; fn index(&self, index: usize) -> &Self::Output { &self.container[0][index] } } } #[macro_export] macro_rules! read { ($iter:ident -> ($v:ident : $t1:tt $([$($t2:tt)+] $({$($t3:tt)+})?)?)) => { let $v = read_value!($iter -> $t1 $([$($t2)+] $({$($t3)+})? )?); }; ($iter:ident -> ($v:ident : $t1:tt $([$($t2:tt)+] $({$($t3:tt)+})?)? , $($r:tt)*)) => { read!($iter -> ($v : $t1 $([$($t2)+] $({$($t3)+})?)?)); read!($iter -> ($($r)*)); }; } #[macro_export] macro_rules! read_value { ($source:ident -> ($($t1:tt $([$($t2:tt)+])?),+)) => { ( $(read_value!($source -> $t1 $([$($t2)+])?)),* ) }; ($source:ident -> [ $t1:tt ; $len:expr ]) => { { let mut x: [::std::mem::MaybeUninit<$t1>; $len] = ::std::mem::MaybeUninit::uninit().assume_init(); for elem in x.iter_mut() { elem.as_mut_ptr().write(read_value!($source -> $t1)); } ::std::mem::transmute::<[::std::mem::MaybeUninit<$t1>; $len], [$t1; $len]>(x) } }; ($source:ident -> [ $c2:tt [ $t1:tt $(; $len2:expr)? ] ; $len1:expr ]) => { { let mut x: [::std::mem::MaybeUninit<$c2<$t1>>; $len1] = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() }; for elem in x.iter_mut() { elem.as_mut_ptr().write(read_value!($source -> $c2 [ $t1 $(; $len2)? ])); } ::std::mem::transmute::<[::std::mem::MaybeUninit<$c2<$t1>>; $len1], [$c2<$t1>; $len1]>(x) } }; ($source:ident -> $t1:tt[ $t2:tt $([$($t3:tt)+])? ; $len:expr ]) => { (0..($len)).map(|_| read_value!($source -> $t2 $([$($t3)+])?)).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ $t2:tt $([$($t3:tt)+])? ]) => { (0..(read_value!($source -> u32))).map(|_| read_value!($source -> $t2 $([$($t3)+])?)).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ ($($t2:tt),+) ; $len:expr ] { $($p1:pat => ($($pos:tt),*)),* }) => { (0..($len)).map(|_| { let mut v = ($($t2::default()),+); v.0 = my_parser::parse_without_checking(($source).next().unwrap()); match v.0 { $($p1 => { $(v.$pos = my_parser::parse_without_checking(($source).next().unwrap()));* }),* _ => unreachable!(), } v }).collect::<$t1<_>>() }; ($source:ident -> $t1:tt[ ($($t2:tt),+) ] { $($p1:pat => ($($pos:tt),*)),* }) => { read_value!($source -> $t1[ ($($t2),+) ; read_value!($source -> u32) ] { $($p1 => ($($pos),*)),* }) }; ($source:ident -> $t:ty) => { my_parser::parse_without_checking::<$t>(($source).next().unwrap()) }; } mod my_parser { #[allow(unused)] pub unsafe fn parse_without_checking(target: &str) -> F { unsafe { Parsable::from_str(target) } } pub trait Parsable { unsafe fn from_str(s: &str) -> Self; } impl Parsable for String { unsafe fn from_str(s: &str) -> Self { Self::from(s) } } impl Parsable for char { unsafe fn from_str(s: &str) -> Self { s.chars().next().unwrap() } } macro_rules! parse_float { ($s:ident) => {{ let mut iter = $s.bytes().peekable(); let sign = match iter.peek().unwrap() { b'-' => { iter.next(); -1.0 } b'+' => { iter.next(); 1.0 } _ => 1.0, }; let mut result = 0.0; while let Some(cur) = iter.next() && cur != b'.' { result = result * 10.0 + (cur - b'0') as Self; } let mut digit = 1.0; (result + iter .map(|cur| { digit *= 0.1; digit * (cur - b'0') as Self }) .sum::()) * sign }}; } impl Parsable for u8 { unsafe fn from_str(s: &str) -> Self { ((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u32)) & 0x0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) as Self } } impl Parsable for u16 { unsafe fn from_str(s: &str) -> Self { ((((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u64)) & 0x0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) as Self } } impl Parsable for u32 { unsafe fn from_str(s: &str) -> Self { ((((((((s.bytes().fold(0, |acc, x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self } } impl Parsable for u64 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u64; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; s.as_bytes().chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self }) } } impl Parsable for u128 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u128; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; s.as_bytes().chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as Self }) } } impl Parsable for i8 { unsafe fn from_str(s: &str) -> Self { ((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u32)) & 0x0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) as i32) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i16 { unsafe fn from_str(s: &str) -> Self { ((((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u64)) & 0x0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) as i64) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i32 { unsafe fn from_str(s: &str) -> Self { ((((((((((s .bytes() .skip(match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }) .fold(0, |acc, x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as i128) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i64 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u64; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; let skip = match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }; ((s.as_bytes()[skip..].chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) as u64 }) as i64) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for i128 { unsafe fn from_str(s: &str) -> Self { const POW_10: [u128; 17] = [ 1, 10, 100, 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000, 10_000_000_000, 100_000_000_000, 1_000_000_000_000, 10_000_000_000_000, 100_000_000_000_000, 1_000_000_000_000_000, 10_000_000_000_000_000, ]; let skip = match s.as_bytes()[0].is_ascii_digit() { true => 0, false => 1, }; ((s.as_bytes()[skip..].chunks(16).fold(0, |acc, x| { acc * POW_10[x.len()] + ((((((((x.into_iter().fold(0, |acc, &x| (acc << 8) | (x as u128)) & 0x0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f) .wrapping_mul((1 << 8) + 10) >> 8) & 0x00ff00ff00ff00ff00ff00ff00ff00ff) .wrapping_mul((1 << 16) + 100) >> 16) & 0x0000ffff0000ffff0000ffff0000ffff) .wrapping_mul((1 << 32) + 10000) >> 32) & 0x00000000ffffffff00000000ffffffff) .wrapping_mul((1 << 64) + 100000000) >> 64) }) as i128) * match s.as_bytes()[0] == b'-' { true => -1, false => 1, }) as Self } } impl Parsable for f32 { unsafe fn from_str(s: &str) -> Self { parse_float!(s) } } impl Parsable for f64 { unsafe fn from_str(s: &str) -> Self { parse_float!(s) } } } #[macro_export] macro_rules! write { ($out:expr) => {{ use std::io::Write; std::io::stdout() .lock() .write_all(($out).as_bytes()) .unwrap(); }}; } #[macro_export] macro_rules! format_iter { ($i:expr, $sep:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ let mut iter = $i; #[allow(unused_parens)] let ($($elem),+) = iter.next().unwrap(); #[allow(unused_parens)] iter.fold(std::format!($form $(, $ex)*), |mut acc, ($($elem),+)| { use std::fmt::Write; acc.push_str($sep); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) }} } #[macro_export] macro_rules! format_vec { ($v:expr, $sep:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ if $v.is_empty() { String::new() } else { let mut v = $v; #[allow(unused_parens)] let ($($elem),+) = v.drain(..1).next().unwrap(); #[allow(unused_parens)] v.into_iter().fold(std::format!($form $(, $ex)*), |mut acc, ($($elem),+)| { use std::fmt::Write; acc.push_str($sep); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) } }} } #[macro_export] macro_rules! format_vec_vec { ($v:expr, $sep1:expr, $sep2:expr, ($($elem:ident),+) -> ($form:expr $(, $ex:expr)*)) => {{ let mut v = $v; let v_first = v.drain(..1).next().unwrap(); #[allow(unused_parens)] v.into_iter().fold(format_vec!(v_first, $sep2, ($($elem),+) -> ($form $(, $ex)*)), |mut acc, mut v| { use std::fmt::Write; acc.push_str($sep1); let ($($elem),+) = v.drain(..1).next().unwrap(); std::write!(&mut acc, $form $(, $ex)*).unwrap(); v.into_iter().fold(acc, |mut acc, ($($elem),+)| { acc.push_str($sep2); std::write!(&mut acc, $form $(, $ex)*).unwrap(); acc }) }) }} }