Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Gen_Lazy_Segment_Tree< Policy > Class Template Reference

Lazy segment tree with range update and range query. More...

#include <tpl_segment_tree.H>

Inheritance diagram for Aleph::Gen_Lazy_Segment_Tree< Policy >:
[legend]
Collaboration diagram for Aleph::Gen_Lazy_Segment_Tree< Policy >:
[legend]

Public Types

using value_type = typename Policy::value_type
 
using lazy_type = typename Policy::lazy_type
 
using Item_Type = value_type
 

Public Member Functions

 Gen_Lazy_Segment_Tree (const size_t num, const value_type &init_val=value_type(), Policy p=Policy())
 Construct with num elements, all equal to init_val.
 
 Gen_Lazy_Segment_Tree (std::initializer_list< value_type > il, Policy p=Policy())
 Construct from an initializer list.
 
 Gen_Lazy_Segment_Tree (const Array< value_type > &values, Policy p=Policy())
 Construct from an Array<value_type>.
 
 Gen_Lazy_Segment_Tree (const std::vector< value_type > &values, Policy p=Policy())
 Construct from a std::vector.
 
 Gen_Lazy_Segment_Tree (const DynList< value_type > &values, Policy p=Policy())
 Construct from a DynList.
 
 Gen_Lazy_Segment_Tree (const Gen_Lazy_Segment_Tree &)=default
 
 Gen_Lazy_Segment_Tree (Gen_Lazy_Segment_Tree &&) noexcept(std::is_nothrow_move_constructible_v< Array< value_type > > and std::is_nothrow_move_constructible_v< Array< lazy_type > > and std::is_nothrow_move_constructible_v< Policy >)=default
 
Gen_Lazy_Segment_Treeoperator= (const Gen_Lazy_Segment_Tree &)=default
 
Gen_Lazy_Segment_Treeoperator= (Gen_Lazy_Segment_Tree &&) noexcept(std::is_nothrow_move_assignable_v< Array< value_type > > and std::is_nothrow_move_assignable_v< Array< lazy_type > > and std::is_nothrow_move_assignable_v< Policy >)=default
 
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 point_update (const size_t i, const lazy_type &delta)
 Point update: apply delta to a[i].
 
void set (const size_t i, const value_type &val)
 Set a[i] = val.
 
value_type query (const size_t l, const size_t r) const
 Range query over [l, r].
 
value_type get (const size_t i) const
 Retrieve the value a[i].
 
constexpr size_t size () const noexcept
 Number of logical elements.
 
constexpr bool is_empty () const noexcept
 True if the tree contains no elements.
 
Array< value_typevalues () const
 Reconstruct all original values into an Array.
 
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).
 

Private Member Functions

void push_down (size_t node, const size_t start, const size_t end) const
 
void build (size_t node, const size_t start, const size_t end)
 
void update_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r, const lazy_type &val)
 
value_type query_impl (size_t node, const size_t start, const size_t end, const size_t l, const size_t r) const
 
void set_impl (size_t node, const size_t start, const size_t end, const size_t idx, const value_type &val)
 
template<class Getter >
void fill_and_build (Getter getter)
 
template<class Getter >
void fill_leaves_recursive (size_t node, size_t start, size_t end, Getter &getter)
 
template<class AlephIt >
void fill_from_aleph_it (AlephIt it)
 
size_t alloc_size () const
 

Private Attributes

Array< value_typetree
 
Array< lazy_typelazy
 
size_t n = 0
 
Policy pol
 

Detailed Description

template<typename Policy>
requires SegmentTreePolicy<Policy>
class Aleph::Gen_Lazy_Segment_Tree< Policy >

Lazy segment tree with range update and range query.

Parameterised by a Policy that bundles value type, lazy type, and the combine/apply/compose functions. Supports range updates and range queries in O(log n) with lazy propagation.

Lazy propagation
Updates store a pending tag at internal nodes and push it down to children only when required (during updates/queries), keeping the asymptotic cost to O(log n).
Constness
query() is declared const in the public API, but it may still trigger propagation (push_down) by mutating internal mutable storage. This preserves logical constness for callers while allowing on-demand evaluation of pending updates.
Template Parameters
Policymust satisfy SegmentTreePolicy.
Example
Lazy_Sum_Segment_Tree<int> st = {1, 2, 3, 4, 5};
st.update(1, 3, 10); // a[1..3] += 10
st.query(0, 4); // 1 + 12 + 13 + 14 + 5 = 45
value_type query(const size_t l, const size_t r) const
Range query over [l, r].
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.
Lazy segment tree for range add + range sum.

