結果
問題 | No.749 クエリ全部盛り |
ユーザー | akakimidori |
提出日時 | 2019-08-06 19:13:22 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 308 ms / 3,000 ms |
コード長 | 6,289 bytes |
コンパイル時間 | 11,908 ms |
コンパイル使用メモリ | 377,900 KB |
実行使用メモリ | 129,280 KB |
最終ジャッジ日時 | 2024-07-18 14:19:06 |
合計ジャッジ時間 | 14,011 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 0 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 1 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 1 ms
5,376 KB |
testcase_08 | AC | 1 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 11 ms
5,376 KB |
testcase_12 | AC | 11 ms
5,376 KB |
testcase_13 | AC | 11 ms
5,376 KB |
testcase_14 | AC | 10 ms
5,376 KB |
testcase_15 | AC | 308 ms
129,280 KB |
testcase_16 | AC | 285 ms
129,280 KB |
testcase_17 | AC | 294 ms
129,256 KB |
testcase_18 | AC | 290 ms
129,280 KB |
testcase_19 | AC | 292 ms
129,152 KB |
ソースコード
// ---------- begin Lazy Segment Tree ---------- mod segment_tree { pub struct Lazy<T, E, F, G, H> { n: usize, k: usize, a: Vec<(T, E)>, e: T, id: E, f: F, g: G, h: H, } #[allow(dead_code)] impl<T: Clone, E: Clone, F: Fn(T, T) -> T, G: Fn(T, E) -> T, H: Fn(E, E) -> E> Lazy<T, E, F, G, H> { pub fn new(n: usize, e: T, id: E, f: F, g: G, h: H) -> Lazy<T, E, F, G, H> { let mut k = 0; while (1 << k) < n { k += 1; } Lazy { n: 1 << k, k: k, a: vec![(e.clone(), id.clone()); 2 << k], e: e, id: id, f: f, g: g, h: h, } } pub fn build_by(z: &Vec<T>, e: T, id: E, f: F, g: G, h: H) -> Lazy<T, E, F, G, H> { let n = z.len(); let mut k = 0; while (1 << k) < n { k += 1; } let mut a = vec![(e.clone(), id.clone()); 2 << k]; for i in 0..n { a[(1 << k) + i].0 = z[i].clone(); } for i in (1..(1 << k)).rev() { let l = g(a[2 * i].0.clone(), id.clone()); let r = g(a[2 * i + 1].0.clone(), id.clone()); a[i].0 = f(l, r); } Lazy { n: 1 << k, k: k, a: a, e: e, id: id, f: f, g: g, h: h, } } fn eval (&self, x: usize) -> T { (self.g)(self.a[x].0.clone(), self.a[x].1.clone()) } fn propagate (&mut self, mut x: usize) { x += self.n; for k in (1..(self.k + 1)).rev() { let y = x >> k; self.a[2 * y].1 = (self.h)(self.a[2 * y].1.clone(), self.a[y].1.clone()); self.a[2 * y + 1].1 = (self.h)(self.a[2 * y + 1].1.clone(), self.a[y].1.clone()); self.a[y].1 = self.id.clone(); self.a[y].0 = (self.f)(self.eval(2 * y), self.eval(2 * y + 1)); } } fn save (&mut self, x: usize) { let mut k = (x + self.n) >> 1; while k > 0 { self.a[k].0 = (self.f)(self.eval(2 * k), self.eval(2 * k + 1)); k >>= 1; } } pub fn update (&mut self, l: usize, r: usize, p: E) { self.propagate(l); self.propagate(r - 1); let mut x = l + self.n; let mut y = r + self.n; while x < y { if (x & 1) == 1 { self.a[x].1 = (self.h)(self.a[x].1.clone(), p.clone()); x += 1; } if (y & 1) == 1 { y -= 1; self.a[y].1 = (self.h)(self.a[y].1.clone(), p.clone()); } x >>= 1; y >>= 1; } self.save(l); self.save(r - 1); } pub fn find (&mut self, l: usize, r: usize) -> T { self.propagate(l); self.propagate(r - 1); let mut p = self.e.clone(); let mut q = self.e.clone(); let mut x = l + self.n; let mut y = r + self.n; while x < y { if (x & 1) == 1 { p = (self.f)(p, self.eval(x)); x += 1; } if (y & 1) == 1 { y -= 1; q = (self.f)(self.eval(y), q); } x >>= 1; y >>= 1; } (self.f)(p, q) } } } // ---------- end Lazy Segment Tree ---------- //https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より 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_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_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::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ここまで use std::io::Write; fn run() { let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); input! { n: usize, q: usize, p: [(u8, usize, usize, u64); q], } const MOD: u64 = 1000000007; let mut z = vec![(0, 1, 0); n + 1]; z[1].2 = 1; for i in 2..n { z[i].2 = (z[i - 1].2 + z[i - 2].2) % MOD; } let add = |a, b| {let mut v = a + b; if v >= MOD {v -= MOD;} v}; let f = |(a, b, c), (x, y, z)| (add(a, x), b + y, add(c, z)); let g = |(a, b, c), (p, q, r)| ((a * p + b * q + c * r) % MOD, b, c); let h = |(p, q, r), (s, t, u)| (p * s % MOD, (q * s + t) % MOD, (r * s + u) % MOD); let mut s = segment_tree::Lazy::build_by(&z, (0, 0, 0), (1, 0, 0), f, g, h); for (q, l, r, k) in p { match q { 0 => { let ans = s.find(l, r + 1).0 * k % MOD; writeln!(out, "{}", ans).unwrap(); }, 1 => s.update(l, r + 1, (0, k, 0)), 2 => s.update(l, r + 1, (1, k, 0)), 3 => s.update(l, r + 1, (k, 0, 0)), _ => s.update(l, r + 1, (1, 0, k)), } } } fn main() { run(); }