/
dynamic_segment_tree.rs
141 lines (129 loc) · 3.65 KB
/
dynamic_segment_tree.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
pub trait Magma: Sized + Clone {
fn op(&self, rhs: &Self) -> Self;
}
pub trait Associative: Magma {}
pub trait Unital: Magma {
fn identity() -> Self;
}
pub trait Monoid: Magma + Associative + Unital {}
impl<T: Magma + Associative + Unital> Monoid for T {}
pub enum Node<T: Monoid> {
Section(Box<Section<T>>),
Leaf(Leaf<T>),
None,
}
pub struct Section<T: Monoid> {
left: Node<T>,
right: Node<T>,
val: T,
}
pub struct Leaf<T: Monoid> {
i: usize,
val: T,
}
impl<T: Monoid> Leaf<T> {
fn new(i: usize, x: T) -> Self {
Leaf { i: i, val: x }
}
fn fold(&self) -> T { self.val.clone() }
}
impl<T: Monoid> Section<T> {
fn new() -> Self {
Section {
left: Node::None,
right: Node::None,
val: T::identity(),
}
}
fn fold(&self) -> T { self.val.clone() }
fn update(&mut self, i: usize, x: T, l: usize, r: usize) {
let m = (l + r) >> 1;
if i < m {
let left = self.left.take();
self.left = left.update(i, x, l, m);
}
else {
let right = self.right.take();
self.right = right.update(i, x, m, r);
}
self.val = self.left.fold().op(&self.right.fold());
}
}
impl<T: Monoid> Node<T> {
fn take(&mut self) -> Node<T> {
std::mem::replace(self, Node::None)
}
fn fold(&self) -> T {
match self {
&Node::Section(ref sec) => sec.as_ref().fold(),
&Node::Leaf(ref leaf) => leaf.fold(),
&Node::None => T::identity(),
}
}
fn update(self, i: usize, x: T, l: usize, r: usize) -> Self {
match self {
Node::Section(mut sec) => {
sec.as_mut().update(i, x, l, r);
Node::Section(sec)
}
Node::Leaf(leaf) => {
if leaf.i == i {
Node::Leaf(Leaf::new(i, x))
}
else {
let mut new_section = Section::new();
let m = (l + r) >> 1;
if leaf.i < m {
new_section.left = Node::Leaf(leaf);
}
else {
new_section.right = Node::Leaf(leaf);
}
new_section.update(i, x, l, r);
Node::Section(Box::new(new_section))
}
}
Node::None => {
Node::Leaf(Leaf::new(i, x))
}
}
}
fn range_fold(&self, a: usize, b: usize, l: usize, r: usize) -> T {
match self {
&Node::Section(ref sec) => {
if b <= l || r <= a { T::identity() }
else if a <= l && r <= b { sec.fold() }
else {
let m = (l + r) >> 1;
sec.left.range_fold(a, b, l, m).op(&sec.right.range_fold(a, b, m, r))
}
}
&Node::Leaf(ref leaf) => {
if a <= leaf.i && leaf.i < b { leaf.fold() }
else { T::identity() }
}
&Node::None => T::identity(),
}
}
}
pub struct DynamicSegmentTree<T: Monoid> {
root: Node<T>,
n: usize,
}
impl<T: Monoid> DynamicSegmentTree<T> {
pub fn new(sz: usize) -> Self {
let mut n = 1;
while n < sz { n = n << 1; }
DynamicSegmentTree {
root: Node::None,
n: n,
}
}
pub fn update(&mut self, i: usize, x: T) {
let r = self.root.take();
self.root = r.update(i, x, 0, self.n);
}
pub fn fold(&self, l: usize, r: usize) -> T {
self.root.range_fold(l, r, 0, self.n)
}
}