Skip to content

Commit e92e269

Browse files
Merge branch 'main' into deque
2 parents 23177b2 + e3b286d commit e92e269

File tree

16 files changed

+499
-90
lines changed

16 files changed

+499
-90
lines changed

Cargo.toml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,8 @@ name = "hld_path_composite_yosupo"
125125
path = "examples/graphs/hld_path_composite_yosupo.rs"
126126

127127
[[example]]
128-
name = "hld_jump_on_tree_nodes"
129-
path = "examples/graphs/hld_jump_on_tree_nodes.rs"
130-
131-
[[example]]
132-
name = "hld_jump_on_tree_edges"
133-
path = "examples/graphs/hld_jump_on_tree_edges.rs"
128+
name = "hld_jump_on_path"
129+
path = "examples/graphs/hld_jump_on_path.rs"
134130

135131
[[example]]
136132
name = "hopcroft_karp_yosupo"
@@ -201,13 +197,25 @@ name = "lis_yosupo"
201197
path = "examples/helpers/lis_yosupo.rs"
202198

203199
[[example]]
204-
name = "lis_pop"
205-
path = "examples/helpers/lis_pop.rs"
200+
name = "lis_handmade"
201+
path = "examples/helpers/lis_handmade.rs"
202+
203+
[[example]]
204+
name = "range_container_aizu"
205+
path = "examples/data_structures/range_container_aizu.rs"
206+
207+
[[example]]
208+
name = "range_container_handmade"
209+
path = "examples/data_structures/range_container_handmade.rs"
206210

207211
[[example]]
208212
name = "mono_st"
209213
path = "examples/monotonic/mono_st.rs"
210214

215+
[[example]]
216+
name = "hld_aux_tree"
217+
path = "examples/graphs/hld_aux_tree.rs"
218+
211219
[[example]]
212220
name = "count_rects"
213221
path = "examples/monotonic/count_rects.rs"
@@ -231,3 +239,7 @@ path = "examples/data_structures/disjoint_rmq_non_commutative.rs"
231239
[[example]]
232240
name = "lca_rmq_next_on_path"
233241
path = "examples/graphs/lca_rmq_next_on_path.rs"
242+
243+
[[example]]
244+
name = "linear_rmq"
245+
path = "examples/data_structures/linear_rmq.rs"

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- tests are named `[algo].rs`
33
- use both yosupo and aizu to test whenever possible because bugs have existed on one of the sites but not the other
44
- when using both sites name the files `[algo]_yosupo.rs` and `[algo]_aizu.rs`
5+
- when there's no problem to test on, test on [hello world](https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/all/ITP1_1_A) and name the files `[algo]_handmade.rs`
56
- when only testing a specific function or componenet of some algorithm name the file `[algo]_[component].rs`
67

