#![allow(non_snake_case)] #![allow(unused_imports)] #![allow(unused_macros)] #![allow(clippy::needless_range_loop)] #![allow(clippy::comparison_chain)] #![allow(clippy::nonminimal_bool)] #![allow(clippy::neg_multiply)] #![allow(dead_code)] // use itertools::Itertools; // use superslice::Ext; use std::cmp::Reverse; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, VecDeque}; #[derive(Default)] struct Solver {} impl Solver { fn solve(&mut self) { input! { T: usize, } let f = |a: bool, b: bool, e: &String| -> bool { if e.as_str() == "and" { a & b } else if e.as_str() == "or" { a | b } else if e.as_str() == "xor" { a ^ b } else { !a | b } }; for _ in 0..T { input! { N: usize, X: [String; N], Y: [String; N - 1], S: [usize; N - 1] } let mut X: Vec = X.iter().map(|x| x.as_str() == "True").collect(); // let mut set_u = BinaryIndexedTree::new(N); // set_u.build(&vec![1; N]); // let mut set_v = BinaryIndexedTree::new(N - 1); // set_v.build(&vec![1; N - 1]); let mut set_u = BinaryIndexedTree::new(1); set_u.add(0, 1); for _ in 0..N - 1 { set_u.push(1); } let mut set_v = BinaryIndexedTree::new(0); for _ in 0..N - 1 { set_v.push(1); } let mut ans = true; for &s in &S { let pos_u0 = set_u.lower_bound(s as isize) as usize; let pos_u1 = set_u.lower_bound(s as isize + 1) as usize; let pos_v = set_v.lower_bound(s as isize) as usize; ans = f(X[pos_u0], X[pos_u1], &Y[pos_v]); X[pos_u0] = ans; set_u.add(pos_u1, -1); set_v.add(pos_v, -1); } if ans { println!("True"); } else { println!("False"); } } } } #[derive(Debug, Clone)] struct BinaryIndexedTree { size: usize, data: Vec, } impl BinaryIndexedTree { fn new(n: usize) -> Self { BinaryIndexedTree { size: n, data: vec![0; n], } } fn lsb(&self, i: usize) -> usize { i & i.wrapping_neg() } fn build(&mut self, v: &[isize]) { assert_eq!(self.size, v.len(), "size not correct!"); self.data = v.to_vec(); for i in 1..=self.size { let lsb = self.lsb(i); if i + lsb <= self.size { self.data[i + lsb - 1] += self.data[i - 1]; } } } fn push(&mut self, mut x: isize) { self.size += 1; let mut d = 1; let k = self.lsb(self.size); while d != k { x += self.data[self.size - d - 1]; d <<= 1; } self.data.push(x); } fn add(&mut self, i: usize, x: isize) { let mut idx = i + 1; while idx <= self.size { self.data[idx - 1] += x; idx += self.lsb(idx); } } // [0, r) fn sum(&self, i: usize) -> isize { let mut ret = 0; let mut idx = i; while idx > 0 { ret += self.data[idx - 1]; idx -= self.lsb(idx); } ret } // [l, r) fn range_sum(&self, l: usize, r: usize) -> isize { self.sum(r) - self.sum(l) } fn lower_bound(&self, x: isize) -> isize { let mut i = 0; let mut k = 1; let mut x = x; while k <= self.size { k <<= 1; } while k > 0 { if i + k <= self.size && self.data[i + k - 1] < x { x -= self.data[i + k - 1]; i += k; } k >>= 1; } if x > 0 { i as isize } else { -1 } } fn upper_bound(&self, x: isize) -> isize { let mut i = 0; let mut k = 1; let mut x = x; while k <= self.size { k <<= 1; } while k > 0 { if i + k <= self.size && self.data[i + k - 1] <= x { x -= self.data[i + k - 1]; i += k; } k >>= 1; } if i < self.size { i as isize } else { -1 } } } #[macro_export] macro_rules! max { ($x: expr) => ($x); ($x: expr, $( $y: expr ),+) => { std::cmp::max($x, max!($( $y ),+)) } } #[macro_export] macro_rules! min { ($x: expr) => ($x); ($x: expr, $( $y: expr ),+) => { std::cmp::min($x, min!($( $y ),+)) } } fn main() { std::thread::Builder::new() .stack_size(128 * 1024 * 1024) .spawn(|| Solver::default().solve()) .unwrap() .join() .unwrap(); } #[macro_export] macro_rules! input { () => {}; (mut $var:ident: $t:tt, $($rest:tt)*) => { let mut $var = __input_inner!($t); input!($($rest)*) }; ($var:ident: $t:tt, $($rest:tt)*) => { let $var = __input_inner!($t); input!($($rest)*) }; (mut $var:ident: $t:tt) => { let mut $var = __input_inner!($t); }; ($var:ident: $t:tt) => { let $var = __input_inner!($t); }; } #[macro_export] macro_rules! __input_inner { (($($t:tt),*)) => { ($(__input_inner!($t)),*) }; ([$t:tt; $n:expr]) => { (0..$n).map(|_| __input_inner!($t)).collect::>() }; ([$t:tt]) => {{ let n = __input_inner!(usize); (0..n).map(|_| __input_inner!($t)).collect::>() }}; (chars) => { __input_inner!(String).chars().collect::>() }; (bytes) => { __input_inner!(String).into_bytes() }; (usize1) => { __input_inner!(usize) - 1 }; ($t:ty) => { $crate::read::<$t>() }; } #[macro_export] macro_rules! println { () => { $crate::write(|w| { use std::io::Write; std::writeln!(w).unwrap() }) }; ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::writeln!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! print { ($($arg:tt)*) => { $crate::write(|w| { use std::io::Write; std::write!(w, $($arg)*).unwrap() }) }; } #[macro_export] macro_rules! flush { () => { $crate::write(|w| { use std::io::Write; w.flush().unwrap() }) }; } pub fn read() -> T where T: std::str::FromStr, T::Err: std::fmt::Debug, { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDIN: RefCell> = RefCell::new(stdin().lock()); } STDIN.with(|r| { let mut r = r.borrow_mut(); let mut s = vec![]; loop { let buf = r.fill_buf().unwrap(); if buf.is_empty() { break; } if let Some(i) = buf.iter().position(u8::is_ascii_whitespace) { s.extend_from_slice(&buf[..i]); r.consume(i + 1); if !s.is_empty() { break; } } else { s.extend_from_slice(buf); let n = buf.len(); r.consume(n); } } std::str::from_utf8(&s).unwrap().parse().unwrap() }) } pub fn write(f: F) where F: FnOnce(&mut std::io::BufWriter), { use std::cell::RefCell; use std::io::*; thread_local! { pub static STDOUT: RefCell>> = RefCell::new(BufWriter::new(stdout().lock())); } STDOUT.with(|w| f(&mut w.borrow_mut())) }