38# include <gtest/gtest.h>
50using namespace testing;
59 T brute_min(
const std::vector<T> & v,
size_t l,
size_t r)
62 for (
size_t i =
l + 1; i <=
r; ++i)
72 for (
size_t i =
l + 1; i <=
r; ++i)
79 template <
typename T,
class Comp>
82 for (
size_t i = 0; i <
ct.size(); ++i)
84 size_t p =
ct.parent_of(i);
87 ct.data_at(p) ==
ct.data_at(i))
88 <<
"Heap violation at node " << i
90 <<
" data[parent]=" <<
ct.data_at(p)
91 <<
" data[i]=" <<
ct.data_at(i);
96 template <
typename T,
class Comp>
99 auto io =
ct.inorder();
101 for (
size_t i = 0; i <
ct.size(); ++i)
102 EXPECT_EQ(
io(i), i) <<
"Inorder mismatch at position " << i;
106 template <
typename T,
class Comp>
113 std::vector<bool> is_ancestor(
ct.size(),
false);
117 is_ancestor[curr] =
true;
118 curr =
ct.parent_of(curr);
123 while (!is_ancestor[curr])
124 curr =
ct.parent_of(curr);
138 std::vector<int> empty;
143 auto io =
ct.inorder();
266 std::vector<int>
vec = {5, 1, 3, 2, 4};
332 constexpr size_t N = 1000;
333 std::mt19937
rng(42);
334 std::uniform_int_distribution<int> dist(0, 10000);
336 std::vector<int> v(
N);
353 std::vector<int> empty;
378 for (
size_t i = 0; i < lca.
size(); ++i)
379 EXPECT_EQ(lca.
lca(i, i), i) <<
"lca(i,i) should be i for i=" << i;
441 for (
size_t i = 0; i < lca.
size(); ++i)
448 for (
size_t u = 0; u < lca.
size(); ++u)
449 for (
size_t v = u; v < lca.
size(); ++v)
451 <<
"Symmetry failed for u=" << u <<
" v=" << v;
464 constexpr size_t N = 500;
465 constexpr size_t Q = 2000;
466 std::mt19937
rng(123);
467 std::uniform_int_distribution<int> dist(0, 10000);
469 std::vector<int> v(
N);
474 const auto &
ct = lca.
tree();
476 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
477 for (
size_t q = 0; q <
Q; ++q)
483 <<
"LCA mismatch for u=" << u <<
" v=" <<
w;
494 std::vector<int> empty;
519 for (
size_t i = 0; i <
rmq.size(); ++i)
525 constexpr size_t N = 100;
526 std::mt19937
rng(99);
527 std::uniform_int_distribution<int> dist(-1000, 1000);
529 std::vector<int> v(
N);
537 for (
size_t l = 0;
l <
N; ++
l)
538 for (
size_t r =
l;
r <
N; ++
r)
540 <<
"Mismatch at [" <<
l <<
", " <<
r <<
"]";
552 idx =
rmq.query_idx(2, 6);
567 constexpr size_t N = 1000;
568 constexpr size_t Q = 5000;
569 std::mt19937
rng(777);
570 std::uniform_int_distribution<int> dist(-50000, 50000);
572 std::vector<int> v(
N);
578 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
579 for (
size_t q = 0; q <
Q; ++q)
588 <<
"Mismatch at [" <<
l <<
", " <<
r <<
"] query " << q;
Simple dynamic array with automatic resizing and functional operations.
Dynamic singly linked list with functional programming support.
size_t query_idx(const size_t l, const size_t r) const
Return the index of the optimal element in a[l..r].
Explicit Cartesian Tree built in O(n) with a monotonic stack.
void swap(Gen_Cartesian_Tree &other) noexcept(noexcept(data_.swap(other.data_)) &&noexcept(parent_.swap(other.parent_)) &&noexcept(left_.swap(other.left_)) &&noexcept(right_.swap(other.right_)) &&noexcept(std::swap(root_, other.root_)) &&noexcept(std::swap(n_, other.n_)) &&std::is_nothrow_swappable_v< Comp >)
Swap with other in O(1).
constexpr size_t root() const noexcept
Index of the root node.
size_t euler_tour_size() const noexcept
Size of the Euler Tour (should be 2n-1 for n > 0).
constexpr size_t size() const noexcept
Number of nodes.
const Gen_Cartesian_Tree< T, Comp > & tree() const noexcept
Const reference to the internal Cartesian Tree.
size_t depth_of(const size_t u) const
Depth of node u in the Cartesian Tree.
constexpr bool is_empty() const noexcept
True if empty.
size_t lca(const size_t u, const size_t v) const
Lowest Common Ancestor of nodes u and v in O(1).
size_t distance(const size_t u, const size_t v) const
Distance between nodes u and v in the tree.
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
T brute_min(const vector< T > &v, size_t l, size_t r)
Main namespace for Aleph-w library functions.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Range minimum queries via Cartesian Tree.
Range maximum queries via Cartesian Tree.
Cartesian Tree for min-heap (range minimum).
LCA on a min-heap Cartesian Tree.
Cartesian Tree for max-heap (range maximum).
Sparse Table for range minimum queries.
Cartesian Tree, LCA via Euler Tour, and RMQ via Cartesian Tree.
Sparse Table for static range queries in O(1).