結果
問題 | No.1548 [Cherry 2nd Tune B] 貴方と私とサイクルとモーメント |
ユーザー | koba-e964 |
提出日時 | 2021-12-11 14:21:45 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 745 ms / 4,500 ms |
コード長 | 11,691 bytes |
コンパイル時間 | 16,389 ms |
コンパイル使用メモリ | 377,588 KB |
実行使用メモリ | 52,736 KB |
最終ジャッジ日時 | 2024-07-19 19:22:33 |
合計ジャッジ時間 | 38,117 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 434 ms
27,188 KB |
testcase_03 | AC | 150 ms
14,592 KB |
testcase_04 | AC | 509 ms
52,352 KB |
testcase_05 | AC | 32 ms
5,376 KB |
testcase_06 | AC | 298 ms
26,852 KB |
testcase_07 | AC | 540 ms
52,736 KB |
testcase_08 | AC | 438 ms
27,136 KB |
testcase_09 | AC | 271 ms
14,720 KB |
testcase_10 | AC | 417 ms
52,092 KB |
testcase_11 | AC | 537 ms
52,224 KB |
testcase_12 | AC | 152 ms
8,192 KB |
testcase_13 | AC | 399 ms
52,224 KB |
testcase_14 | AC | 239 ms
14,464 KB |
testcase_15 | AC | 465 ms
52,672 KB |
testcase_16 | AC | 387 ms
27,264 KB |
testcase_17 | AC | 504 ms
52,224 KB |
testcase_18 | AC | 521 ms
52,480 KB |
testcase_19 | AC | 157 ms
14,464 KB |
testcase_20 | AC | 429 ms
52,480 KB |
testcase_21 | AC | 284 ms
27,012 KB |
testcase_22 | AC | 673 ms
52,452 KB |
testcase_23 | AC | 669 ms
52,608 KB |
testcase_24 | AC | 680 ms
52,552 KB |
testcase_25 | AC | 677 ms
52,668 KB |
testcase_26 | AC | 669 ms
52,736 KB |
testcase_27 | AC | 667 ms
52,608 KB |
testcase_28 | AC | 677 ms
52,608 KB |
testcase_29 | AC | 689 ms
52,572 KB |
testcase_30 | AC | 686 ms
52,608 KB |
testcase_31 | AC | 672 ms
52,608 KB |
testcase_32 | AC | 564 ms
52,608 KB |
testcase_33 | AC | 745 ms
52,608 KB |
testcase_34 | AC | 626 ms
52,592 KB |
testcase_35 | AC | 685 ms
52,684 KB |
testcase_36 | AC | 666 ms
52,608 KB |
testcase_37 | AC | 685 ms
52,608 KB |
testcase_38 | AC | 559 ms
52,608 KB |
testcase_39 | AC | 716 ms
52,576 KB |
testcase_40 | AC | 609 ms
52,460 KB |
testcase_41 | AC | 667 ms
52,600 KB |
ソースコード
use std::io::Read; fn get_word() -> String { let stdin = std::io::stdin(); let mut stdin=stdin.lock(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec<u8> = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } /// 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> From<i64> for ModInt<M> { fn from(x: i64) -> Self { Self::new(x) } } } // mod mod_int macro_rules! define_mod { ($struct_name: ident, $modulo: expr) => { #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct $struct_name {} impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } } } const MOD: i64 = 998_244_353; define_mod!(P, MOD); type MInt = mod_int::ModInt<P>; // 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 } #[derive(Clone)] pub struct LazySegTree<R: ActionRing + Clone> { n: usize, dep: usize, dat: Vec<R::T>, lazy: Vec<R::U>, } impl<R: ActionRing + Clone> 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, l: usize, r: usize) -> R::T { 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, l: usize, r: usize, f: R::U) { 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(); } } #[derive(Clone)] enum Affine {} type AffineInt = MInt; // Change here to change type impl ActionRing for Affine { type T = (AffineInt, AffineInt); // data, size type U = (AffineInt, AffineInt); // action, (a, b) |-> x |-> ax + b fn biop((x, s): Self::T, (y, t): Self::T) -> Self::T { (x + y, s + t) } fn update((x, s): Self::T, (a, b): Self::U) -> Self::T { (x * a + b * s, 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(), 0.into()) } fn upe() -> Self::U { // identity for upop (1.into(), 0.into()) } } #[allow(dead_code)] fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() } fn upd(n: usize, st: &mut LazySegTree<Affine>, l: usize, r: usize, x: MInt) { if l <= r { st.update(l, r + 1, (0.into(), x)); } else { st.update(l, n, (0.into(), x)); st.update(0, r + 1, (0.into(), x)); } } fn que(n: usize, st: &mut LazySegTree<Affine>, l: usize, r: usize) -> MInt { if l <= r { st.query(l, r + 1).0 } else { let a = st.query(l, n).0; let b = st.query(0, r + 1).0; a + b } } // https://yukicoder.me/problems/no/1548 (3) // サイクルの中の範囲の 1 乗和から 4 乗和が分かれば良いので、遅延セグメント木を 4 本持てば良い。 fn main() { let n: usize = get(); let a: Vec<i64> = (0..n).map(|_| get()).collect(); let q: usize = get(); let mut st = vec![LazySegTree::<Affine>::new(n); 4]; for i in 0..n { let b = MInt::new(a[i]); let mut c = b; for j in 0..4 { st[j].set(i, (c, 1.into())); c *= b; } } let comb = vec![ vec![1], vec![1, 1], vec![1, 2, 1], vec![1, 3, 3, 1], vec![1, 4, 6, 4, 1], ]; for _ in 0..q { let ty: usize = get(); let u = get::<usize>() - 1; let v = get::<usize>() - 1; let w = get::<usize>() - 1; let (u, v) = if u > v { (v, u) } else { (u, v) }; let (l, r) = if u < w && w < v { (u, v) } else { (v, u) }; if ty == 0 { let b: i64 = get(); let b = MInt::new(b); let mut c = b; for i in 0..4 { upd(n, &mut st[i], l, r, c); c *= b; } } else { let len = (r + n - l) % n + 1; let len = len as i64; let leninv = MInt::new(len).inv(); let mut ans = MInt::new(0); let avg = -que(n, &mut st[0], l, r) * leninv; let mut c = MInt::new(1); for i in (0..ty + 1).rev() { let tmp = if i == 0 { MInt::new(len) } else { que(n, &mut st[i - 1], l, r) } * comb[ty][i] * c; ans += tmp; c *= avg; } println!("{}", ans * leninv); } } }