結果

問題 No.1054 Union add query
ユーザー ziitaziita
提出日時 2020-05-15 22:55:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 447 ms / 2,000 ms
コード長 4,317 bytes
コンパイル時間 5,836 ms
コンパイル使用メモリ 188,676 KB
実行使用メモリ 18,640 KB
最終ジャッジ日時 2023-10-19 16:12:16
合計ジャッジ時間 9,459 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 322 ms
13,896 KB
testcase_04 AC 347 ms
18,580 KB
testcase_05 AC 316 ms
13,308 KB
testcase_06 AC 297 ms
15,072 KB
testcase_07 AC 271 ms
15,072 KB
testcase_08 AC 195 ms
15,072 KB
testcase_09 AC 447 ms
18,640 KB
testcase_10 AC 76 ms
14,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case, unused)]

use std::cmp::*;
use std::collections::*;
use std::ops::*;

// https://atcoder.jp/contests/hokudai-hitachi2019-1/submissions/10518254

macro_rules! eprint {
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = write!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! eprintln {
	() => { eprintln!(""); };
	($($t:tt)*) => {{
		use ::std::io::Write;
		let _ = writeln!(::std::io::stderr(), $($t)*);
	}};
}
macro_rules! dbg {
	($v:expr) => {{
		let val = $v;
		eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val);
		val
	}}
}

macro_rules! mat {
	($($e:expr),*) => { Vec::from(vec![$($e),*]) };
	($($e:expr,)*) => { Vec::from(vec![$($e),*]) };
	($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) };
	($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) };
}

macro_rules! ok {
	($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => {
		$a$([$i])*.$f($($t),*)
	};
	($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { {
		let t = $e;
		ok!($a$([$i])*.$f($($es),*)$(@$t)*@t)
	} };
}

pub fn readln() -> String {
	let mut line = String::new();
	::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e));
	line
}

macro_rules! read {
	($($t:tt),*; $n:expr) => {{
		let stdin = ::std::io::stdin();
		let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
			let line = line.unwrap();
			let mut it = line.split_whitespace();
			_read!(it; $($t),*)
		}).collect::<Vec<_>>();
		ret
	}};
	($($t:tt),*) => {{
		let line = readln();
		let mut it = line.split_whitespace();
		_read!(it; $($t),*)
	}};
}

macro_rules! _read {
	($it:ident; [char]) => {
		_read!($it; String).chars().collect::<Vec<_>>()
	};
	($it:ident; [u8]) => {
		Vec::from(_read!($it; String).into_bytes())
	};
	($it:ident; usize1) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
	};
	($it:ident; [usize1]) => {
		$it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
	};
	($it:ident; [$t:ty]) => {
		$it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
	};
	($it:ident; $t:ty) => {
		$it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
	};
	($it:ident; $($t:tt),+) => {
		($(_read!($it; $t)),*)
	};
}


pub fn main() {
	let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join();
}

const MOD: u32 = 1_000_000_007;
const INF: i64 = std::i64::MAX/2;

pub struct UnionFind {
    data: Vec<i32>,
    val: Vec<i64>,
}

impl UnionFind {
	pub fn new(n: usize) -> Self{
		UnionFind {
            data: vec![-1;n],
            val: vec![0;n]
		}
	}
	pub fn root(&mut self, x: usize) -> usize{
		if self.data[x]<0 {
			x
		}
		else {
			let p = self.data[x] as usize;
			self.root(p)
		}
	}
	pub fn unite(&mut self, x: usize, y: usize){
		let rx = self.root(x);
		let ry = self.root(y);
		if rx!=ry {
			let (rx,ry) = if self.data[rx]<=self.data[ry] {
				(rx,ry)
			}
			else{
				(ry,rx)
			};
			self.data[rx] += self.data[ry];
            self.data[ry] = rx as i32;
            self.val[ry] -= self.val[rx];
		}
	}
	pub fn same(&mut self, x: usize, y: usize) ->  bool{
		self.root(x) == self.root(y)
	}
	pub fn size(&mut self, x: usize) -> i32 {
		let r = self.root(x);
		- self.data[r]
    }
    pub fn add(&mut self, x: usize, v: i64) {
        let r = self.root(x);
        self.val[r] += v;
    }
    pub fn query(&mut self, x: usize) -> i64{
        if self.data[x]<0 {
			return self.val[x];
        }
        let mut sum = 0;
        sum += self.val[x];
        let p = self.data[x] as usize;
        sum += self.query(p);
        return sum;
    }
}

fn solve() {
    let (n,q) = read!(usize,usize);
    let tab = read!(i64,i64,i64;q);
    let mut uf = UnionFind::new(n);
    for (t,a,b) in tab {
        if t==1 {
            let s = (a-1) as usize;
            let t = (b-1) as usize;
            uf.unite(s,t);
        }
        if t==2 {
            let s = (a-1) as usize;
            uf.add(s,b);
        }
        if t==3 {
            let s = (a-1) as usize;
            println!("{}", uf.query(s));
        }
    }
}

fn run() {
    solve();
}
0