結果

問題 No.789 範囲の合計
ユーザー phsplsphspls
提出日時 2022-12-05 09:30:24
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 99 ms / 1,000 ms
コード長 3,018 bytes
コンパイル時間 28,666 ms
コンパイル使用メモリ 389,808 KB
実行使用メモリ 14,636 KB
最終ジャッジ日時 2024-10-12 07:44:11
合計ジャッジ時間 15,322 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,820 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 85 ms
13,296 KB
testcase_03 AC 34 ms
6,816 KB
testcase_04 AC 77 ms
13,560 KB
testcase_05 AC 68 ms
14,636 KB
testcase_06 AC 74 ms
13,532 KB
testcase_07 AC 29 ms
6,816 KB
testcase_08 AC 47 ms
8,576 KB
testcase_09 AC 44 ms
8,304 KB
testcase_10 AC 99 ms
13,988 KB
testcase_11 AC 72 ms
12,180 KB
testcase_12 AC 74 ms
13,580 KB
testcase_13 AC 1 ms
6,816 KB
testcase_14 AC 1 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `orgn` is never read
 --> src/main.rs:6:5
  |
4 | struct SegmentTree<F, T> {
  |        ----------- field in this struct
5 |     n: usize,
6 |     orgn: usize,
  |     ^^^^
  |
  = note: `SegmentTree` 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

ソースコード

diff #

use std::collections::{BTreeSet, HashMap};

#[derive(Debug, Clone)]
struct SegmentTree<F, T> {
    n: usize,
    orgn: usize,
    unit: T,
    mem: Vec<T>,
    operator: F
}

impl<F, T> SegmentTree<F, T> where
T: std::fmt::Debug + Copy,
F: Fn(T, T) -> T
{
    fn new(n: usize, unit: T, operator: F) -> Self {
        let mut size = 1usize;
        while size < n { size *= 2; }
        Self {
            n: size,
            orgn: n,
            unit: unit,
            mem: vec![unit; size*2-1],
            operator: operator,
        }
    }

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

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

    fn set(&mut self, pos: usize, val: T) {
        let mut pos = pos + self.n - 1;
        self.mem[pos] = val;
        while pos > 0 {
            pos = self._parent(pos);
            self.mem[pos] = (self.operator)(self.mem[self._left(pos)], self.mem[self._right(pos)]);
        }
    }

    fn get(&mut self, pos: usize) -> T {
        self.mem[pos + self.n - 1]
    }

    fn _find(&self, a: usize, b: usize, nodenum: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.unit;
        } else if a <= l && r <= b {
            return self.mem[nodenum];
        }
        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 _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 n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut queries = (0..n).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], temp[1], temp[2])
        })
        .collect::<Vec<_>>();

    let mut pos = BTreeSet::new();
    for &(t, x, y) in queries.iter() {
        pos.insert(x);
        if t == 1 {
            pos.insert(y);
        }
    }
    let mapping = pos.iter().enumerate().map(|(i, &v)| (v, i)).collect::<HashMap<usize, usize>>();
    for i in 0..n {
        let (t, x, y) = queries[i];
        if t == 0 {
            queries[i] = (t, *mapping.get(&x).unwrap(), y);
        } else {
            queries[i] = (t, *mapping.get(&x).unwrap(), *mapping.get(&y).unwrap());
        }
    }
    let mut segtree = SegmentTree::new(pos.len(), 0usize, |x, y| x + y);
    let mut result = 0usize;
    for &(t, x, y) in queries.iter() {
        if t == 0 {
            let val = segtree.get(x);
            segtree.set(x, val+y);
        } else {
            result += segtree.find_closed(x, y);
        }
    }
    println!("{}", result);
}
0