//! # Bundled libraries //! //! - `input_i_scanner 0.1.0 (git+https://github.com/ia7ck/rust-competitive-programming#b60dc67706797611e680510aa6492f6397a2e104)` licensed under **missing** as `crate::__cargo_equip::crates::input_i_scanner` pub use __cargo_equip::prelude::*; use input_i_scanner::InputIScanner; use std::cmp::Reverse; use std::collections::{BTreeSet, HashMap}; fn main() { let stdin = std::io::stdin(); let mut _i_i = InputIScanner::from(stdin.lock()); macro_rules! scan { (($($t: ty),+)) => { ($(scan!($t)),+) }; ($t: ty) => { _i_i.scan::<$t>() as $t }; (($($t: ty),+); $n: expr) => { std::iter::repeat_with(|| scan!(($($t),+))).take($n).collect::>() }; ($t: ty; $n: expr) => { std::iter::repeat_with(|| scan!($t)).take($n).collect::>() }; } let (n, q) = scan!((usize, usize)); let s = scan!(String); let s: Vec = s.chars().collect(); let mut cum = vec![0; n + 1]; let mut stack = Vec::new(); let mut lr_pos = HashMap::new(); let mut rl_pos = HashMap::new(); let mut lr = BTreeSet::new(); for (i, &ch) in s.iter().enumerate() { if ch == '(' { cum[i + 1] += 1; stack.push(i + 1); } else { cum[i + 1] -= 1; let j = stack.pop().unwrap(); let opt = lr_pos.insert(j, i + 1); assert!(opt.is_none()); let opt = rl_pos.insert(i + 1, j); assert!(opt.is_none()); lr.insert((j, Reverse(i + 1))); } } assert!(stack.is_empty()); for i in 1..=n { cum[i] += cum[i - 1]; } assert_eq!(cum[n], 0); for _ in 0..q { let (x, y) = scan!((usize, usize)); let xx = if s[x - 1] == '(' { lr_pos[&x] } else { rl_pos[&x] }; let yy = if s[y - 1] == '(' { lr_pos[&y] } else { rl_pos[&y] }; let mut positions = [x, xx, y, yy]; positions.sort(); assert_eq!(s[positions[0] - 1], '('); assert_eq!(s[positions[3] - 1], ')'); if s[positions[1] - 1] == '(' { // (...(...)...) let ans_l = positions[0]; let ans_r = lr_pos[&ans_l]; println!("{} {}", ans_l, ans_r); } else { // (...)...(...) if let Some((ans_l, Reverse(ans_r))) = lr.range(..(positions[0], Reverse(positions[3]))).last() { println!("{} {}", ans_l, ans_r); } else { println!("-1"); } } } } // The following code was expanded by `cargo-equip`. #[rustfmt::skip] #[allow(unused)] mod __cargo_equip { pub(crate) mod crates { pub mod input_i_scanner {use std::fmt;use std::io;use std::str;pub struct InputIScanner{r:R,l:String,i:usize,}implInputIScanner{pub fn new(reader:R)->Self{Self{r:reader,l:String::new(),i:0,}}pub fn scan(&mut self)->T where T:str::FromStr,::Err:fmt::Debug,{self.skip_blanks();assert!(self.i{self.i+=j;break;}None=>{let mut buf=String::new();let num_bytes=self.r.read_line(&mut buf).unwrap_or_else(|_|panic!("invalid UTF-8"));assert!(num_bytes>0,"reached EOF :(");self.l=buf.trim_end_matches('\n').trim_end_matches('\r').to_string();self.i=0;}}}}}impl<'a>From<&'a str>for InputIScanner<&'a[u8]>{fn from(s:&'a str)->Self{Self::new(s.as_bytes())}}impl<'a>From >for InputIScanner > >{fn from(stdin:io::StdinLock<'a>)->Self{Self::new(io::BufReader::new(stdin))}}} } pub(crate) mod macros { pub mod input_i_scanner {} } pub(crate) mod prelude {pub use crate::__cargo_equip::crates::*;} mod preludes { pub mod input_i_scanner {} } }