Aleph-w 7.10.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 &&other) 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 >)
 Take the elements of another tree, which is left empty.
 
Gen_Lazy_Segment_Tree & operator= (const Gen_Lazy_Segment_Tree &)=default
 
Gen_Lazy_Segment_Tree & operator= (Gen_Lazy_Segment_Tree &&other) noexcept(noexcept(swap(other)))
 Exchange this tree with another, which is left with the previous elements of this one.
 
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_type > values () const
 Reconstruct all original values into an Array.
 
void swap (Gen_Lazy_Segment_Tree &other) noexcept(std::is_nothrow_swappable_v< 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_type > tree
 
Array< lazy_type > lazy
 
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 971 of file tpl_segment_tree.H.

Member Typedef Documentation

◆ Item_Type

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

Definition at line 976 of file tpl_segment_tree.H.

◆ lazy_type

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

Definition at line 975 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 974 of file tpl_segment_tree.H.

Constructor & Destructor Documentation

◆ Gen_Lazy_Segment_Tree() [1/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( const size_t  num,
const value_type &  init_val = value_type(),
Policy  p = Policy() 
)
inline

Construct with num elements, all equal to init_val.

Definition at line 1112 of file tpl_segment_tree.H.

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

◆ Gen_Lazy_Segment_Tree() [2/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( std::initializer_list< value_type >  il,
Policy  p = Policy() 
)
inline

◆ Gen_Lazy_Segment_Tree() [3/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( const Array< value_type > &  values,
Policy  p = Policy() 
)
inline

◆ Gen_Lazy_Segment_Tree() [4/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( const std::vector< value_type > &  values,
Policy  p = Policy() 
)
inline

◆ Gen_Lazy_Segment_Tree() [5/7]

◆ Gen_Lazy_Segment_Tree() [6/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( const Gen_Lazy_Segment_Tree< Policy > &  )
default

◆ Gen_Lazy_Segment_Tree() [7/7]

template<typename Policy >
Aleph::Gen_Lazy_Segment_Tree< Policy >::Gen_Lazy_Segment_Tree ( Gen_Lazy_Segment_Tree< Policy > &&  other)
inlinenoexcept

Take the elements of another tree, which is left empty.

The implicit move took the arrays but copied the size: the moved-from tree then indexed arrays that it no longer had.

Parameters
[in,out]othertree to move from.

Definition at line 1179 of file tpl_segment_tree.H.

Member Function Documentation

◆ alloc_size()

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

Definition at line 1105 of file tpl_segment_tree.H.

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

◆ build()

template<typename Policy >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::build ( size_t  node,
const size_t  start,
const size_t  end 
)
inlineprivate

◆ fill_and_build()

◆ fill_from_aleph_it()

template<typename Policy >
template<class AlephIt >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_from_aleph_it ( AlephIt  it)
inlineprivate

◆ fill_leaves_recursive()

template<typename Policy >
template<class Getter >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::fill_leaves_recursive ( size_t  node,
size_t  start,
size_t  end,
Getter &  getter 
)
inlineprivate

◆ 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 1269 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 1278 of file tpl_segment_tree.H.

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

Referenced by TEST().

◆ operator=() [1/2]

template<typename Policy >
Gen_Lazy_Segment_Tree & Aleph::Gen_Lazy_Segment_Tree< Policy >::operator= ( const Gen_Lazy_Segment_Tree< Policy > &  )
default

◆ operator=() [2/2]

template<typename Policy >
Gen_Lazy_Segment_Tree & Aleph::Gen_Lazy_Segment_Tree< Policy >::operator= ( Gen_Lazy_Segment_Tree< Policy > &&  other)
inlinenoexcept

Exchange this tree with another, which is left with the previous elements of this one.

Parameters
[in,out]othertree to move from.
Returns
this tree.

Definition at line 1194 of file tpl_segment_tree.H.

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

◆ 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 1223 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 1255 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(), scenario_2_ajustes_salariales(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().

◆ query_impl()

template<typename Policy >
value_type Aleph::Gen_Lazy_Segment_Tree< Policy >::query_impl ( size_t  node,
const size_t  start,
const size_t  end,
const size_t  l,
const size_t  r 
) const
inlineprivate

◆ 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 1236 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 1275 of file tpl_segment_tree.H.

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

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

◆ swap()

template<typename Policy >
void Aleph::Gen_Lazy_Segment_Tree< Policy >::swap ( Gen_Lazy_Segment_Tree< Policy > &  other)
inlinenoexcept

Swap this tree with other in O(1).

Parameters
[in,out]othertree to exchange with.
Exceptions
whatswapping the policies throws. They are exchanged before the arrays, so that, if that swap throws, nothing else is exchanged, and both trees are left as they were if it left its arguments as they were.

Definition at line 1299 of file tpl_segment_tree.H.

References Aleph::Gen_Lazy_Segment_Tree< Policy >::lazy, Aleph::Gen_Lazy_Segment_Tree< Policy >::n, Aleph::Gen_Lazy_Segment_Tree< Policy >::pol, Aleph::Array< T >::swap(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and Aleph::Gen_Lazy_Segment_Tree< Policy >::tree.

Referenced by Aleph::Gen_Lazy_Segment_Tree< Policy >::operator=(), Aleph::Gen_Lazy_Segment_Tree< Policy >::swap(), and TEST().

◆ 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 1209 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(), scenario_2_ajustes_salariales(), 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: