結果
問題 | No.1054 Union add query |
ユーザー | koba-e964 |
提出日時 | 2021-09-17 09:54:51 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 392 ms / 2,000 ms |
コード長 | 5,019 bytes |
コンパイル時間 | 13,199 ms |
コンパイル使用メモリ | 376,704 KB |
実行使用メモリ | 54,368 KB |
最終ジャッジ日時 | 2024-06-29 17:23:10 |
合計ジャッジ時間 | 16,704 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 159 ms
22,784 KB |
testcase_04 | AC | 392 ms
54,368 KB |
testcase_05 | AC | 124 ms
18,268 KB |
testcase_06 | AC | 132 ms
29,184 KB |
testcase_07 | AC | 119 ms
29,184 KB |
testcase_08 | AC | 133 ms
29,184 KB |
testcase_09 | AC | 212 ms
48,764 KB |
testcase_10 | AC | 133 ms
44,928 KB |
ソースコード
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } // 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) 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) { let r = self.root[idx]; self.dat[r] = val; } #[allow(unused)] fn get(&self, idx: usize) -> &T { let r = self.root[idx]; &self.dat[r] } // unite always merges y to x if |x| >= |y|. fn unite_with_hooks<F: FnMut(&T, i64), G: FnMut(T, T) -> T, H: FnMut(usize, &T, &T)>( &mut self, x: usize, y: usize, mut hook: F, mut merge: G, mut smaller: H) { 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; smaller(v, &self.dat[x], &self.dat[y]); } 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()), ); 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() } } trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); } impl<T: PartialOrd> Change for T { fn chmax(&mut self, x: T) { if *self < x { *self = x; } } fn chmin(&mut self, x: T) { if *self > x { *self = x; } } } 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 out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts {($($format:tt)*) => (let _ = write!(out,$($format)*););} input! { n: usize, q: usize, tab: [(i32, usize1, i64); q], } let mut qf = QuickFind::with_dat(n, 0); let mut val = vec![0; n]; for (t, a, b) in tab { if t == 1 { let b = b as usize - 1; qf.unite_with_hooks(a, b, |_, _| (), |x, _| x, |idx, large, small| val[idx] += small - large); } else if t == 2 { let old = *qf.get(a); qf.set(a, old + b); } else { puts!("{}\n", *qf.get(a) + val[a]); } } }