78
# Documentation Guidelines
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/staticrmq
2+
3+
use proconio::input;
4+
use programming_team_code_rust::data_structures::linear_rmq::LinearRMQ;
5+
6+
fn main() {
7+
input! {
8+
n: usize,
9+
q: usize,
10+
a: [usize; n],
11+
}
12+
13+
let rmq = LinearRMQ::new(&a, |&x, &y| x.lt(&y));
14+
for _ in 0..q {
15+
input! {
16+
le: usize,
17+
ri: usize,
18+
}
19+
let idx_min = rmq.query_idx(le..ri);
20+
assert!((le..ri).contains(&idx_min));
21+
let res = rmq.query(le..ri);
22+
assert_eq!(a[idx_min], *res);
23+
println!("{}", res);
24+
}
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/DSL_2_D
2+
3+
use proconio::input;
4+
use programming_team_code_rust::data_structures::range_container::RangeContainer;
5+
6+
fn main() {
7+
input! {
8+
n: usize,
9+
q: usize,
10+
}
11+
let mut rc = RangeContainer::default();
12+
let mut to_value = vec![i32::MAX; 2 * n + 2];
13+
rc.insert_range(0..(2 * n + 1) as i32);
14+
15+
for _ in 0..q {
16+
input! {
17+
kind: usize,
18+
}
19+
match kind {
20+
0 => {
21+
input! {
22+
le: i32,
23+
ri: i32,
24+
x: i32,
25+
}
26+
let le = 2 * le;
27+
let ri = 2 * ri;
28+
let save_range = rc.get_range(ri + 2).unwrap();
29+
let save_value = to_value[save_range.start as usize];
30+
rc.remove_range(le - 1..ri + 2);
31+
rc.insert_range(le..ri + 1);
32+
let save_range = rc.get_range(ri + 2).unwrap();
33+
to_value[save_range.start as usize] = save_value;
34+
to_value[le as usize] = x;
35+
}
36+
_ => {
37+
input! {
38+
index: i32,
39+
}
40+
let range_containing = rc.get_range(2 * index).unwrap();
41+
println!("{}", to_value[range_containing.start as usize]);
42+
}
43+
}
44+
}
45+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/all/ITP1_1_A
2+
3+
use programming_team_code_rust::data_structures::range_container::RangeContainer;
4+
use rand::{thread_rng, Rng};
5+
use std::collections::BTreeMap;
6+
7+
fn main() {
8+
let mut rng = thread_rng();
9+
for _ in 0..100 {
10+
let max_n = rng.gen_range(1..100);
11+
let mut vis = vec![false; max_n + 1];
12+
let mut rc = RangeContainer::default();
13+
for _ in 0..100 {
14+
let mut le = rng.gen_range(0..=max_n);
15+
let mut ri = rng.gen_range(0..=max_n);
16+
if le > ri {
17+
(le, ri) = (ri, le);
18+
}
19+
match rng.gen_range(0..2) {
20+
0 => {
21+
rc.insert_range(le as i32..ri as i32);
22+
for elem in vis.iter_mut().take(ri).skip(le) {
23+
*elem = true;
24+
}
25+
}
26+
_ => {
27+
rc.remove_range(le as i32..ri as i32);
28+
for elem in vis.iter_mut().take(ri).skip(le) {
29+
*elem = false;
30+
}
31+
}
32+
}
33+
let mut to_end = vec![None; max_n + 1];
34+
for i in (0..max_n).rev() {
35+
if vis[i] && !vis[i + 1] {
36+
to_end[i] = Some(i + 1);
37+
} else if vis[i] {
38+
to_end[i] = to_end[i + 1];
39+
}
40+
}
41+
let mut naive_mp = BTreeMap::<i32, i32>::new();
42+
let mut start = None;
43+
for i in 0..max_n + 1 {
44+
if vis[i] {
45+
if start.is_none() {
46+
start = Some(i);
47+
}
48+
assert_eq!(
49+
rc.get_range(i as i32).unwrap(),
50+
start.unwrap() as i32..to_end[i].unwrap() as i32
51+
);
52+
} else {
53+
assert_eq!(rc.get_range(i as i32), None);
54+
if let Some(curr_start) = start {
55+
naive_mp.insert(curr_start as i32, i as i32);
56+
}
57+
start = None;
58+
}
59+
}
60+
assert_eq!(rc.mp, naive_mp);
61+
}
62+
}
63+
println!("Hello World");
64+
}

examples/graphs/hld_aux_tree.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/GRL_5_B
2+
3+
use proconio::input;
4+
use programming_team_code_rust::graphs::hld::HLD;
5+
use std::collections::VecDeque;
6+
7+
fn main() {
8+
input! {
9+
n: usize,
10+
edges: [(usize, usize, u32); n - 1],
11+
}
12+
13+
let mut adj_weighted = vec![vec![]; n];
14+
let mut adj = vec![vec![]; n];
15+
for &(u, v, w) in &edges {
16+
adj[u].push(v);
17+
adj[v].push(u);
18+
adj_weighted[u].push((v, w));
19+
adj_weighted[v].push((u, w));
20+
}
21+
let adj_weighted = adj_weighted;
22+
23+
let mut dist = vec![0; n];
24+
{
25+
fn dfs(u: usize, p: Option<usize>, adj_weighted: &[Vec<(usize, u32)>], dist: &mut [u32]) {
26+
for &(v, w) in &adj_weighted[u] {
27+
if Some(v) == p {
28+
continue;
29+
}
30+
dist[v] = w + dist[u];
31+
dfs(v, Some(u), adj_weighted, dist);
32+
}
33+
}
34+
dfs(0, None, &adj_weighted, &mut dist);
35+
}
36+
let dist = dist;
37+
38+
let hld = HLD::new(&mut adj, true);
39+
40+
let weighted_dist = |u: usize, v: usize| -> u32 {
41+
let lc = hld.lca(u, v);
42+
dist[u] + dist[v] - 2 * dist[lc]
43+
};
44+
45+
let mut diam_u = 0;
46+
for i in 1..n {
47+
if weighted_dist(0, i) > weighted_dist(0, diam_u) {
48+
diam_u = i;
49+
}
50+
}
51+
let mut diam_v = 0;
52+
for i in 1..n {
53+
if weighted_dist(diam_u, i) > weighted_dist(diam_u, diam_v) {
54+
diam_v = i;
55+
}
56+
}
57+
58+
for u in 0..n {
59+
let (par, to_node) = hld.aux_tree(vec![diam_u, diam_v, u]);
60+
let mut aux_adj = vec![vec![]; par.len()];
61+
for i in 1..par.len() {
62+
let edge_w = dist[to_node[i]] - dist[to_node[par[i]]];
63+
aux_adj[i].push((par[i], edge_w));
64+
aux_adj[par[i]].push((i, edge_w));
65+
}
66+
let mut q = VecDeque::new();
67+
q.push_back((to_node.iter().position(|&x| x == u).unwrap(), None, 0));
68+
let mut res = 0;
69+
while let Some((node, parent, curr_dist)) = q.pop_front() {
70+
res = res.max(curr_dist);
71+
for &(v, w) in &aux_adj[node] {
72+
if Some(v) == parent {
73+
continue;
74+
}
75+
q.push_back((v, Some(node), curr_dist + w));
76+
}
77+
}
78+
println!("{}", res);
79+
}
80+
}

examples/graphs/hld_jump_on_tree_edges.rs renamed to examples/graphs/hld_jump_on_path.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ fn main() {
2828
k: usize,
2929
}
3030

31+
assert_eq!(hld.kth_par(u, hld.d[u]), Some(0));
32+
assert_eq!(hld.kth_par(u, hld.d[u] + 1), None);
33+
3134
match hld.kth_on_path(u, v, k) {
3235
Some(w) => {
3336
assert!(k <= hld.dist(u, v));

examples/graphs/hld_jump_on_tree_nodes.rs

Lines changed: 0 additions & 59 deletions
This file was deleted.

examples/helpers/lis_pop.rs renamed to examples/helpers/lis_handmade.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// verification-helper: PROBLEM https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/all/ITP1_1_A
22

33
use programming_team_code_rust::helpers::lis::Lis;
4+
use rand::{thread_rng, Rng};
45

56
fn lis_quadratic(a: &[i32]) -> usize {
67
let n = a.len();
@@ -19,13 +20,14 @@ fn lis_quadratic(a: &[i32]) -> usize {
1920
}
2021

2122
fn main() {
23+
let mut rng = thread_rng();
2224
for _ in 0..100 {
2325
let mut lis = Lis::default();
2426
let mut a = Vec::new();
2527
for _ in 0..1000 {
26-
match rand::random::<u8>() % 3 {
28+
match rng.gen_range(0..3) {
2729
0 => {
28-
let new_num = rand::random::<i32>();
30+
let new_num = rng.r#gen();
2931
lis.push(new_num);
3032
a.push(new_num);
3133
}

0 commit comments

Comments
 (0)