Submission #29137219
Source Code Expand
use dsu::*;
use proconio::{input, marker::Usize1};
fn main() {
input! {
n: usize,
q: usize,
lr: [(Usize1, Usize1); q],
};
let mut dsu = Dsu::new(n + 1);
for (l, r) in lr {
dsu.merge(l, r + 1);
}
let ans = dsu.same(0, n);
println!("{}", if ans { "Yes" } else { "No" });
}
//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
parent_or_size: Vec<i32>,
}
impl Dsu {
// 0 <= size <= 10^8 is constrained.
pub fn new(size: usize) -> Self {
Self {
n: 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.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 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<Vec<usize>> {
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::<Vec<Vec<usize>>>()
}
}
#[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]]);
}
}
}
Submission Info
| Submission Time |
|
| Task |
E - Range Sums |
| User |
bouzuya |
| Language |
Rust (1.42.0) |
| Score |
500 |
| Code Size |
3292 Byte |
| Status |
AC |
| Exec Time |
34 ms |
| Memory |
8460 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
500 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt, example2.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, example0.txt, example1.txt, example2.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
34 ms |
8348 KiB |
| 001.txt |
AC |
2 ms |
2020 KiB |
| 002.txt |
AC |
6 ms |
2772 KiB |
| 003.txt |
AC |
1 ms |
2072 KiB |
| 004.txt |
AC |
18 ms |
4824 KiB |
| 005.txt |
AC |
19 ms |
5932 KiB |
| 006.txt |
AC |
33 ms |
7268 KiB |
| 007.txt |
AC |
29 ms |
6844 KiB |
| 008.txt |
AC |
6 ms |
3200 KiB |
| 009.txt |
AC |
6 ms |
2636 KiB |
| 010.txt |
AC |
30 ms |
8372 KiB |
| 011.txt |
AC |
31 ms |
8244 KiB |
| 012.txt |
AC |
34 ms |
8352 KiB |
| 013.txt |
AC |
32 ms |
8460 KiB |
| 014.txt |
AC |
29 ms |
8372 KiB |
| 015.txt |
AC |
31 ms |
8356 KiB |
| 016.txt |
AC |
30 ms |
8404 KiB |
| 017.txt |
AC |
34 ms |
8268 KiB |
| 018.txt |
AC |
32 ms |
8336 KiB |
| 019.txt |
AC |
28 ms |
8308 KiB |
| 020.txt |
AC |
31 ms |
8408 KiB |
| 021.txt |
AC |
31 ms |
8424 KiB |
| 022.txt |
AC |
31 ms |
8336 KiB |
| 023.txt |
AC |
33 ms |
8324 KiB |
| 024.txt |
AC |
32 ms |
8400 KiB |
| 025.txt |
AC |
2 ms |
2272 KiB |
| 026.txt |
AC |
19 ms |
6592 KiB |
| 027.txt |
AC |
23 ms |
6456 KiB |
| 028.txt |
AC |
23 ms |
6512 KiB |
| example0.txt |
AC |
2 ms |
2140 KiB |
| example1.txt |
AC |
1 ms |
2100 KiB |
| example2.txt |
AC |
1 ms |
2128 KiB |