結果
問題 | No.2296 Union Path Query (Hard) |
ユーザー | akakimidori |
提出日時 | 2023-04-23 04:38:06 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1,237 ms / 7,000 ms |
コード長 | 4,683 bytes |
コンパイル時間 | 13,813 ms |
コンパイル使用メモリ | 398,636 KB |
実行使用メモリ | 102,580 KB |
最終ジャッジ日時 | 2024-11-23 04:57:01 |
合計ジャッジ時間 | 46,796 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 43 ms
71,552 KB |
testcase_02 | AC | 43 ms
71,348 KB |
testcase_03 | AC | 43 ms
71,292 KB |
testcase_04 | AC | 509 ms
88,628 KB |
testcase_05 | AC | 410 ms
88,596 KB |
testcase_06 | AC | 623 ms
91,832 KB |
testcase_07 | AC | 544 ms
95,356 KB |
testcase_08 | AC | 571 ms
95,536 KB |
testcase_09 | AC | 407 ms
58,136 KB |
testcase_10 | AC | 462 ms
57,968 KB |
testcase_11 | AC | 466 ms
57,936 KB |
testcase_12 | AC | 403 ms
51,892 KB |
testcase_13 | AC | 92 ms
12,724 KB |
testcase_14 | AC | 71 ms
11,648 KB |
testcase_15 | AC | 557 ms
93,856 KB |
testcase_16 | AC | 538 ms
73,880 KB |
testcase_17 | AC | 292 ms
29,752 KB |
testcase_18 | AC | 652 ms
97,464 KB |
testcase_19 | AC | 487 ms
52,448 KB |
testcase_20 | AC | 680 ms
97,420 KB |
testcase_21 | AC | 630 ms
93,852 KB |
testcase_22 | AC | 613 ms
93,264 KB |
testcase_23 | AC | 291 ms
100,276 KB |
testcase_24 | AC | 274 ms
53,044 KB |
testcase_25 | AC | 312 ms
93,608 KB |
testcase_26 | AC | 314 ms
93,540 KB |
testcase_27 | AC | 1,033 ms
93,580 KB |
testcase_28 | AC | 1,030 ms
93,620 KB |
testcase_29 | AC | 1,113 ms
95,540 KB |
testcase_30 | AC | 1,142 ms
95,608 KB |
testcase_31 | AC | 1,237 ms
94,116 KB |
testcase_32 | AC | 1,175 ms
93,096 KB |
testcase_33 | AC | 1,028 ms
90,932 KB |
testcase_34 | AC | 792 ms
88,712 KB |
testcase_35 | AC | 723 ms
87,476 KB |
testcase_36 | AC | 549 ms
86,048 KB |
testcase_37 | AC | 1,068 ms
66,228 KB |
testcase_38 | AC | 1,013 ms
66,104 KB |
testcase_39 | AC | 1,117 ms
68,276 KB |
testcase_40 | AC | 1,115 ms
68,400 KB |
testcase_41 | AC | 1 ms
5,248 KB |
testcase_42 | AC | 1 ms
5,248 KB |
testcase_43 | AC | 1 ms
5,248 KB |
testcase_44 | AC | 568 ms
101,244 KB |
testcase_45 | AC | 585 ms
100,984 KB |
testcase_46 | AC | 610 ms
101,808 KB |
testcase_47 | AC | 650 ms
102,580 KB |
testcase_48 | AC | 618 ms
101,428 KB |
ソースコード
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; } }