#![allow(unused_parens)] #![allow(unused_imports)] #![allow(non_upper_case_globals)] #![allow(non_snake_case)] #![allow(unused_mut)] #![allow(unused_variables)] #![allow(dead_code)] type Vec2 = Vec>; type Vec3 = Vec>>; #[allow(unused_macros)] macro_rules! invec { ( $ t : ty ) => {{ let mut s = String::new(); match std::io::stdin().read_line(&mut s) { Ok(0) => Vec::<$t>::new(), Ok(n) => s .trim() .split_whitespace() .map(|s| s.parse::<$t>().unwrap()) .collect::>(), Err(_) => Vec::<$t>::new(), } }}; } #[allow(unused_macros)] macro_rules! get { ($t:ty) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse::<$t>().unwrap() } }; ($($t:ty),*) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( $(iter.next().unwrap().parse::<$t>().unwrap(),)* ) } }; ($t:ty; $n:expr) => { (0..$n).map(|_| get!($t) ).collect::>() }; ($($t:ty),*; $n:expr) => { (0..$n).map(|_| get!($($t),*) ).collect::>() }; ($t:ty ;;) => { { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.split_whitespace() .map(|t| t.parse::<$t>().unwrap()) .collect::>() } }; ($t:ty ;; $n:expr) => { (0..$n).map(|_| get!($t ;;)).collect::>() }; } #[allow(unused_macros)] macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let mut s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => { let mut $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } #[allow(unused_macros)] macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($next:expr, [$t:tt]) => { { let len = read_value!($next, usize); (0..len).map(|_| read_value!($next, $t)).collect::>() } }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } #[allow(unused_macros)] #[cfg(debug_assertions)] macro_rules! mydbg { //($arg:expr) => (dbg!($arg)) //($arg:expr) => (println!("{:?}",$arg)); ($($a:expr),*) => { eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*); } } #[cfg(not(debug_assertions))] macro_rules! mydbg { ($($arg:expr),*) => {}; } macro_rules! echo { ($($a:expr),*) => { $(println!("{}",$a))* } } use std::cmp::*; use std::collections::*; use std::ops::{Add, Div, Mul, Rem, Sub}; trait SafeRangeContain { fn safe_contains(&self, x: i64) -> bool; } impl SafeRangeContain for std::ops::Range { fn safe_contains(&self, x: i64) -> bool { if x < 0 { return false; } return self.contains(&(x as usize)); } } #[allow(dead_code)] static INF_I64: i64 = i64::max_value() / 2; #[allow(dead_code)] static INF_I32: i32 = i32::max_value() / 2; #[allow(dead_code)] static INF_USIZE: usize = usize::max_value() / 2; #[allow(dead_code)] static M_O_D: usize = 1000000007; #[allow(dead_code)] static PAI: f64 = 3.1415926535897932; trait IteratorExt: Iterator { fn toVec(self) -> Vec; } impl IteratorExt for T { fn toVec(self) -> Vec { self.collect() } } trait CharExt { fn toNum(&self) -> usize; fn toAlphabetIndex(&self) -> usize; fn toNumIndex(&self) -> usize; } impl CharExt for char { fn toNum(&self) -> usize { return *self as usize; } fn toAlphabetIndex(&self) -> usize { return self.toNum() - 'a' as usize; } fn toNumIndex(&self) -> usize { return self.toNum() - '0' as usize; } } trait VectorExt { fn joinToString(&self, s: &str) -> String; } impl VectorExt for Vec { fn joinToString(&self, s: &str) -> String { return self .iter() .map(|x| x.to_string()) .collect::>() .join(s); } } trait StringExt { fn get_reverse(&self) -> String; } impl StringExt for String { fn get_reverse(&self) -> String { self.chars().rev().collect::() } } trait UsizeExt { fn pow(&self, n: usize) -> usize; } impl UsizeExt for usize { fn pow(&self, n: usize) -> usize { return ((*self as u64).pow(n as u32)) as usize; } } //https://github.com/rust-lang-ja/ac-library-rs pub mod dsu { /// Implement (union by size) + (path compression) /// Reference: /// Zvi Galil and Giuseppe F. Italiano, /// Data structures and algorithms for disjoint set union problems pub struct Dsu { n: usize, // root node: -1 * component size // otherwise: parent k: usize, parent_or_size: Vec, } impl Dsu { // 0 <= size <= 10^8 is constrained. pub fn new(size: usize) -> Self { Self { n: size, k: size, parent_or_size: vec![-1; size], } } pub fn merge(&mut self, a: usize, b: usize) -> usize { assert!(a < self.n); assert!(b < self.n); let (mut x, mut y) = (self.leader(a), self.leader(b)); if x == y { return x; } if !self.same(a, b) { self.k -= 1; } if -self.parent_or_size[x] < -self.parent_or_size[y] { std::mem::swap(&mut x, &mut y); } self.parent_or_size[x] += self.parent_or_size[y]; self.parent_or_size[y] = x as i32; x } pub fn same(&mut self, a: usize, b: usize) -> bool { assert!(a < self.n); assert!(b < self.n); self.leader(a) == self.leader(b) } pub fn leader(&mut self, a: usize) -> usize { assert!(a < self.n); if self.parent_or_size[a] < 0 { return a; } self.parent_or_size[a] = self.leader(self.parent_or_size[a] as usize) as i32; self.parent_or_size[a] as usize } pub fn groupsize(&self) -> usize { self.k } pub fn size(&mut self, a: usize) -> usize { assert!(a < self.n); let x = self.leader(a); -self.parent_or_size[x] as usize } pub fn groups(&mut self) -> Vec> { let mut leader_buf = vec![0; self.n]; let mut group_size = vec![0; self.n]; for i in 0..self.n { leader_buf[i] = self.leader(i); group_size[leader_buf[i]] += 1; } let mut result = vec![Vec::new(); self.n]; for i in 0..self.n { result[i].reserve(group_size[i]); } for i in 0..self.n { result[leader_buf[i]].push(i); } result .into_iter() .filter(|x| !x.is_empty()) .collect::>>() } } #[cfg(test)] mod tests { use super::*; #[test] fn dsu_works() { let mut d = Dsu::new(4); d.merge(0, 1); assert_eq!(d.same(0, 1), true); d.merge(1, 2); assert_eq!(d.same(0, 2), true); assert_eq!(d.size(0), 3); assert_eq!(d.same(0, 3), false); assert_eq!(d.groups(), vec![vec![0, 1, 2], vec![3]]); assert_eq!(d.groupsize(), 2); } } } use dsu::*; fn main() { solve(); } fn solve() { let (N, Q) = get!(usize, usize); let mut query = vec![]; for _ in 0..Q { let (t, a, b) = get!(usize, usize, i64); query.push((t, a, b)); } let mut dsu = Dsu::new(N); let mut s = vec![0_i64; N]; let mut A = vec![vec![]; N]; let mut X = vec![0_i64; N]; for i in 0..N as usize { A[i].push(i); } let mut ans = vec![]; for &(t, a, b) in &query { if t == 1 { let (mut u, mut v) = (a - 1, b as usize - 1); if !dsu.same(u, v) { if dsu.size(u) > dsu.size(v) { std::mem::swap(&mut u, &mut v); } let l = dsu.leader(u); let l2 = dsu.leader(v); for &c in &A[l] { X[c] -= s[l2]; X[c] += s[l]; } let tmp = std::mem::take(&mut A[l]); for &i in &tmp { A[l2].push(i); } dsu.merge(u, v); let l3 = dsu.leader(v); if l2 != l3 { s[l3] = s[l2]; let tmp = std::mem::take(&mut A[l2]); A[l3] = tmp; } } } else if t == 2 { let u = a - 1; let l = dsu.leader(u); s[l] += b; } else { let mut u = a - 1; let l = dsu.leader(u); ans.push(X[u] + s[l]); } } mydbg!(X); echo!(ans.joinToString(("\n"))); }