結果

問題 No.2308 [Cherry 5th Tune B] もしかして、真?
ユーザー haihamabossuhaihamabossu
提出日時 2023-05-19 22:45:57
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 265 ms / 2,000 ms
コード長 5,655 bytes
コンパイル時間 25,588 ms
コンパイル使用メモリ 379,428 KB
実行使用メモリ 24,060 KB
最終ジャッジ日時 2024-05-10 06:14:56
合計ジャッジ時間 23,601 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 204 ms
7,872 KB
testcase_02 AC 227 ms
14,448 KB
testcase_03 AC 206 ms
12,328 KB
testcase_04 AC 211 ms
12,904 KB
testcase_05 AC 215 ms
13,936 KB
testcase_06 AC 231 ms
19,644 KB
testcase_07 AC 227 ms
18,984 KB
testcase_08 AC 238 ms
20,836 KB
testcase_09 AC 240 ms
21,172 KB
testcase_10 AC 217 ms
12,696 KB
testcase_11 AC 227 ms
14,352 KB
testcase_12 AC 228 ms
12,976 KB
testcase_13 AC 228 ms
12,904 KB
testcase_14 AC 227 ms
12,912 KB
testcase_15 AC 227 ms
12,940 KB
testcase_16 AC 228 ms
14,352 KB
testcase_17 AC 228 ms
14,360 KB
testcase_18 AC 227 ms
14,364 KB
testcase_19 AC 227 ms
14,376 KB
testcase_20 AC 227 ms
14,368 KB
testcase_21 AC 259 ms
22,028 KB
testcase_22 AC 263 ms
22,028 KB
testcase_23 AC 256 ms
22,032 KB
testcase_24 AC 259 ms
22,044 KB
testcase_25 AC 260 ms
22,016 KB
testcase_26 AC 260 ms
22,060 KB
testcase_27 AC 258 ms
22,144 KB
testcase_28 AC 258 ms
24,060 KB
testcase_29 AC 265 ms
23,936 KB
testcase_30 AC 258 ms
22,028 KB
testcase_31 AC 198 ms
21,416 KB
testcase_32 AC 198 ms
21,384 KB
testcase_33 AC 199 ms
21,252 KB
testcase_34 AC 203 ms
22,044 KB
testcase_35 AC 202 ms
22,040 KB
testcase_36 AC 202 ms
22,152 KB
testcase_37 AC 18 ms
5,376 KB
testcase_38 AC 223 ms
12,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://github.com/rust-lang-ja/ac-library-rs

pub mod fenwicktree {
    use std::ops::{Bound, RangeBounds};

    // Reference: https://en.wikipedia.org/wiki/Fenwick_tree
    pub struct FenwickTree<T> {
        n: usize,
        ary: Vec<T>,
        e: T,
    }

    impl<T: Clone + std::ops::AddAssign<T>> FenwickTree<T> {
        pub fn new(n: usize, e: T) -> Self {
            FenwickTree {
                n,
                ary: vec![e.clone(); n],
                e,
            }
        }
        pub fn accum(&self, mut idx: usize) -> T {
            let mut sum = self.e.clone();
            while idx > 0 {
                sum += self.ary[idx - 1].clone();
                idx &= idx - 1;
            }
            sum
        }
        /// performs data[idx] += val;
        pub fn add<U: Clone>(&mut self, mut idx: usize, val: U)
        where
            T: std::ops::AddAssign<U>,
        {
            let n = self.n;
            idx += 1;
            while idx <= n {
                self.ary[idx - 1] += val.clone();
                idx += idx & idx.wrapping_neg();
            }
        }
        /// Returns data[l] + ... + data[r - 1].
        pub fn sum<R>(&self, range: R) -> T
        where
            T: std::ops::Sub<Output = T>,
            R: RangeBounds<usize>,
        {
            let r = match range.end_bound() {
                Bound::Included(r) => r + 1,
                Bound::Excluded(r) => *r,
                Bound::Unbounded => self.n,
            };
            let l = match range.start_bound() {
                Bound::Included(l) => *l,
                Bound::Excluded(l) => l + 1,
                Bound::Unbounded => return self.accum(r),
            };
            self.accum(r) - self.accum(l)
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use std::ops::Bound::*;

        #[test]
        fn fenwick_tree_works() {
            let mut bit = FenwickTree::new(5, 0i64);
            // [1, 2, 3, 4, 5]
            for i in 0..5 {
                bit.add(i, i as i64 + 1);
            }
            assert_eq!(bit.sum(0..5), 15);
            assert_eq!(bit.sum(0..4), 10);
            assert_eq!(bit.sum(1..3), 5);

            assert_eq!(bit.sum(..), 15);
            assert_eq!(bit.sum(..2), 3);
            assert_eq!(bit.sum(..=2), 6);
            assert_eq!(bit.sum(1..), 14);
            assert_eq!(bit.sum(1..=3), 9);
            assert_eq!(bit.sum((Excluded(0), Included(2))), 5);
        }
    }
}
use fenwicktree::*;

pub mod scanner {

