結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー ikdikd
提出日時 2021-12-07 22:10:39
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 4,418 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 169,668 KB
実行使用メモリ 10,920 KB
最終ジャッジ日時 2023-09-22 09:26:30
合計ジャッジ時間 13,748 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 326 ms
4,380 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 1 ms
4,380 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 477 ms
9,328 KB
testcase_24 AC 477 ms
9,676 KB
testcase_25 AC 507 ms
9,676 KB
testcase_26 AC 390 ms
10,920 KB
testcase_27 AC 343 ms
10,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//! # 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::collections::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::<Vec<_>>()
        };
        ($t: ty; $n: expr) => {
            std::iter::repeat_with(|| scan!($t)).take($n).collect::<Vec<_>>()
        };
    }

    let (n, q) = scan!((usize, usize));
    let s = scan!(String);
    let s: Vec<char> = std::iter::once('^').chain(s.chars()).collect();

    let mut cum = vec![0; n + 1];
    let mut l_parens = Vec::new();
    let mut stack = Vec::new();
    let mut lr_pos = HashMap::new();
    let mut rl_pos = HashMap::new();
    for i in 1..=n {
        if s[i] == '(' {
            cum[i] += 1;
            l_parens.push(i);
            stack.push(i);
        } else {
            cum[i] -= 1;
            let j = stack.pop().unwrap();
            let opt = lr_pos.insert(j, i);
            assert!(opt.is_none());
            let opt = rl_pos.insert(i, j);
            assert!(opt.is_none());
        }
    }
    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] == '(' { lr_pos[&x] } else { rl_pos[&x] };
        let yy = if s[y] == '(' { lr_pos[&y] } else { rl_pos[&y] };
        let mut positions = [x, xx, y, yy];
        positions.sort();
        assert_eq!(s[positions[0]], '(');
        assert_eq!(s[positions[3]], ')');
        if s[positions[1]] == '(' {
            // (...(...)...)
            let ans_l = positions[0];
            let ans_r = lr_pos[&ans_l];
            println!("{} {}", ans_l, ans_r);
        } else {
            // (...)...(...)
            let mut ok = 0;
            let mut ng = l_parens.len();
            while ok + 1 < ng {
                let mid = (ok + ng) / 2;
                let r = lr_pos[&l_parens[mid]];
                if l_parens[mid] <= positions[0] && positions[3] <= r {
                    ok = mid;
                } else {
                    ng = mid;
                }
            }
            let r = lr_pos[&l_parens[ok]];
            if ok <= positions[0] && positions[3] <= r {
                println!("{} {}", l_parens[ok], 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:R,l:String,i:usize,}impl<R:io::BufRead>InputIScanner<R>{pub fn new(reader:R)->Self{Self{r:reader,l:String::new(),i:0,}}pub fn scan<T>(&mut self)->T where T:str::FromStr,<T as str::FromStr>::Err:fmt::Debug,{self.skip_blanks();assert!(self.i<self.l.len());assert_ne!(&self.l[self.i..=self.i]," ");let rest=&self.l[self.i..];let len=rest.find(' ').unwrap_or_else(| |rest.len());let val=rest[..len].parse().unwrap_or_else(|e|panic!("{:?}, attempt to read `{}`",e,rest));self.i+=len;val}fn skip_blanks(&mut self){loop{match self.l[self.i..].find(|ch|ch!=' '){Some(j)=>{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<io::StdinLock<'a> >for InputIScanner<io::BufReader<io::StdinLock<'a> > >{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 {}
    }
}
0