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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::cmp::max;
use std::cmp::min;
pub trait Monoid {
type T: Clone;
fn identity_element() -> Self::T;
fn binary_operation(a: &Self::T, b: &Self::T) -> Self::T;
}
pub struct Min {}
impl Monoid for Min {
type T = i64;
#[inline]
fn identity_element() -> Self::T {
std::i32::MAX as i64
}
#[inline]
fn binary_operation(a: &Self::T, b: &Self::T) -> Self::T {
min(*a, *b)
}
}
pub struct Max {}
impl Monoid for Max {
type T = i64;
#[inline]
fn identity_element() -> Self::T {
std::i64::MIN
}
#[inline]
fn binary_operation(a: &Self::T, b: &Self::T) -> Self::T {
max(*a, *b)
}
}
pub struct SegmentTree<M>
where
M: Monoid,
{
n: usize,
size: usize,
log: usize,
data: Vec<M::T>,
}
impl<M: Monoid> SegmentTree<M> {
pub fn new(n: usize) -> SegmentTree<M> {
vec![M::identity_element(); n].into()
}
}
impl<M: Monoid> From<Vec<M::T>> for SegmentTree<M> {
fn from(v: Vec<M::T>) -> Self {
let n = v.len();
let log = (32 - (n as u32).saturating_sub(1).leading_zeros()) as usize;
let size = 1 << log;
let data = {
let mut data = vec![M::identity_element(); 2 * size];
data[size..(size + n)].clone_from_slice(&v);
data
};
{
let mut sg = SegmentTree { n, size, log, data };
(1..size).rev().for_each(|i| sg.update(i));
sg
}
}
}
impl<M: Monoid> SegmentTree<M> {
pub fn query(&self, mut l: usize, mut r: usize) -> M::T {
let (mut sml, mut smr) = (M::identity_element(), M::identity_element());
l += self.size;
r += self.size;
while l < r {
if l & 1 != 0 {
sml = M::binary_operation(&sml, unsafe { self.data.get_unchecked(l) });
l += 1;
}
if r & 1 != 0 {
r -= 1;
smr = M::binary_operation(unsafe { self.data.get_unchecked(r) }, &smr);
}
l >>= 1;
r >>= 1;
}
M::binary_operation(&sml, &smr)
}
fn update(&mut self, k: usize) {
*unsafe { self.data.get_unchecked_mut(k) } =
M::binary_operation(unsafe { self.data.get_unchecked(2 * k) }, unsafe {
self.data.get_unchecked(2 * k + 1)
});
}
pub fn set(&mut self, mut p: usize, x: M::T) {
p += self.size;
self.data[p] = x;
(1..=self.log).for_each(|i| self.update(p >> i));
}
pub fn get(&self, i: usize) -> M::T {
self.data[i].clone()
}
pub fn max_right<F>(&self, mut l: usize, f: F) -> usize
where
F: Fn(&M::T) -> bool,
{
assert!(l <= self.n);
assert!(f(&M::identity_element()));
if l == self.n {
return self.n;
}
l += self.size;
let mut sm = M::identity_element();
while {
while l % 2 == 0 {
l >>= 1;
}
if !f(&M::binary_operation(&sm, unsafe {
self.data.get_unchecked(l)
})) {
while l < self.size {
l *= 2;
let res = M::binary_operation(&sm, unsafe { self.data.get_unchecked(l) });
if f(&res) {
sm = res;
l += 1;
}
}
return l - self.size;
}
sm = M::binary_operation(&sm, unsafe { self.data.get_unchecked(l) });
l += 1;
{
let l = l as isize;
(l & -l) != l
}
} {}
self.n
}
pub fn min_left<F>(&self, mut r: usize, f: F) -> usize
where
F: Fn(&M::T) -> bool,
{
assert!(r <= self.n);
assert!(f(&M::identity_element()));
if r == 0 {
return 0;
}
r += self.size;
let mut sm = M::identity_element();
while {
r -= 1;
while r > 1 && r % 2 == 1 {
r >>= 1;
}
if !f(&M::binary_operation(
unsafe { self.data.get_unchecked(r) },
&sm,
)) {
while r < self.size {
r = 2 * r + 1;
let res = M::binary_operation(unsafe { self.data.get_unchecked(r) }, &sm);
if f(&res) {
sm = res;
r -= 1;
}
}
return r + 1 - self.size;
}
sm = M::binary_operation(unsafe { self.data.get_unchecked(r) }, &sm);
{
let r = r as isize;
(r & -r) != r
}
} {}
0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_practice2_sample() {
let a = vec![1, 2, 3, 2, 1];
let cxy = vec![(2, 1, 5), (3, 2, 3), (1, 3, 1), (2, 2, 4), (3, 1, 3)];
let mut st = SegmentTree::<Max>::from(a);
let mut ans = vec![];
for (c, x, y) in cxy {
if c == 1 {
st.set(x as usize - 1, y);
} else if c == 2 {
ans.push(st.query(x as usize - 1, y as usize));
} else {
ans.push(st.max_right(x as usize - 1, |&v| y > v) as i64 + 1);
}
}
assert_eq!(vec![3, 3, 2, 6], ans);
}
}