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)
69 size_t brute_min_idx(
const std::vector<T> & v,
size_t l,
size_t r)
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)
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>
100 ASSERT_EQ(io.size(), ct.
size());
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;
123 while (!is_ancestor[curr])
138 std::vector<int> empty;
140 EXPECT_EQ(ct.
size(), 0u);
144 EXPECT_EQ(io.size(), 0u);
145 EXPECT_EQ(ct.
height(), 0u);
148TEST(GenCartesianTree, SingleElement)
151 EXPECT_EQ(ct.
size(), 1u);
153 EXPECT_EQ(ct.
root(), 0u);
160 EXPECT_EQ(ct.
height(), 1u);
164TEST(GenCartesianTree, TwoElements)
168 EXPECT_EQ(ct.
root(), 1u);
175TEST(GenCartesianTree, HeapProperty)
181TEST(GenCartesianTree, InorderProperty)
187TEST(GenCartesianTree, ManualSmall)
195 EXPECT_EQ(ct.
root(), 3u);
210TEST(GenCartesianTree, DuplicateValues)
223 EXPECT_EQ(ct.
root(), 0u);
226 EXPECT_EQ(ct.
height(), 5u);
229TEST(GenCartesianTree, ReverseSorted)
233 EXPECT_EQ(ct.
root(), 4u);
236 EXPECT_EQ(ct.
height(), 5u);
239TEST(GenCartesianTree, AllEqual)
246TEST(GenCartesianTree, CustomComparatorMax)
255TEST(GenCartesianTree, FromArray)
259 EXPECT_EQ(ct.
size(), 5u);
264TEST(GenCartesianTree, FromVector)
266 std::vector<int> vec = {5, 1, 3, 2, 4};
268 EXPECT_EQ(ct.
size(), 5u);
273TEST(GenCartesianTree, FromInitList)
276 EXPECT_EQ(ct.
size(), 5u);
281TEST(GenCartesianTree, FromDynList)
285 EXPECT_EQ(ct.
size(), 5u);
294 EXPECT_EQ(ct1.
height(), 1u);
298 EXPECT_GE(ct2.
height(), 2u);
299 EXPECT_LE(ct2.
height(), 3u);
302TEST(GenCartesianTree, CopyMoveSwap)
315 EXPECT_EQ(ct4.
size(), 5u);
316 EXPECT_EQ(ct3.
size(), 2u);
319TEST(GenCartesianTree, BoundsChecking)
322 EXPECT_THROW(ct.
data_at(3), std::out_of_range);
323 EXPECT_THROW(ct.
left_child(5), std::out_of_range);
324 EXPECT_THROW(ct.
right_child(100), std::out_of_range);
325 EXPECT_THROW(ct.
parent_of(3), std::out_of_range);
326 EXPECT_THROW(ct.
is_leaf(10), std::out_of_range);
327 EXPECT_THROW(ct.
is_root(3), std::out_of_range);
330TEST(GenCartesianTree, StressRandom)
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);
341 EXPECT_EQ(ct.
size(),
N);
353 std::vector<int> empty;
355 EXPECT_EQ(lca.
size(), 0u);
359TEST(EulerTourLCA, SingleElement)
362 EXPECT_EQ(lca.
size(), 1u);
363 EXPECT_EQ(lca.
lca(0, 0), 0u);
368TEST(EulerTourLCA, EulerTourSize)
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;
382TEST(EulerTourLCA, LcaRootChildren)
391 EXPECT_EQ(lca.
lca(0, 4),
r);
392 EXPECT_EQ(lca.
lca(2, 4),
r);
407 EXPECT_EQ(lca.
lca(0, 2), 1u);
410 EXPECT_EQ(lca.
lca(0, 1), 1u);
413 EXPECT_EQ(lca.
lca(1, 4), 3u);
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)
450 EXPECT_EQ(lca.
lca(u, v), lca.
lca(v, u))
451 <<
"Symmetry failed for u=" << u <<
" v=" << v;
454TEST(EulerTourLCA, LcaBoundsCheck)
457 EXPECT_THROW(lca.
lca(0, 5), std::out_of_range);
458 EXPECT_THROW(lca.
lca(10, 0), std::out_of_range);
459 EXPECT_THROW(lca.
depth_of(3), std::out_of_range);
462TEST(EulerTourLCA, StressVsBruteForce)
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)
479 size_t u = idx_dist(
rng);
480 size_t w = idx_dist(
rng);
483 <<
"LCA mismatch for u=" << u <<
" v=" <<
w;
494 std::vector<int> empty;
496 EXPECT_EQ(rmq.
size(), 0u);
500TEST(CartesianTreeRMQ, SingleElement)
503 EXPECT_EQ(rmq.
size(), 1u);
504 EXPECT_EQ(rmq.
query(0, 0), 42);
506 EXPECT_EQ(rmq.
get(0), 42);
509TEST(CartesianTreeRMQ, QueryEntireRange)
512 EXPECT_EQ(rmq.
query(0, 6), 1);
516TEST(CartesianTreeRMQ, QuerySingleElement)
519 for (
size_t i = 0; i < rmq.
size(); ++i)
520 EXPECT_EQ(rmq.
query(i, i), rmq.
get(i));
523TEST(CartesianTreeRMQ, QueryVsSparseTable)
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 <<
"]";
543TEST(CartesianTreeRMQ, QueryIdx)
549 EXPECT_EQ(rmq.
get(idx), 2);
553 EXPECT_EQ(rmq.
get(idx), 1);
557TEST(CartesianTreeRMQ, MaxVariant)
560 EXPECT_EQ(rmq.
query(0, 6), 7);
561 EXPECT_EQ(rmq.
query(0, 2), 5);
562 EXPECT_EQ(rmq.
query(4, 6), 6);
565TEST(CartesianTreeRMQ, StressRandomVsBruteForce)
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)
581 size_t l = idx_dist(
rng);
582 size_t r = idx_dist(
rng);
588 <<
"Mismatch at [" <<
l <<
", " <<
r <<
"] query " << q;
592TEST(CartesianTreeRMQ, BoundsCheck)
595 EXPECT_THROW(rmq.
query(0, 5), std::out_of_range);
596 EXPECT_THROW(rmq.
query(2, 1), std::out_of_range);
597 EXPECT_THROW(rmq.
get(3), std::out_of_range);
Simple dynamic array with automatic resizing and functional operations.
Doubly-linked list (defined in tpl_dynList.H).
constexpr size_t size() const noexcept
Number of elements.
T get(const size_t i) const
Retrieve the value at position i in O(1).
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
size_t query_idx(const size_t l, const size_t r) const
Return the index of the optimal element in a[l..r].
constexpr bool is_empty() const noexcept
True if empty.
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).
Array< size_t > inorder() const
Inorder traversal of the tree.
constexpr size_t root() const noexcept
Index of the root node.
const T & data_at(const size_t i) const
Value at node i.
size_t left_child(const size_t i) const
Left child of node i.
bool is_leaf(const size_t i) const
True if node i is a leaf (no children).
constexpr size_t size() const noexcept
Number of nodes.
size_t parent_of(const size_t i) const
Parent of node i.
size_t right_child(const size_t i) const
Right child of node i.
bool is_root(const size_t i) const
True if node i is the root.
constexpr bool is_empty() const noexcept
True if the tree is empty.
size_t height() const
Height of the tree (longest root-to-leaf path).
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).
Number Theoretic Transform over Z / MOD Z.
Minimal std::expected-style result type for C++20.
T brute_min(const vector< T > &v, size_t l, size_t r)
Main namespace for Aleph-w library functions.
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).