結果
問題 |
No.3207 Digital Font
|
ユーザー |
|
提出日時 | 2025-08-19 11:08:00 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 1,983 ms / 3,000 ms |
コード長 | 14,781 bytes |
コンパイル時間 | 12,410 ms |
コンパイル使用メモリ | 404,044 KB |
実行使用メモリ | 45,080 KB |
最終ジャッジ日時 | 2025-08-19 11:09:03 |
合計ジャッジ時間 | 50,320 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 38 |
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; #[allow(unused_imports)] use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } /// Verified by https://atcoder.jp/contests/abc198/submissions/21774342 mod mod_int { use std::ops::*; pub trait Mod: Copy { fn m() -> i64; } #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> } impl<M: Mod> ModInt<M> { // x >= 0 pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) } fn new_internal(x: i64) -> Self { ModInt { x: x, phantom: ::std::marker::PhantomData } } pub fn pow(self, mut e: i64) -> Self { debug_assert!(e >= 0); let mut sum = ModInt::new_internal(1); let mut cur = self; while e > 0 { if e % 2 != 0 { sum *= cur; } cur *= cur; e /= 2; } sum } #[allow(dead_code)] pub fn inv(self) -> Self { self.pow(M::m() - 2) } } impl<M: Mod> Default for ModInt<M> { fn default() -> Self { Self::new_internal(0) } } impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> { type Output = Self; fn add(self, other: T) -> Self { let other = other.into(); let mut sum = self.x + other.x; if sum >= M::m() { sum -= M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> { type Output = Self; fn sub(self, other: T) -> Self { let other = other.into(); let mut sum = self.x - other.x; if sum < 0 { sum += M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> { type Output = Self; fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) } } impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> { fn add_assign(&mut self, other: T) { *self = *self + other; } } impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> { fn sub_assign(&mut self, other: T) { *self = *self - other; } } impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> { fn mul_assign(&mut self, other: T) { *self = *self * other; } } impl<M: Mod> Neg for ModInt<M> { type Output = Self; fn neg(self) -> Self { ModInt::new(0) - self } } impl<M> ::std::fmt::Display for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { self.x.fmt(f) } } impl<M: Mod> ::std::fmt::Debug for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { let (mut a, mut b, _) = red(self.x, M::m()); if b < 0 { a = -a; b = -b; } write!(f, "{}/{}", a, b) } } impl<M: Mod> From<i64> for ModInt<M> { fn from(x: i64) -> Self { Self::new(x) } } // Finds the simplest fraction x/y congruent to r mod p. // The return value (x, y, z) satisfies x = y * r + z * p. fn red(r: i64, p: i64) -> (i64, i64, i64) { if r.abs() <= 10000 { return (r, 1, 0); } let mut nxt_r = p % r; let mut q = p / r; if 2 * nxt_r >= r { nxt_r -= r; q += 1; } if 2 * nxt_r <= -r { nxt_r += r; q -= 1; } let (x, z, y) = red(nxt_r, r); (x, y - q * z, z) } } // mod mod_int macro_rules! define_mod { ($struct_name: ident, $modulo: expr) => { #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct $struct_name {} impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } } } const MOD: i64 = 1_000_000_007; define_mod!(P, MOD); type MInt = mod_int::ModInt<P>; struct Rng { x: u64, } impl Rng { fn new() -> Self { use std::hash::{Hasher, BuildHasher}; let hm = std::collections::HashMap::<i32, i32>::new(); let mut hash = hm.hasher().build_hasher(); hash.write_u32(8128); Rng { x: hash.finish(), } } fn next(&mut self) -> u32 { let a = 0xdead_c0de_0013_3331u64; let b = 2457; self.x = self.x.wrapping_mul(a).wrapping_add(b); let x = self.x; ((x ^ x << 10) >> 32) as _ } } // Lazy Segment Tree. This data structure is useful for fast folding and updating on intervals of an array // whose elements are elements of monoid T. Note that constructing this tree requires the identity // element of T and the operation of T. This is monomorphised, because of efficiency. T := i64, biop = max, upop = (+) // Reference: https://github.com/atcoder/ac-library/blob/master/atcoder/lazysegtree.hpp // Verified by: https://judge.yosupo.jp/submission/68794 // https://atcoder.jp/contests/joisc2021/submissions/27734236 pub trait ActionRing { type T: Clone + Copy; // data type U: Clone + Copy + PartialEq + Eq; // action fn biop(x: Self::T, y: Self::T) -> Self::T; fn update(x: Self::T, a: Self::U) -> Self::T; fn upop(fst: Self::U, snd: Self::U) -> Self::U; fn e() -> Self::T; fn upe() -> Self::U; // identity for upop } pub struct LazySegTree<R: ActionRing> { n: usize, dep: usize, dat: Vec<R::T>, lazy: Vec<R::U>, } impl<R: ActionRing> LazySegTree<R> { pub fn new(n_: usize) -> Self { let mut n = 1; let mut dep = 0; while n < n_ { n *= 2; dep += 1; } // n is a power of 2 LazySegTree { n: n, dep: dep, dat: vec![R::e(); 2 * n], lazy: vec![R::upe(); n], } } #[allow(unused)] pub fn with(a: &[R::T]) -> Self { let mut ret = Self::new(a.len()); let n = ret.n; for i in 0..a.len() { ret.dat[n + i] = a[i]; } for i in (1..n).rev() { ret.update_node(i); } ret } #[inline] pub fn set(&mut self, idx: usize, x: R::T) { debug_assert!(idx < self.n); self.apply_any(idx, |_t| x); } #[inline] pub fn apply(&mut self, idx: usize, f: R::U) { debug_assert!(idx < self.n); self.apply_any(idx, |t| R::update(t, f)); } pub fn apply_any<F: Fn(R::T) -> R::T>(&mut self, idx: usize, f: F) { debug_assert!(idx < self.n); let idx = idx + self.n; for i in (1..self.dep + 1).rev() { self.push(idx >> i); } self.dat[idx] = f(self.dat[idx]); for i in 1..self.dep + 1 { self.update_node(idx >> i); } } pub fn get(&mut self, idx: usize) -> R::T { debug_assert!(idx < self.n); let idx = idx + self.n; for i in (1..self.dep + 1).rev() { self.push(idx >> i); } self.dat[idx] } /* [l, r) (note: half-inclusive) */ #[inline] pub fn query(&mut self, rng: std::ops::Range<usize>) -> R::T { let (l, r) = (rng.start, rng.end); debug_assert!(l <= r && r <= self.n); if l == r { return R::e(); } let mut l = l + self.n; let mut r = r + self.n; for i in (1..self.dep + 1).rev() { if ((l >> i) << i) != l { self.push(l >> i); } if ((r >> i) << i) != r { self.push((r - 1) >> i); } } let mut sml = R::e(); let mut smr = R::e(); while l < r { if (l & 1) != 0 { sml = R::biop(sml, self.dat[l]); l += 1; } if (r & 1) != 0 { r -= 1; smr = R::biop(self.dat[r], smr); } l >>= 1; r >>= 1; } R::biop(sml, smr) } /* ary[i] = upop(ary[i], v) for i in [l, r) (half-inclusive) */ #[inline] pub fn update(&mut self, rng: std::ops::Range<usize>, f: R::U) { let (l, r) = (rng.start, rng.end); debug_assert!(l <= r && r <= self.n); if l == r { return; } let mut l = l + self.n; let mut r = r + self.n; for i in (1..self.dep + 1).rev() { if ((l >> i) << i) != l { self.push(l >> i); } if ((r >> i) << i) != r { self.push((r - 1) >> i); } } { let l2 = l; let r2 = r; while l < r { if (l & 1) != 0 { self.all_apply(l, f); l += 1; } if (r & 1) != 0 { r -= 1; self.all_apply(r, f); } l >>= 1; r >>= 1; } l = l2; r = r2; } for i in 1..self.dep + 1 { if ((l >> i) << i) != l { self.update_node(l >> i); } if ((r >> i) << i) != r { self.update_node((r - 1) >> i); } } } #[inline] fn update_node(&mut self, k: usize) { self.dat[k] = R::biop(self.dat[2 * k], self.dat[2 * k + 1]); } fn all_apply(&mut self, k: usize, f: R::U) { self.dat[k] = R::update(self.dat[k], f); if k < self.n { self.lazy[k] = R::upop(self.lazy[k], f); } } fn push(&mut self, k: usize) { let val = self.lazy[k]; self.all_apply(2 * k, val); self.all_apply(2 * k + 1, val); self.lazy[k] = R::upe(); } } enum AffineRolling3207 {} type AffineInt = MInt; // Change here to change type impl ActionRing for AffineRolling3207 { type T = (AffineInt, AffineInt, AffineInt); // data, bias, cumulative sum of bias^? type U = (AffineInt, AffineInt); // action, (a, b) |-> x |-> ax + b fn biop((x, s, t): Self::T, (y, u, v): Self::T) -> Self::T { (x * u + y, s * u, t * u + v) } fn update((x, bias, s): Self::T, (a, b): Self::U) -> Self::T { (x * a + b * s, bias, s) } fn upop(fst: Self::U, snd: Self::U) -> Self::U { let (a, b) = fst; let (c, d) = snd; (a * c, b * c + d) } fn e() -> Self::T { (0.into(), 1.into(), 0.into()) } fn upe() -> Self::U { // identity for upop (1.into(), 0.into()) } } fn hashes( h: usize, w: usize, ijx: &[(usize, usize, i8)], ldru: &[(usize, usize, usize, usize)], bases: &[MInt; 2], letters: &[MInt; 10], ) -> Vec<MInt> { let q = ldru.len(); let mut st = LazySegTree::<AffineRolling3207>::new(h); for i in 0..h { st.set(i, (0.into(), bases[0], 1.into())); } let mut ev = vec![vec![]; w + 1]; for &(i, j, x) in ijx { ev[j].push((2, i, letters[x as usize])); } for i in 0..w { ev[i].push((1, 0, 0.into())); } for i in 0..q { let (_, d, _, u) = ldru[i]; ev[d].push((0, i, -bases[1].pow((u - d) as i64))); ev[u].push((0, i, MInt::new(1))); } for i in 0..w + 1 { ev[i].sort(); } let mut hashes = vec![MInt::new(0); q]; for i in 0..w + 1 { for &(ty, idx, x) in &ev[i] { if ty == 0 { let (l, _, r, _) = ldru[idx]; hashes[idx] += x * st.query(l..r).0; } else if ty == 1 { st.update(0..h, (bases[1], 0.into())); } else { st.apply(idx, (1.into(), x)); } } } hashes } // https://yukicoder.me/problems/no/3207 (3.5) // 上下それぞれの方向から平面走査して rolling hash を計算すればよい。 // 二つの方向で計算する時、rolling hash の bases と文字に対する乱数が同じになるようにすること。 // -> サンプルが合わずに困ったが、d のときのハッシュ値は単に -1 枚するのではなく bases[1]^{u-d} 倍する必要があることに気づいた。 fn main() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} input! { h: usize, w: usize, n: usize, ijx: [(usize1, usize1, i8); n], q: usize, ldru: [(usize1, usize1, usize, usize); q], } let mut rng = Rng::new(); let mut ok = vec![true; q]; for _ in 0..2 { let mut bases = [MInt::new(0); 2]; let mut letters = [MInt::new(0); 10]; for i in 0..2 { bases[i] = MInt::new(rng.next() as i64); bases[i] = MInt::new(10); } for i in 1..10 { letters[i] = MInt::new(rng.next() as i64); letters[i] = MInt::new(i as i64); } let hashes1 = hashes(h, w, &ijx, &ldru, &bases, &letters); let ijx = ijx.iter().map(|&(i, j, x)| { let y = match x { 6 => 9, 9 => 6, _ => x, }; (h - 1 - i, w - 1 - j, y) }).collect::<Vec<_>>(); let ldru = ldru.iter().map(|&(l, d, r, u)| { (h - r, w - u, h - l, w - d) }).collect::<Vec<_>>(); let hashes2 = hashes(h, w, &ijx, &ldru, &bases, &letters); for i in 0..q { ok[i] &= hashes1[i] == hashes2[i]; } } for i in 0..q { puts!("{}\n", if ok[i] { "Yes" } else { "No" }); } }