結果
問題 | No.1675 Strange Minimum Query |
ユーザー | ziita |
提出日時 | 2021-09-10 21:51:40 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 614 ms / 2,000 ms |
コード長 | 6,406 bytes |
コンパイル時間 | 27,466 ms |
コンパイル使用メモリ | 392,096 KB |
実行使用メモリ | 31,008 KB |
最終ジャッジ日時 | 2024-06-11 23:32:52 |
合計ジャッジ時間 | 26,917 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 255 ms
13,724 KB |
testcase_04 | AC | 259 ms
17,976 KB |
testcase_05 | AC | 83 ms
10,572 KB |
testcase_06 | AC | 270 ms
15,320 KB |
testcase_07 | AC | 330 ms
21,416 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 3 ms
6,940 KB |
testcase_10 | AC | 322 ms
22,352 KB |
testcase_11 | AC | 150 ms
18,540 KB |
testcase_12 | AC | 358 ms
22,836 KB |
testcase_13 | AC | 457 ms
28,220 KB |
testcase_14 | AC | 455 ms
27,544 KB |
testcase_15 | AC | 483 ms
28,504 KB |
testcase_16 | AC | 1 ms
6,944 KB |
testcase_17 | AC | 96 ms
7,620 KB |
testcase_18 | AC | 128 ms
8,384 KB |
testcase_19 | AC | 264 ms
20,608 KB |
testcase_20 | AC | 405 ms
25,380 KB |
testcase_21 | AC | 407 ms
24,188 KB |
testcase_22 | AC | 494 ms
26,564 KB |
testcase_23 | AC | 304 ms
13,744 KB |
testcase_24 | AC | 319 ms
16,264 KB |
testcase_25 | AC | 204 ms
11,096 KB |
testcase_26 | AC | 145 ms
12,036 KB |
testcase_27 | AC | 369 ms
23,488 KB |
testcase_28 | AC | 223 ms
20,492 KB |
testcase_29 | AC | 211 ms
19,328 KB |
testcase_30 | AC | 142 ms
11,660 KB |
testcase_31 | AC | 394 ms
20,368 KB |
testcase_32 | AC | 610 ms
29,584 KB |
testcase_33 | AC | 611 ms
29,528 KB |
testcase_34 | AC | 614 ms
30,564 KB |
testcase_35 | AC | 547 ms
29,660 KB |
testcase_36 | AC | 568 ms
31,008 KB |
ソースコード
#![allow(unused_imports)] #![allow(non_snake_case, unused)] // ---------- begin input macro ---------- // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 より macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($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:ty) => { $next().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::cmp::*; use std::collections::*; use std::ops::*; use std::marker::*; const INF: i64 = std::i64::MAX/100; const MOD: i64 = 1_000_000_007; // const MOD: i64 = 998_244_353; // ---------- begin lazy segment tree ---------- // https://yukicoder.me/submissions/379658 より pub trait TE { type T: Clone; type E: Clone; fn fold(l:Self::T, r:Self::T) -> Self::T; fn eval(p:Self::T, x:Self::E) -> Self::T; fn merge(f:Self::E, g:Self::E) -> Self::E; fn e() -> Self::T; fn id() -> Self::E; } pub struct LazySegmentTree<R: TE> { size: usize, bit: usize, a: Vec<(R::T, R::E)>, } impl <R: TE> LazySegmentTree<R> { pub fn new(n: usize) -> LazySegmentTree<R> { let mut bit = 0; while (1 << bit) < n { bit += 1; } LazySegmentTree { size: 1 << bit, bit: bit, a: vec![(R::e(), R::id()); 2 << bit], } } pub fn build_by(z: &[R::T]) -> LazySegmentTree<R> { let n = z.len(); let mut bit = 0; while (1 << bit) < n { bit += 1; } let mut a = vec![(R::e(), R::id()); 2 << bit]; for (a, z) in a[(1 << bit)..].iter_mut().zip(z.iter()) { a.0 = z.clone(); } for i in (1..(1 << bit)).rev() { let l = R::eval(a[2 * i].0.clone(), a[2 * i].1.clone()); let r = R::eval(a[2 * i + 1].0.clone(), a[2 * i + 1].1.clone()); a[i].0 = R::fold(l, r); } LazySegmentTree { size: 1 << bit, bit : bit, a: a, } } fn eval(&self, k: usize) -> R::T { R::eval(self.a[k].0.clone(), self.a[k].1.clone()) } fn propagate(&mut self, x: usize) { let x = x + self.size; for i in (1..(self.bit + 1)).rev() { let k = x >> i; self.a[2 * k].1 = R::merge(self.a[2 * k].1.clone(), self.a[k].1.clone()); self.a[2 * k + 1].1 = R::merge(self.a[2 * k + 1].1.clone(), self.a[k].1.clone()); self.a[k].1 = R::id(); self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1)); } } fn save(&mut self, x: usize) { let x = x + self.size; for i in 1..(self.bit + 1) { let k = x >> i; self.a[k].0 = R::fold(self.eval(2 * k), self.eval(2 * k + 1)); } } pub fn update(&mut self, l: usize, r: usize, op: R::E) { self.propagate(l); self.propagate(r - 1); let mut x = l + self.size; let mut y = r + self.size; while x < y { if x & 1 == 1 { self.a[x].1 = R::merge(self.a[x].1.clone(), op.clone()); x += 1; } if y & 1 == 1 { y -= 1; self.a[y].1 = R::merge(self.a[y].1.clone(), op.clone()); } x >>= 1; y >>= 1; } self.save(l); self.save(r - 1); } pub fn find(&mut self, l: usize, r: usize) -> R::T { self.propagate(l); self.propagate(r - 1); let mut x = l + self.size; let mut y = r + self.size; let mut p = R::e(); let mut q = R::e(); while x < y { if x & 1 == 1 { p = R::fold(p, self.eval(x)); x += 1; } if y & 1 == 1 { y -= 1; q = R::fold(self.eval(y), q); } x >>= 1; y >>= 1; } R::fold(p, q) } } struct R; impl TE for R { type T = (i64,usize); type E = (i64,usize); fn fold(l: Self::T, r: Self::T) -> Self::T { min(l,r) } fn eval(p: Self::T, x: Self::E) -> Self::T { if x.1>p.1 { x } else { p } } fn merge(f: Self::E, g: Self::E) -> Self::E { if f.1>g.1 { f } else { g } } fn e() -> Self::T { (1_000_000_000,0) } fn id() -> Self::E { (1_000_000_000,0) } } // ---------- end lazy segment tree ---------- fn main() { input! { n: usize, q: usize, lrb: [(usize,usize,i64);q], } let mut seg = LazySegmentTree::<R>::new(n); for i in 0..n { seg.update(i,i+1,(1_000_000_000,0)); } let mut v = vec![]; for &(l,r,b) in &lrb { v.push((b,l-1,r-1)); } v.sort(); let mut idx = 1; for &(b,l,r) in &v { seg.update(l,r+1,(b,idx)); idx += 1; } for &(b,l,r) in &v { let v = seg.find(l,r+1); if b!=v.0 { println!("-1"); return; } } for i in 0..n-1 { print!("{} ",seg.find(i,i+1).0); } println!("{}",seg.find(n-1,n).0); }