maomao90's Library
A C++20 library for competitive programming.
|
A balanced binary search tree. More...
#include <splaytree.hpp>
Public Member Functions | |
SplayTree () | |
Initialises empty splay tree. | |
SplayTree (int n) | |
Initialises the splay tree with n elements, all equal to the identity element T::id() . | |
SplayTree (const vector< T > &v) | |
Initialises the splay tree with values from vector v . | |
int | size () |
Gets the number of elements in the splay tree. | |
void | set (int k, T x) |
Set the k -th index (0-indexed) to x . | |
T | get (int k) |
Get the value at the k -th index (0-indexed). | |
void | update (int l, int r, L x) |
Apply update x to the half-open interval [l, r) . | |
T | query (int l, int r) |
Query the half-open interval [l, r) . | |
template<typename P> | |
int | max_right (int l, P pred) |
Finds the largest x such that the predicate returns true for the left-associative fold on the half-open interval [l, x) . | |
template<bool(*)(T) pred> | |
int | max_right (int l) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
template<typename P> | |
int | min_left (int r, P pred) |
Finds the smallest x such that the predicate returns true for the left-associative fold on the half-open interval [x, r) . | |
template<bool(*)(T) pred> | |
int | min_left (int r) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
void | reverse (int l, int r) |
Reverses the half-open interval [l, r) . | |
void | insert (int k, T x) |
Inserts x into the k -th index (0-indexed). | |
void | erase (int k) |
Erases the value at the k -th index (0-indexed). | |
splaytree | split (int k) |
Splits this into two parts, then, set this to be the left part and returns the right part. | |
pair< splaytree, splaytree > | split (int l, int r) |
Splits this into three parts, then, set this to be the left part and returns the middle and right parts. | |
void | merge (splaytree &o) |
Merge splay tree o to the right of this . | |
void | merge (splaytree &b, splaytree &c) |
Merge splay tree b to the right of this , then c to the right of this and b . | |
A balanced binary search tree.
Supports operations similar to a LazySegTree. Additionally, also supports splitting, merging, and range reversals. All operations work in ammortised time. Note that all indices are 0-indexed, and all intervals are half-open: [start, end).
T | the monoid to be stored in the treap. Note that the merge function is called using left_monoid.merge(right_monoid) . |
L | the type used to perform range updates on T . Note that the merge function is called using new_update.merge(old_update) . |
store_reverse | should be true only if range reversal is required and T is not commutative. |
|
inline |
Initialises empty splay tree.
|
inlineexplicit |
Initialises the splay tree with n
elements, all equal to the identity element T::id()
.
n | the number of elements to be initialised with. |
|
inlineexplicit |
Initialises the splay tree with values from vector v
.
v | the vector of values to be stored in the splay tree. |
|
inline |
Gets the number of elements in the splay tree.
|
inline |
Set the k
-th index (0-indexed) to x
.
k | the index to set. |
x | the value to set. |
0 <= k < size()
.
|
inline |
Get the value at the k
-th index (0-indexed).
k | the index to get the value of. |
k
-th index.0 <= k < size()
.
|
inline |
Apply update x
to the half-open interval [l, r)
.
l | the inclusive left endpoint (0-indexed) of the interval. |
r | the exclusive right endpoint (0-indexed) of the interval. |
x | the update to be applied. |
0 <= l <= r <= size()
.
|
inline |
Query the half-open interval [l, r)
.
l | the inclusive left endpoint (0-indexed) of the interval. |
r | the exclusive right endpoint (0-indexed) of the interval. |
0 <= l <= r <= size()
.
|
inline |
Finds the largest x
such that the predicate returns true for the left-associative fold on the half-open interval [l, x)
.
P | the type of the predicate function. |
l | the inclusive left endpoint (0-indexed) to search to the right of. |
pred | a function that accepts T as a parameter, and returns a boolean. |
x
such that pred(query(l, x))
is true. Note that the query
function is exclusive of the right endpoint.0 <= l <= size()
. pred(T::id()) = true
. In other words, pred
should be true for query(l, l)
.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Finds the smallest x
such that the predicate returns true for the left-associative fold on the half-open interval [x, r)
.
P | the type of the predicate function. |
r | the exclusive right endpoint (0-indexed) to search to the left of. |
pred | a function that accepts T as a parameter, and returns a boolean. |
x
such that pred(query(x, r))
is true. Note that the query
function is exclusive of the right endpoint.0 <= r <= size()
. pred(T::id()) = true
. In other words, pred
should be true for query(r, r)
.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Reverses the half-open interval [l, r)
.
l | the inclusive left endpoint (0-indexed) of the interval. |
r | the exclusive right endpoint (0-indexed) of the interval. |
0 <= l <= r <= size()
.
|
inline |
|
inline |
Erases the value at the k
-th index (0-indexed).
k | the index to erase. |
0 <= k < size()
.
|
inline |
Splits this
into two parts, then, set this
to be the left part and returns the right part.
k | the number of elements in the left part after the split. |
size() - k
elements.
|
inline |
Splits this
into three parts, then, set this
to be the left part and returns the middle and right parts.
l | the number of elements in the left part after the split. |
r | the total number of elements in the left and middle part, i.e., the middle part has r - l elements. |
(middle, right)
where middle
is the middle splay tree containing r - l
elements and right
is the right splay tree containing size() - r
elements.0 <= l <= r <= size()
.
|
inline |
Merge splay tree o
to the right of this
.
o | the splay tree to merge to the right of this . |
o
points to an empty splay tree.
|
inline |
Merge splay tree b
to the right of this
, then c
to the right of this
and b
.
b | the middle splay tree after merging. |
c | the right splay tree after merging. |
b
and c
points to empty splay trees.