// スコアは単調増加 // k=1では何もできん // k=NはNを達成可能 // // あるiが存在して // (A_j, B_j) = (i, Z_i) // となるようなjの個数を最大化したい // // 固定したkについて解けるか // 端から揃えていく? // ABがswap可能なら区間でマッチ可能なやつあったら適当にそれを持ってくる // 片方だけswap可能でもマッチ可能なやつあったらそれを // 動かせないならまあなんとでも // i とZ_iはマッチ可能になったら以降はマッチできる // iとz_iがマッチする最小のkはセグ木で計算できる use std::io::Write; fn run() { input! { n: usize, a: [usize1; n], b: [usize1; n], z: [usize1; n], } let mut ia = vec![0; n]; let mut ib = vec![0; n]; for i in 0..n { ia[a[i]] = i; ib[b[i]] = i; } let inf = n; // max a, max b, a-b min max, b-a min max let mut seg = SegmentTreePURQ::new(n, (0, inf, inf, 0), |a, b| { let x = std::cmp::min(a.0.max(b.2), a.2.max(b.1)); let y = std::cmp::min(a.1.max(b.3), a.3.max(b.0)); (a.0.max(b.0), a.1.max(b.1), x, y) }); for i in 0..n { seg.update_tmp(i, (a[i], b[i], a[i].max(b[i]), a[i].max(b[i]))); } seg.update_all(); let mut imos = vec![0; n]; for i in 0..n { let x = ia[i]; let y = ib[z[i]]; if x == y { imos[0] += 1; } else { let l = x.min(y); let r = x.max(y) + 1; let p = seg.find(l, r); let pos = p.0.min(p.1).min(p.2).min(p.3); imos[pos] += 1; } } for i in 1..n { imos[i] += imos[i - 1]; } let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); for a in imos { writeln!(out, "{}", a).ok(); } } fn main() { run(); } // ---------- begin input macro ---------- // reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 #[macro_export] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } #[macro_export] macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[macro_export] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- // ---------- begin segment tree Point Update Range Query ---------- pub struct SegmentTreePURQ { n: usize, size: usize, data: Vec, e: T, op: F, } impl SegmentTreePURQ where T: Clone, F: Fn(&T, &T) -> T, { pub fn new(n: usize, e: T, op: F) -> Self { assert!(n > 0); let size = n.next_power_of_two(); let data = vec![e.clone(); 2 * size]; SegmentTreePURQ { n, size, data, e, op, } } pub fn update_tmp(&mut self, x: usize, v: T) { assert!(x < self.n); self.data[x + self.size] = v; } pub fn update_all(&mut self) { for i in (1..self.size).rev() { self.data[i] = (self.op)(&self.data[2 * i], &self.data[2 * i + 1]); } } pub fn update(&mut self, x: usize, v: T) { assert!(x < self.n); let mut x = x + self.size; self.data[x] = v; x >>= 1; while x > 0 { self.data[x] = (self.op)(&self.data[2 * x], &self.data[2 * x + 1]); x >>= 1; } } pub fn find(&self, l: usize, r: usize) -> T { assert!(l <= r && r <= self.n); if l == r { return self.e.clone(); } let mut l = self.size + l; let mut r = self.size + r; let mut x = self.e.clone(); let mut y = self.e.clone(); while l < r { if l & 1 == 1 { x = (self.op)(&x, &self.data[l]); l += 1; } if r & 1 == 1 { r -= 1; y = (self.op)(&self.data[r], &y); } l >>= 1; r >>= 1; } (self.op)(&x, &y) } pub fn max_right

(&self, l: usize, f: P) -> usize where P: Fn(&T) -> bool, { assert!(l <= self.n); assert!(f(&self.e)); if l == self.n { return self.n; } let mut l = l + self.size; let mut sum = self.e.clone(); while { l >>= l.trailing_zeros(); let v = (self.op)(&sum, &self.data[l]); if !f(&v) { while l < self.size { l <<= 1; let v = (self.op)(&sum, &self.data[l]); if f(&v) { sum = v; l += 1; } } return l - self.size; } sum = v; l += 1; l.count_ones() > 1 } {} self.n } pub fn min_left

(&self, r: usize, f: P) -> usize where P: Fn(&T) -> bool, { assert!(r <= self.n); assert!(f(&self.e)); if r == 0 { return 0; } let mut r = r + self.size; let mut sum = self.e.clone(); while { r -= 1; while r > 1 && r & 1 == 1 { r >>= 1; } let v = (self.op)(&self.data[r], &sum); if !f(&v) { while r < self.size { r = 2 * r + 1; let v = (self.op)(&self.data[r], &sum); if f(&v) { sum = v; r -= 1; } } return r + 1 - self.size; } sum = v; (r & (!r + 1)) != r } {} 0 } } // ---------- end segment tree Point Update Range Query ----------