結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | koba-e964 |
提出日時 | 2023-07-03 11:02:42 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 791 ms / 4,000 ms |
コード長 | 5,515 bytes |
コンパイル時間 | 13,176 ms |
コンパイル使用メモリ | 399,176 KB |
実行使用メモリ | 87,924 KB |
最終ジャッジ日時 | 2024-07-17 08:28:15 |
合計ジャッジ時間 | 47,530 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 1 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 669 ms
86,252 KB |
testcase_04 | AC | 607 ms
50,344 KB |
testcase_05 | AC | 600 ms
44,252 KB |
testcase_06 | AC | 671 ms
86,148 KB |
testcase_07 | AC | 684 ms
86,428 KB |
testcase_08 | AC | 637 ms
85,972 KB |
testcase_09 | AC | 628 ms
86,016 KB |
testcase_10 | AC | 757 ms
86,144 KB |
testcase_11 | AC | 763 ms
86,144 KB |
testcase_12 | AC | 764 ms
86,044 KB |
testcase_13 | AC | 681 ms
85,976 KB |
testcase_14 | AC | 590 ms
84,600 KB |
testcase_15 | AC | 483 ms
84,808 KB |
testcase_16 | AC | 791 ms
85,260 KB |
testcase_17 | AC | 648 ms
87,924 KB |
testcase_18 | AC | 687 ms
87,700 KB |
testcase_19 | AC | 723 ms
87,164 KB |
testcase_20 | AC | 740 ms
86,784 KB |
testcase_21 | AC | 750 ms
86,816 KB |
testcase_22 | AC | 693 ms
86,992 KB |
testcase_23 | AC | 708 ms
87,440 KB |
testcase_24 | AC | 693 ms
87,040 KB |
testcase_25 | AC | 1 ms
5,376 KB |
testcase_26 | AC | 1 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 500 ms
6,144 KB |
testcase_29 | AC | 529 ms
10,496 KB |
testcase_30 | AC | 514 ms
14,720 KB |
testcase_31 | AC | 515 ms
19,120 KB |
testcase_32 | AC | 548 ms
23,040 KB |
testcase_33 | AC | 567 ms
27,392 KB |
testcase_34 | AC | 583 ms
31,496 KB |
testcase_35 | AC | 596 ms
35,840 KB |
testcase_36 | AC | 596 ms
39,936 KB |
testcase_37 | AC | 616 ms
44,160 KB |
testcase_38 | AC | 622 ms
48,384 KB |
testcase_39 | AC | 641 ms
52,480 KB |
testcase_40 | AC | 646 ms
56,576 KB |
testcase_41 | AC | 643 ms
60,928 KB |
testcase_42 | AC | 652 ms
65,152 KB |
testcase_43 | AC | 664 ms
69,376 KB |
testcase_44 | AC | 670 ms
73,472 KB |
testcase_45 | AC | 680 ms
77,440 KB |
testcase_46 | AC | 684 ms
81,860 KB |
testcase_47 | AC | 690 ms
86,016 KB |
testcase_48 | AC | 415 ms
84,368 KB |
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::Read; fn get_word() -> String { let stdin = std::io::stdin(); let mut stdin=stdin.lock(); let mut u8b: [u8; 1] = [0]; loop { let mut buf: Vec<u8> = Vec::with_capacity(16); loop { let res = stdin.read(&mut u8b); if res.unwrap_or(0) == 0 || u8b[0] <= b' ' { break; } else { buf.push(u8b[0]); } } if buf.len() >= 1 { let ret = String::from_utf8(buf).unwrap(); return ret; } } } fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() } // Quick-Find data structure. // Verified by: https://atcoder.jp/contests/cf17-tournament-round3-open/submissions/22928265 // Verified by: https://atcoder.jp/contests/ttpc2019/submissions/23384553 (polymorphic version) // Verified by: https://yukicoder.me/submissions/727881 (polymorphic version) #[derive(Clone)] struct QuickFind<T = ()> { root: Vec<usize>, mem: Vec<Vec<usize>>, dat: Vec<T>, default: T, } impl QuickFind<()> { #[allow(unused)] fn new(n: usize) -> Self { Self::with_dat(n, ()) } #[allow(unused)] fn unite(&mut self, x: usize, y: usize) { self.unite_with_hooks(x, y, |&(), _| (), |(), (), _| ()); } } impl<T: Clone> QuickFind<T> { fn with_dat(n: usize, def: T) -> Self { let root = (0..n).collect(); let mut mem = vec![vec![]; n]; for i in 0..n { mem[i] = vec![i]; } QuickFind { root: root, mem: mem, dat: vec![def.clone(); n], default: def } } fn root(&self, x: usize) -> usize { self.root[x] } #[allow(unused)] fn set(&mut self, idx: usize, val: T) { self.apply(idx, move |me| *me = val); } #[allow(unused)] fn get(&mut self, idx: usize) -> T { let mut ans = self.default.clone(); self.apply(idx, |me| ans = me.clone()); ans } fn apply<F: FnOnce(&mut T)>(&mut self, idx: usize, f: F) { let r = self.root[idx]; f(&mut self.dat[r]); } // unite always merges y to x if |x| >= |y|. fn unite_with_hooks<F: FnMut(&T, i64), G: FnMut(T, T, &[usize]) -> T>( &mut self, x: usize, y: usize, mut hook: F, mut merge: G) { let mut x = self.root(x); let mut y = self.root(y); if x == y { return } if self.mem[x].len() < self.mem[y].len() { std::mem::swap(&mut x, &mut y); } let memy = std::mem::replace(&mut self.mem[y], vec![]); for &v in &memy { self.root[v] = x; } self.mem[x].extend_from_slice(&memy); // hook hook(&self.dat[x], -1); hook(&self.dat[y], -1); self.dat[x] = merge( std::mem::replace(&mut self.dat[x], self.default.clone()), std::mem::replace(&mut self.dat[y], self.default.clone()), &memy, ); hook(&self.dat[x], 1); } #[allow(unused)] fn is_same_set(&self, x: usize, y: usize) -> bool { self.root(x) == self.root(y) } #[allow(unused)] fn size(&self, x: usize) -> usize { let x = self.root(x); self.mem[x].len() } } // https://yukicoder.me/problems/no/2294 (3.5) // 30 個の桁ごとに独立な問題 (重みが 0 か 1) を解くと思うことができる。 // 連結性とある頂点からの重みの和の偶奇は UnionFind か QuickFind でできる。 // -> TLE。QuickFind そのものは複数ある必要はないが、複数持ってしまったため。 fn main() { let n: usize = get(); let mut x: usize = get(); let q: usize = get(); let mut qf = QuickFind::with_dat(n, [(1, 0); 30]); let mut wei = vec![vec![0; n]; 30]; for _ in 0..q { let ty: i32 = get(); if ty == 1 { let v: usize = get(); let w: u32 = get(); qf.unite_with_hooks(x, v, |_, _| (), |a, b, less| { let mut res = [(0, 0); 30]; for i in 0..30 { let w = (w >> i) & 1; if w != wei[i][x] ^ wei[i][v] { for &v in less { wei[i][v] ^= 1; } res[i] = (a[i].0 + b[i].1, a[i].1 + b[i].0); } else { res[i] = (a[i].0 + b[i].0, a[i].1 + b[i].1); } } res }); continue; } if ty == 2 { let u: usize = get(); let v: usize = get(); if qf.root(u) != qf.root(v) { println!("-1"); continue; } let mut ans = 0u32; for i in 0..30 { ans |= (wei[i][u] ^ wei[i][v]) << i; } println!("{}", ans); x = (x + ans as usize) % n; continue; } if ty == 3 { let v: usize = get(); let mut ans = 0i64; const MOD: i64 = 998_244_353; let val = qf.get(v); for i in 0..30 { let (a, b) = val[i]; ans += (a as i64 * b as i64) % MOD << i; ans %= MOD; } println!("{}", ans); continue; } let val: usize = get(); x = (x + val) % n; } }