37# include <gtest/gtest.h>
49using namespace testing;
55 T brute_sum(
const std::vector<T> & v,
size_t l,
size_t r)
58 for (
size_t i =
l; i <=
r; ++i)
64 T brute_min(
const std::vector<T> & v,
size_t l,
size_t r)
66 return *std::min_element(v.begin() +
l, v.
begin() +
r + 1);
70 T brute_max(
const std::vector<T> & v,
size_t l,
size_t r)
72 return *std::max_element(v.begin() +
l, v.
begin() +
r + 1);
78 constexpr int operator()(
const int a,
const int b)
const noexcept
87 int operator()(
const int a,
const int b)
const noexcept
89 return std::gcd(a, b);
115 for (
size_t i = 0; i < 8; ++i)
124 const std::vector<int> values = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
127 for (
size_t l = 0;
l < values.
size(); ++
l)
128 for (
size_t r =
l;
r < values.
size(); ++
r)
134 const std::vector<int> values = {9, 4, 7, 1, 8, 2, 6, 3, 5, 0};
137 for (
size_t l = 0;
l < values.
size(); ++
l)
138 for (
size_t r =
l;
r < values.
size(); ++
r)
144 const std::vector<int> values = {9, 4, 7, 1, 8, 2, 6, 3, 5, 0};
147 for (
size_t l = 0;
l < values.
size(); ++
l)
148 for (
size_t r =
l;
r < values.
size(); ++
r)
177 const std::vector<int> values = {5, 3, 7, 1, 9, 2, 8, 4, 6};
182 for (
const int x : values)
187 for (
const int x : values)
193 for (
size_t l = 0;
l < values.
size(); ++
l)
194 for (
size_t r =
l;
r < values.
size(); ++
r)
206 const std::vector<int> values = {3, 5, 7, 2, 8};
224 const std::vector<int> values = {12, 18, 24, 36, 60};
227 EXPECT_EQ(st.query(0, 1), std::gcd(12, 18));
229 EXPECT_EQ(st.query(2, 4), std::gcd(std::gcd(24, 36), 60));
307 constexpr size_t N = 1000;
308 constexpr size_t OPS = 5000;
310 std::mt19937
rng(42);
311 std::uniform_int_distribution<int>
val_dist(-100, 100);
312 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
314 std::vector<int>
brute(
N);
315 for (
size_t i = 0; i <
N; ++i)
320 for (
size_t op = 0; op <
OPS; ++op)
335 if (a > b) std::swap(a, b);
343 constexpr size_t N = 500;
344 constexpr size_t OPS = 3000;
346 std::mt19937
rng(123);
347 std::uniform_int_distribution<int>
val_dist(-1000, 1000);
348 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
350 std::vector<int>
brute(
N);
351 for (
size_t i = 0; i <
N; ++i)
356 for (
size_t op = 0; op <
OPS; ++op)
371 if (a > b) std::swap(a, b);
478 const std::vector<int> values = {5, 3, 7, 1, 9};
483 for (
const int x : values)
488 for (
const int x : values)
494 for (
size_t l = 0;
l < values.
size(); ++
l)
495 for (
size_t r =
l;
r < values.
size(); ++
r)
547 constexpr size_t N = 200;
548 constexpr size_t OPS = 2000;
550 std::mt19937
rng(99);
551 std::uniform_int_distribution<int>
val_dist(-50, 50);
552 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
554 std::vector<int>
brute(
N, 0);
557 for (
size_t op = 0; op <
OPS; ++op)
561 if (a > b) std::swap(a, b);
566 for (
size_t i = a; i <= b; ++i)
606 st.
update(1, 3, {
true, 10});
615 st.
update(2, 4, {
true, 0});
621 constexpr size_t N = 100;
622 constexpr size_t OPS = 1000;
624 std::mt19937
rng(77);
625 std::uniform_int_distribution<int>
val_dist(-20, 20);
626 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
628 std::vector<int>
brute(
N, 0);
631 for (
size_t op = 0; op <
OPS; ++op)
635 if (a > b) std::swap(a, b);
640 for (
size_t i = a; i <= b; ++i)
754 constexpr size_t N = 100;
755 constexpr size_t OPS = 1000;
757 std::mt19937
rng(2024);
758 std::uniform_int_distribution<int>
val_dist(0, 100);
759 std::uniform_int_distribution<size_t>
idx_dist(0,
N - 1);
760 std::uniform_int_distribution<int>
op_dist(0, 4);
762 std::vector<int>
brute(
N);
763 for (
size_t i = 0; i <
N; ++i)
768 for (
size_t op = 0; op <
OPS; ++op)
772 if (a > b) std::swap(a, b);
780 for (
size_t i = a; i <= b; ++i)
789 for (
size_t i = a; i <= b; ++i)
807 for (
size_t i = 0; i <
N; ++i)
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Lazy segment tree with range update and range query.
void point_update(const size_t i, const lazy_type &delta)
Point update: apply delta to a[i].
constexpr size_t size() const noexcept
Number of logical elements.
value_type query(const size_t l, const size_t r) const
Range query over [l, r].
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
void update(const size_t l, const size_t r, const lazy_type &val)
Range update: apply val to every a[i] with l <= i <= r.
void set(const size_t i, const value_type &val)
Set a[i] = val.
void swap(Gen_Lazy_Segment_Tree &other) noexcept(noexcept(std::swap(std::declval< Policy & >(), std::declval< Policy & >())))
Swap this tree with other in O(1).
value_type get(const size_t i) const
Retrieve the value a[i].
Segment tree over an arbitrary associative binary operation.
void set(const size_t i, const T &val)
Set a[i] = val.
void swap(Gen_Segment_Tree &other) noexcept(noexcept(std::swap(std::declval< T & >(), std::declval< T & >())) and noexcept(std::swap(std::declval< Op & >(), std::declval< Op & >())))
Swap this tree with other in O(1).
T query(const size_t l, const size_t r) const
Range query over [l, r].
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
Array< T > values() const
Reconstruct all original values into an Array.
T get(const size_t i) const
Retrieve the value a[i].
void update(const size_t i, const T &delta)
Point update: a[i] = Op(a[i], delta).
constexpr size_t size() const noexcept
Number of logical elements.
size_t size() const noexcept
Count the number of elements of the list.
Ji Driver's Segment Tree (Segment Tree Beats).
void chmax(const size_t l, const size_t r, const T &v)
Range chmax: for each i in [l, r], set a[i] = max(a[i], v).
constexpr size_t size() const noexcept
Number of logical elements.
T query_sum(const size_t l, const size_t r) const
Range sum query over [l, r].
T get(const size_t i) const
Retrieve the value at position i.
T query_max(const size_t l, const size_t r) const
Range max query over [l, r].
void chmin(const size_t l, const size_t r, const T &v)
Range chmin: for each i in [l, r], set a[i] = min(a[i], v).
Array< T > values() const
Reconstruct all values into an Array.
T query_min(const size_t l, const size_t r) const
Range min query over [l, r].
void swap(Segment_Tree_Beats &other) noexcept
Swap this tree with other in O(1).
constexpr bool is_empty() const noexcept
True if the tree contains no elements.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
T brute_sum(const vector< T > &v, size_t l, size_t r)
T brute_min(const vector< T > &v, size_t l, size_t r)
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
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
T brute_max(const vector< T > &v, size_t l, size_t r)
Lazy segment tree for range add + range max.
Lazy segment tree for range add + range min.
Lazy segment tree for range add + range sum.
Max Segment Tree for range maximum queries with point updates.
Min Segment Tree for range minimum queries with point updates.
Sum Segment Tree for range sum queries with point updates.
int operator()(const int &a, const int &b) const noexcept
constexpr unsigned operator()(unsigned a, unsigned b) const noexcept
Segment trees for dynamic range queries with lazy propagation.