結果

問題 No.2296 Union Path Query (Hard)
ユーザー akakimidoriakakimidori
提出日時 2023-04-23 04:38:06
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,069 ms / 7,000 ms
コード長 4,683 bytes
コンパイル時間 13,881 ms
コンパイル使用メモリ 377,356 KB
実行使用メモリ 102,580 KB
最終ジャッジ日時 2024-05-02 14:28:36
合計ジャッジ時間 39,526 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 49 ms
71,424 KB
testcase_02 AC 48 ms
71,424 KB
testcase_03 AC 48 ms
71,424 KB
testcase_04 AC 411 ms
88,756 KB
testcase_05 AC 413 ms
88,628 KB
testcase_06 AC 494 ms
91,952 KB
testcase_07 AC 417 ms
95,412 KB
testcase_08 AC 427 ms
95,412 KB
testcase_09 AC 296 ms
58,160 KB
testcase_10 AC 311 ms
58,036 KB
testcase_11 AC 305 ms
57,908 KB
testcase_12 AC 281 ms
51,888 KB
testcase_13 AC 72 ms
12,848 KB
testcase_14 AC 64 ms
11,776 KB
testcase_15 AC 485 ms
93,872 KB
testcase_16 AC 405 ms
73,908 KB
testcase_17 AC 171 ms
29,748 KB
testcase_18 AC 551 ms
97,588 KB
testcase_19 AC 341 ms
52,532 KB
testcase_20 AC 562 ms
97,584 KB
testcase_21 AC 510 ms
93,752 KB
testcase_22 AC 503 ms
93,360 KB
testcase_23 AC 267 ms
100,272 KB
testcase_24 AC 215 ms
53,044 KB
testcase_25 AC 263 ms
93,616 KB
testcase_26 AC 271 ms
93,600 KB
testcase_27 AC 823 ms
93,620 KB
testcase_28 AC 855 ms
93,748 KB
testcase_29 AC 999 ms
95,536 KB
testcase_30 AC 967 ms
95,540 KB
testcase_31 AC 1,069 ms
94,000 KB
testcase_32 AC 1,015 ms
93,108 KB
testcase_33 AC 851 ms
90,932 KB
testcase_34 AC 689 ms
88,628 KB
testcase_35 AC 586 ms
87,600 KB
testcase_36 AC 375 ms
85,940 KB
testcase_37 AC 911 ms
66,228 KB
testcase_38 AC 911 ms
66,104 KB
testcase_39 AC 940 ms
68,272 KB
testcase_40 AC 922 ms
68,272 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 0 ms
5,376 KB
testcase_43 AC 0 ms
5,376 KB
testcase_44 AC 500 ms
101,300 KB
testcase_45 AC 529 ms
100,920 KB
testcase_46 AC 553 ms
101,816 KB
testcase_47 AC 597 ms
102,580 KB
testcase_48 AC 559 ms
101,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Write;

fn main() {
    let (n, mut x, _, query) = read();
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    let mut solver = Solver::new(n);
    for (op, v, w) in query {
        if op == 1 {
            solver.unite(x, v, w as i64);
        } else if op == 2 {
            let ans = solver.dist(v, w).unwrap_or(-1);
            writeln!(out, "{}", ans).ok();
            if ans >= 0 {
                x += ans as usize;
            }
        } else if op == 3 {
            writeln!(out, "{}", solver.diameter(v)).ok();
        } else {
            x += v;
        }
        x %= n;
    }
}

fn read() -> (usize, usize, usize, Vec<(u8, usize, usize)>) {
    use std::io::*;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace().flat_map(|s| s.parse::<usize>());
    let mut next = || it.next().unwrap();
    let n = next();
    let x = next();
    let q = next();
    let mut ask = vec![(0, 0, 0); q];
    for ask in ask.iter_mut() {
        ask.0 = next() as u8;
        ask.1 = next();
        if ask.0 <= 2 {
            ask.2 = next();
        }
    }
    (n, x, q, ask)
}

struct Solver {
    dsu: Vec<i32>,
    tree: Vec<Vec<(usize, i64)>>,
    table: Vec<Vec<(usize, i64)>>,
    rad: Vec<(i64, usize, usize)>,
    depth: Vec<(usize, i64)>,
}

impl Solver {
    fn new(n: usize) -> Self {
        let ini = (0..n).map(|x| (x, 0)).collect::<Vec<_>>();
        let rad = (0..n).map(|x| (0, x, x)).collect::<Vec<_>>();
        let k = n.next_power_of_two().trailing_zeros();
        Self {
            dsu: vec![-1; n],
            tree: vec![vec![]; n],
            table: vec![ini; k as usize + 1],
            rad: rad,
            depth: vec![(0, 0); n],
        }
    }
    fn root(&mut self, x: usize) -> usize {
        if self.dsu[x] < 0 {
            x
        } else {
            self.dsu[x] = self.root(self.dsu[x] as usize) as i32;
            self.dsu[x] as usize
        }
    }
    fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }
    fn dist(&mut self, mut x: usize, mut y: usize) -> Option<i64> {
        if !self.same(x, y) {
            return None;
        }
        if self.depth[x].0 < self.depth[y].0 {
            std::mem::swap(&mut x, &mut y);
        }
        let d = self.depth[x].0 - self.depth[y].0;
        let mut ans = 0;
        for (i, table) in self.table.iter().enumerate() {
            if d >> i & 1 == 1 {
                ans += table[x].1;
                x = table[x].0;
            }
        }
        if x == y {
            return Some(ans);
        }
        for table in self.table.iter().rev() {
            if table[x].0 != table[y].0 {
                ans += table[x].1 + table[y].1;
                x = table[x].0;
                y = table[y].0;
            }
        }
        Some(ans + self.table[0][x].1 + self.table[0][y].1)
    }
    fn diameter(&mut self, x: usize) -> i64 {
        let r = self.root(x);
        self.rad[r].0
    }
    fn unite(&mut self, x: usize, y: usize, w: i64) {
        let a = self.root(x);
        let b = self.root(y);
        assert!(a != b);
        self.tree[x].push((y, w));
        self.tree[y].push((x, w));
        let (p, c) = if self.dsu[a] <= self.dsu[b] {
            self.dsu[a] += self.dsu[b];
            self.dsu[b] = a as i32;
            (x, y)
        } else {
            self.dsu[b] += self.dsu[a];
            self.dsu[a] = b as i32;
            (y, x)
        };
        let mut topo = vec![(c, p, w)];
        for i in 0.. {
            if i >= topo.len() {
                break;
            }
            let (v, p, w) = topo[i];
            let up = self.depth[p];
            self.depth[v] = (up.0 + 1, up.1 + w);
            self.table[0][v] = (p, w);
            for &(u, w) in self.tree[v].iter().filter(|e| e.0 != p) {
                topo.push((u, v, w));
            }
        }
        for i in 1..self.table.len() {
            let pre = std::mem::take(&mut self.table[i - 1]);
            let table = &mut self.table[i];
            for &(v, _, _) in topo.iter() {
                let a = pre[v];
                let b = pre[a.0];
                table[v] = (b.0, a.1 + b.1)
            }
            self.table[i - 1] = pre;
        }
        let da = self.rad[a];
        let db = self.rad[b];
        let mut res = std::cmp::max(da, db);
        for &a in [da.1, da.2].iter() {
            for &b in [db.1, db.2].iter() {
                res = res.max((self.dist(a, b).unwrap(), a, b));
            }
        }
        let r = self.root(a);
        self.rad[r] = res;
    }
}
0