    pub struct Scanner {
        buf: Vec<String>,
    }

    impl Scanner {
        pub fn new() -> Self {
            Self { buf: vec![] }
        }

        pub fn new_from(source: &str) -> Self {
            let source = String::from(source);
            let buf = Self::split(source);
            Self { buf }
        }

        pub fn next<T: std::str::FromStr>(&mut self) -> T {
            loop {
                if let Some(x) = self.buf.pop() {
                    return x.parse().ok().expect("");
                }
                let mut source = String::new();
                std::io::stdin().read_line(&mut source).expect("");
                self.buf = Self::split(source);
            }
        }

        fn split(source: String) -> Vec<String> {
            source
                .split_whitespace()
                .rev()
                .map(String::from)
                .collect::<Vec<_>>()
        }
    }
}

use std::ops::Deref;

use crate::scanner::Scanner;
use crate::FenwickTree;

fn main() {
    let mut scanner = Scanner::new();
    let t: usize = scanner.next();
    for _ in 0..t {
        solve(&mut scanner);
    }
}

fn solve(scanner: &mut Scanner) {
    let n: usize = scanner.next();
    let mut x: Vec<bool> = (0..n)
        .map(|_| {
            let s = scanner.next::<String>();
            s.deref() == "True"
        })
        .collect();
    let y: Vec<usize> = (0..n - 1)
        .map(|_| {
            let s = scanner.next::<String>();
            match s.deref() {
                "and" => 0,
                "or" => 1,
                "xor" => 2,
                _ => 3,
            }
        })
        .collect();
    let s: Vec<usize> = (0..n - 1).map(|_| scanner.next::<usize>()).collect();
    let mut bit = FenwickTree::new(n, 0i64);
    for i in 0..n {
        bit.add(i, 1);
    }
    let cont = |ok: usize, ng: usize| -> bool {
        let l = ok.min(ng);
        let r = ok.max(ng);
        l + 1 < r
    };
    for &si in s.iter() {
        let si = si as i64;
        let i: usize;
        {
            let mut ok: usize = 0;
            let mut ng: usize = n + 1;
            while cont(ok, ng) {
                let x = (ok + ng) / 2;
                if bit.sum(0..x) < si {
                    ok = x;
                } else {
                    ng = x;
                }
            }
            i = ok;
        }
        let j: usize;
        {
            let mut ok: usize = 0;
            let mut ng: usize = n + 1;
            while cont(ok, ng) {
                let x = (ok + ng) / 2;
                if bit.sum(0..x) < si + 1 {
                    ok = x;
                } else {
                    ng = x;
                }
            }
            j = ok;
        }
        bit.add(i, -1);
        match y[i] {
            0 => x[j] = x[i] && x[j],
            1 => x[j] = x[i] || x[j],
            2 => x[j] = x[i] ^ x[j],
            _ => {
                if !x[i] {
                    x[j] = true
                }
            }
        }
    }
    if x[n - 1] {
        println!("True");
    } else {
        println!("False");
    }
}
0