#[allow(dead_code)] #[allow(unused_imports)] fn read() -> T { use std::io::*; let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } fn root (par:&Vec, mut x:usize) -> usize { while par[x] != x { x = par[x]; } x } fn main(){ let n:usize = read(); let query:usize = read(); let mut par:Vec = (0..n).collect(); let mut sum = vec![0i64;n]; let mut sz = vec![1;n]; for _ in 0..query { let t:usize = read(); let mut a:usize = read(); let mut b:i64 = read(); if t == 1 { a -= 1; let b = b as usize - 1; a = root(&par,a); let mut b = root(&par,b); if a != b { if sz[a] < sz[b] { std::mem::swap(&mut a,&mut b); } sz[a] += sz[b]; sum[b] -= sum[a]; par[b] = a; } }else if t == 2 { a -= 1; a = root(&par,a); sum[a] += b; }else { a -= 1; let mut x = a; let mut ans = sum[x]; while x != par[x] { x = par[x]; ans += sum[x]; } println!("{}",ans); } } }