Definition at line 922 of file tpl_segment_tree.H.

Member Typedef Documentation

◆ Item_Type

◆ lazy_type

template<typename Policy >
using Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy_type = typename Policy::lazy_type

Definition at line 926 of file tpl_segment_tree.H.

◆ value_type

template<typename Policy >
using Aleph::Gen_Lazy_Segment_Tree< Policy >::value_type = typename Policy::value_type

Definition at line 925 of file tpl_segment_tree.H.

Constructor & Destructor Documentation

◆ Gen_Lazy_Segment_Tree() [1/7]

◆ Gen_Lazy_Segment_Tree() [2/7]

◆ Gen_Lazy_Segment_Tree() [3/7]

◆ Gen_Lazy_Segment_Tree() [4/7]

◆ Gen_Lazy_Segment_Tree() [5/7]

◆ Gen_Lazy_Segment_Tree() [6/7]

◆ Gen_Lazy_Segment_Tree() [7/7]

Member Function Documentation

◆ alloc_size()

template<typename Policy >
size_t Aleph::Gen_Lazy_Segment_Tree< Policy >::alloc_size ( ) const
inlineprivate

Definition at line 1056 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.

◆ build()

◆ fill_and_build()

◆ fill_from_aleph_it()

◆ fill_leaves_recursive()

◆ get()

template<typename Policy >
value_type Aleph::Gen_Lazy_Segment_Tree< Policy >::get ( const size_t  i) const
inline

Retrieve the value a[i].

Equivalent to query(i, i). O(log n).

Definition at line 1203 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::query().

Referenced by scenario_2_ajustes_salariales(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::values().

◆ is_empty()

template<typename Policy >
constexpr bool Aleph::Gen_Lazy_Segment_Tree< Policy >::is_empty ( ) const
inlineconstexprnoexcept

True if the tree contains no elements.

Definition at line 1212 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.

Referenced by TEST().

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ point_update()

template<typename Policy >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::point_update ( const size_t  i,
const lazy_type delta 
)
inline

Point update: apply delta to a[i].

Convenience shorthand for update(i, i, delta).

Definition at line 1157 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::update().

Referenced by TEST().

◆ push_down()

◆ query()

template<typename Policy >
value_type Aleph::Gen_Lazy_Segment_Tree< Policy >::query ( const size_t  l,
const size_t  r 
) const
inline

Range query over [l, r].

Lazy tags are propagated internally via mutable storage, so this method is const and safe to call through const references.

Parameters
l0-based left index (inclusive).
r0-based right index (inclusive).
Returns
combined value over the range.
Exceptions
std::out_of_rangeif l > r or r >= size().

Definition at line 1189 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl(), and r.

Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::get(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ query_impl()

◆ set()

template<typename Policy >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::set ( const size_t  i,
const value_type val 
)
inline

Set a[i] = val.

O(log n).

Parameters
i0-based index.
valnew value for position i.
Exceptions
std::out_of_rangeif i >= size().

Definition at line 1170 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, and Aleph::Gen_Lazy_Segment_Tree< Policy >::set_impl().

Referenced by TEST(), TEST(), and TEST().

◆ set_impl()

◆ size()

template<typename Policy >
constexpr size_t Aleph::Gen_Lazy_Segment_Tree< Policy >::size ( ) const
inlineconstexprnoexcept

Number of logical elements.

Definition at line 1209 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::n.

Referenced by TEST().

◆ swap()

◆ update()

template<typename Policy >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::update ( const size_t  l,
const size_t  r,
const lazy_type val 
)
inline

Range update: apply val to every a[i] with l <= i <= r.

Parameters
l0-based left index (inclusive).
r0-based right index (inclusive).
vallazy tag to apply.
Exceptions
std::out_of_rangeif l > r or r >= size().

Definition at line 1143 of file tpl_segment_tree.H.

References ah_out_of_range_error_if, l, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, r, and Aleph::Gen_Lazy_Segment_Tree< Policy >::update_impl().

Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::point_update(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ update_impl()

◆ values()

Member Data Documentation

◆ lazy

◆ n

◆ pol

◆ tree


The documentation for this class was generated from the following file: