結果

問題 No.1675 Strange Minimum Query
ユーザー phsplsphspls
提出日時 2022-11-03 21:37:43
言語 Rust
(1.77.0)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 4,325 bytes
コンパイル時間 1,190 ms
コンパイル使用メモリ 162,896 KB
実行使用メモリ 27,740 KB
最終ジャッジ日時 2023-09-25 05:06:05
合計ジャッジ時間 14,061 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 245 ms
7,660 KB
testcase_04 AC 256 ms
9,204 KB
testcase_05 AC 23 ms
6,424 KB
testcase_06 AC 294 ms
8,428 KB
testcase_07 AC 350 ms
10,672 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 145 ms
21,208 KB
testcase_11 AC 62 ms
18,968 KB
testcase_12 AC 161 ms
22,848 KB
testcase_13 AC 195 ms
27,740 KB
testcase_14 AC 294 ms
27,276 KB
testcase_15 AC 230 ms
27,572 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 64 ms
7,932 KB
testcase_18 AC 91 ms
8,568 KB
testcase_19 AC 147 ms
21,972 KB
testcase_20 AC 298 ms
21,764 KB
testcase_21 AC 281 ms
24,028 KB
testcase_22 AC 365 ms
25,420 KB
testcase_23 AC 250 ms
11,296 KB
testcase_24 AC 241 ms
15,908 KB
testcase_25 AC 164 ms
9,516 KB
testcase_26 AC 92 ms
12,052 KB
testcase_27 AC 254 ms
24,284 KB
testcase_28 AC 137 ms
19,720 KB
testcase_29 AC 108 ms
21,256 KB
testcase_30 AC 93 ms
12,232 KB
testcase_31 AC 332 ms
14,912 KB
testcase_32 AC 490 ms
27,300 KB
testcase_33 AC 489 ms
27,296 KB
testcase_34 AC 482 ms
27,304 KB
testcase_35 AC 354 ms
27,372 KB
testcase_36 AC 356 ms
27,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `x`
   --> Main.rs:122:19
    |
122 |     let mapper = |x: usize, y: usize| y;
    |                   ^ help: if this is intentional, prefix it with an underscore: `_x`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: field `orgn` is never read
 --> Main.rs:8:5
  |
6 | struct LazySegmentTree<F, G, H, T, U> {
  |        --------------- field in this struct
7 |     n: usize,
8 |     orgn: usize,
  |     ^^^^
  |
  = note: `LazySegmentTree` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
  = note: `#[warn(dead_code)]` on by default

warning: 2 warnings emitted

ソースコード

diff #

const INF: usize = 1usize << 60;
const UNIT: usize = 1e9 as usize;

#[derive(Debug, Clone)]
struct LazySegmentTree<F, G, H, T, U> {
    n: usize,
    orgn: usize,
    unit: T,
    mem: Vec<T>,
    lazy_unit: U,
    lazy: Vec<U>,
    operator: F,
    mapper: G,
    composer: H,
}

impl<F, G, H, T, U> LazySegmentTree<F, G, H, T, U> where
T: std::fmt::Debug + Copy,
U: std::fmt::Debug + std::cmp::PartialEq + Copy,
F: Fn(T, T) -> T,
G: Fn(T, U) -> T,
H: Fn(U, U) -> U,
{
    fn new(n: usize, unit: T, lazy_unit: U, operator: F, mapper: G, composer: H) -> Self {
        let mut size = 1usize;
        while size < n { size *= 2; }
        Self {
            n: size,
            orgn: n,
            unit: unit,
            mem: vec![unit; size*2-1],
            lazy_unit: lazy_unit,
            lazy: vec![lazy_unit; size*2-1],
            operator: operator,
            mapper: mapper,
            composer: composer,
        }
    }

    fn find_closed(&mut self, l: usize, r: usize) -> T {
        self.find(l, r+1)
    }

    fn find(&mut self, l: usize, r: usize) -> T {
        self._find(l, r, 0, 0, self.n)
    }

    fn update_closed(&mut self, left: usize, right: usize, val: U) {
        self.update(left, right+1, val);
    }

    fn update(&mut self, left: usize, right: usize, val: U) {
        self._update(left, right, val, 0, 0, self.n);
    }

    fn get(&mut self, pos: usize) -> T {
        self.find_closed(pos, pos)
    }

    fn _evaluate(&mut self, k: usize) {
        if self.lazy[k] == self.lazy_unit { return; }
        if k < self.n-1 {
            let left = self._left(k);
            let right = self._right(k);
            self.lazy[left] = (self.composer)(self.lazy[left], self.lazy[k]);
            self.lazy[right] = (self.composer)(self.lazy[right], self.lazy[k]);
        }
        self.mem[k] = (self.mapper)(self.mem[k], self.lazy[k]);
        self.lazy[k] = self.lazy_unit;
    }
    fn _find(&mut self, a: usize, b: usize, nodenum: usize, l: usize, r: usize) -> T {
        self._evaluate(nodenum);
        if r <= a || b <= l {
            self.unit
        } else if a <= l && r <= b {
            self.mem[nodenum]
        } else {
            let left = self._find(a, b, self._left(nodenum), l, (l+r)/2);
            let right = self._find(a, b, self._right(nodenum), (l+r)/2, r);
            (self.operator)(left, right)
        }
    }
    fn _update(&mut self, a: usize, b: usize, x: U, nodenum: usize, l: usize, r: usize) {
        self._evaluate(nodenum);
        if a <= l && r <= b {
            self.lazy[nodenum] = (self.composer)(self.lazy[nodenum], x);
            self._evaluate(nodenum);
        } else if a < r && l < b {
            self._update(a, b, x, self._left(nodenum), l, (l + r) / 2);
            self._update(a, b, x, self._right(nodenum), (l + r) / 2, r);
            self.mem[nodenum] = (self.operator)(self.mem[self._left(nodenum)], self.mem[self._right(nodenum)]);
        }
    }
    fn _left(&self, k: usize) -> usize {
        2*k+1
    }
    fn _right(&self, k: usize) -> usize {
        2*k+2
    }
    fn _parent(&self, k: usize) -> usize {
        (k-1)/2
    }
}

fn main() {
    let mut nq = String::new();
    std::io::stdin().read_line(&mut nq).ok();
    let nq: Vec<usize> = nq.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nq[0];
    let q = nq[1];
    let mut queries = (0..q).map(|_| {
            let mut temp = String::new();
            std::io::stdin().read_line(&mut temp).ok();
            let temp: Vec<usize> = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
            (temp[0]-1, temp[1]-1, temp[2])
        })
        .collect::<Vec<_>>();

    queries.sort_by_key(|&(_, _, v)| v);
    let operator = |x: usize, y: usize| x.min(y);
    let mapper = |x: usize, y: usize| y;
    let composer = |x: usize, y: usize| { if y == INF { x } else { y } };
    let mut segtree = LazySegmentTree::new(n, UNIT, INF, operator, mapper, composer);
    for &(l, r, v) in queries.iter() {
        segtree.update_closed(l, r, v);
    }
    if queries.iter().filter(|&&(l, r, v)| segtree.find_closed(l, r).min(UNIT) != v).count() > 0 {
        println!("-1");
    } else {
        println!("{}", (0..n).map(|i| segtree.get(i).min(UNIT).to_string()).collect::<Vec<_>>().join(" "));
    }
}
0