結果

問題 No.2640 traO Stamps
ユーザー nautnaut
提出日時 2024-02-19 23:03:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 6,478 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 192,384 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2024-02-19 23:04:00
合計ジャッジ時間 5,303 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
6,676 KB
testcase_01 AC 0 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 0 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 57 ms
7,124 KB
testcase_07 AC 50 ms
6,768 KB
testcase_08 AC 60 ms
8,248 KB
testcase_09 AC 44 ms
6,676 KB
testcase_10 AC 61 ms
8,252 KB
testcase_11 AC 48 ms
6,676 KB
testcase_12 AC 51 ms
8,612 KB
testcase_13 AC 51 ms
6,676 KB
testcase_14 AC 67 ms
8,304 KB
testcase_15 AC 57 ms
6,676 KB
testcase_16 AC 50 ms
8,188 KB
testcase_17 AC 59 ms
8,192 KB
testcase_18 AC 52 ms
6,676 KB
testcase_19 AC 57 ms
8,708 KB
testcase_20 AC 58 ms
8,516 KB
testcase_21 AC 50 ms
6,676 KB
testcase_22 AC 62 ms
8,568 KB
testcase_23 AC 47 ms
6,676 KB
testcase_24 AC 58 ms
6,676 KB
testcase_25 AC 84 ms
8,884 KB
testcase_26 AC 45 ms
9,036 KB
testcase_27 AC 45 ms
9,036 KB
testcase_28 AC 44 ms
9,036 KB
testcase_29 AC 47 ms
9,036 KB
testcase_30 AC 66 ms
8,780 KB
testcase_31 AC 68 ms
8,848 KB
testcase_32 AC 65 ms
7,672 KB
testcase_33 AC 64 ms
7,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let M = input!(usize);
    let K = input!(usize);

    let S = input!(usize, K + 1);

    let mut S = (0..=K).map(|i| S[i] - 1).collect::<Vec<_>>();

    let edges = (0..M)
        .map(|_| (input!(usize) - 1, input!(usize) - 1, input!(usize)))
        .collect::<Vec<_>>();

    let mut dist = vec![vec![usize::MAX; N]; N];

    for (u, v, w) in edges {
        dist[u][v] = w;
        dist[v][u] = w;
    }

    for i in 0..N {
        dist[i][i] = 0;
    }

    for k in 0..N {
        for i in 0..N {
            for j in 0..N {
                if dist[i][k] == usize::MAX {
                    continue;
                }

                if dist[k][j] == usize::MAX {
                    continue;
                }

                if dist[i][j] > dist[i][k] + dist[k][j] {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }

    let mut stree = direct_segtree::SegmentTree::new(K, |x, y| x + y, 0);

    for i in 0..K {
        stree.insert(i, dist[S[i]][S[i + 1]]);
    }

    let Q = input!(usize);

    for _ in 0..Q {
        let t = input!(usize);
        let X = input!(usize);
        let Y = input!(usize);

        if t == 1 {
            S[X] = Y - 1;

            if X > 0 {
                stree.insert(X - 1, dist[S[X - 1]][S[X]]);
            }

            if X + 1 <= K {
                stree.insert(X, dist[S[X]][S[X + 1]]);
            }
        } else {
            let ans = stree.prod(X..Y);
            writeln!(out, "{}", ans);
        }
    }
}

pub mod direct_segtree {
    pub struct SegmentTree<M> {
        size: usize,
        tree: Vec<M>,
        op: fn(&M, &M) -> M,
        id: M,
    }

    impl<M: Copy + PartialEq> SegmentTree<M> {
        /// self = [id; size], self.op = op, self.id = id
        pub fn new(size: usize, op: fn(&M, &M) -> M, id: M) -> Self {
            return Self {
                size: size,
                tree: vec![id; 2 * size],
                op: op,
                id: id,
            };
        }

        /// self = arr, self.op = op, self.id = id
        pub fn from(arr: &[M], op: fn(&M, &M) -> M, id: M) -> Self {
            let size = arr.len();
            let mut tree = vec![id; 2 * size];

            for i in 0..size {
                tree[i + size] = arr[i];

                assert!(
                    op(&id, &arr[i]) == arr[i],
                    "id is not the identity element of given operator"
                );
            }

            for i in (1..size).rev() {
                tree[i] = op(&tree[i << 1], &tree[i << 1 | 1]);
            }

            return Self {
                size: size,
                tree: tree,
                op: op,
                id: id,
            };
        }

        /// self[pos] <- value
        pub fn insert(&mut self, mut pos: usize, value: M) -> () {
            pos += self.size;
            self.tree[pos] = value;

            while pos > 1 {
                pos >>= 1;
                self.tree[pos] = (self.op)(&self.tree[pos << 1], &self.tree[pos << 1 | 1]);
            }
        }

        /// return self[pos]
        pub fn get_point(&self, pos: usize) -> M {
            return self[pos];
        }

        /// return Π_{i ∈ [left, right)} self[i]
        pub fn get(&self, left: usize, right: usize) -> M {
            let (mut l, mut r) = (left + self.size, right + self.size);
            let (mut vl, mut vr) = (self.id, self.id);

            while l < r {
                if l & 1 == 1 {
                    vl = (self.op)(&vl, &self.tree[l]);
                    l += 1;
                }

                if r & 1 == 1 {
                    r -= 1;
                    vr = (self.op)(&self.tree[r], &vr);
                }

                l >>= 1;
                r >>= 1;
            }

            return (self.op)(&vl, &vr);
        }

        /// return Π_{i ∈ range} self[i]
        pub fn prod<R: std::ops::RangeBounds<usize>>(&self, range: R) -> M {
            let left = match range.start_bound() {
                std::ops::Bound::Included(&l) => l,
                std::ops::Bound::Excluded(&l) => l + 1,
                std::ops::Bound::Unbounded => 0,
            };

            let right = match range.end_bound() {
                std::ops::Bound::Included(&r) => r + 1,
                std::ops::Bound::Excluded(&r) => r,
                std::ops::Bound::Unbounded => self.size,
            };

            return self.get(left, right);
        }
    }

    impl<M> std::ops::Index<usize> for SegmentTree<M> {
        type Output = M;
        fn index(&self, index: usize) -> &Self::Output {
            &self.tree[index + self.size]
        }
    }

    impl<M: std::fmt::Display> std::fmt::Display for SegmentTree<M> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(
                f,
                "{}",
                self.tree[self.size..]
                    .iter()
                    .map(|x| x.to_string())
                    .collect::<Vec<_>>()
                    .join(" ")
            )
        }
    }
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0