結果
問題 | No.2294 Union Path Query (Easy) |
ユーザー | koba-e964 |
提出日時 | 2023-07-03 10:57:28 |
言語 | Rust (1.83.0 + proconio) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,682 bytes |
コンパイル時間 | 17,560 ms |
コンパイル使用メモリ | 380,616 KB |
実行使用メモリ | 13,888 KB |
最終ジャッジ日時 | 2024-07-17 08:20:42 |
合計ジャッジ時間 | 25,831 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
13,888 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | TLE | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::Read; #[allow(dead_code)] fn getline() -> String { let mut ret = String::new(); std::io::stdin().read_line(&mut ret).ok().unwrap(); ret } 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 でできる。 fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); } fn solve() { let n: usize = get(); let mut x: usize = get(); let q: usize = get(); let mut qf = vec![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(); for i in 0..30 { let w = (w >> i) & 1; qf[i].unite_with_hooks(x, v, |_, _| (), |(a0, a1), (b0, b1), less| { if w != wei[i][x] ^ wei[i][v] { for &v in less { wei[i][v] ^= 1; } (a0 + b1, a1 + b0) } else { (a0 + b0, a1 + b1) } }); } continue; } if ty == 2 { let u: usize = get(); let v: usize = get(); if qf[0].root(u) != qf[0].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; for i in 0..30 { let (a, b) = qf[i].get(v); ans += (a as i64 * b as i64) % MOD << i; ans %= MOD; } println!("{}", ans); continue; } let val: usize = get(); x = (x + val) % n; } }