Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::SegmentTree< T > Class Template Reference

class representation of the segment tree More...

Public Member Functions

 SegmentTree (int n)
 
void update (int pos, T val)
 Updates a value at a certain position.
 
range_comb (int l, int r)
 Returns comb across all values between l and r.
 

Private Member Functions

comb (T x, T y)
 Any associative function that combines x and y.
 
int mid (int l, int r)
 Gives the midpoint between two integers.
 
void update (int i, int l, int r, int pos, T val)
 Helper method for update method below.
 
range_comb (int i, int l, int r, int tl, int tr)
 Helper method for range_comb method below.
 

Private Attributes

const T ID = 0
 Comb(ID, x) = x.
 
std::vector< T > t
 Vector to represent the tree.
 
int size = 0
 Number of elements available for querying in the tree.
 

Detailed Description

template<class T>
class data_structures::SegmentTree< T >

class representation of the segment tree

Template Parameters
TThe type of the class that goes in the datastructure

Constructor & Destructor Documentation

◆ SegmentTree()

template<class T >
data_structures::SegmentTree< T >::SegmentTree ( int n)
inline
87: t(n * 4, ID), size(n) {}
const T ID
Comb(ID, x) = x.
Definition segment_tree.cpp:32
int size
Number of elements available for querying in the tree.
Definition segment_tree.cpp:34
std::vector< T > t
Vector to represent the tree.
Definition segment_tree.cpp:33

Member Function Documentation

◆ comb()

template<class T >
T data_structures::SegmentTree< T >::comb ( T x,
T y )
inlineprivate

Any associative function that combines x and y.

Parameters
xThe first operand
yThe second operand
Returns
Some associative operation applied to these two values. In this case, I used addition
42 {
43 return x + y;
44 }

◆ mid()

template<class T >
int data_structures::SegmentTree< T >::mid ( int l,
int r )
inlineprivate

Gives the midpoint between two integers.

Parameters
lThe left endpoint
rThe right endpoint
Returns
the middle point between them
51 {
52 return l + (r - l) / 2;
53 }
double l(double x)
Another test function.
Definition composite_simpson_rule.cpp:119

◆ range_comb() [1/2]

template<class T >
T data_structures::SegmentTree< T >::range_comb ( int i,
int l,
int r,
int tl,
int tr )
inlineprivate

Helper method for range_comb method below.

Parameters
iThe current node
lThe leftmost node of the current node
rThe rightmost node of the current node
tlThe left endpoint of the range
trThe right endpoint of the range
Returns
The comb operation applied to all values between tl and tr
80 {
81 if(l == tl && r == tr) return t[i];
82 if(tl > tr) return 0;
83 int m = mid(l, r);
84 return comb(range_comb(i * 2, l, m, tl, std::min(tr, m)), range_comb(i * 2 + 1, m + 1, r, std::max(tl, m + 1), tr));
85 }
int mid(int l, int r)
Gives the midpoint between two integers.
Definition segment_tree.cpp:51
T comb(T x, T y)
Any associative function that combines x and y.
Definition segment_tree.cpp:42
T range_comb(int i, int l, int r, int tl, int tr)
Helper method for range_comb method below.
Definition segment_tree.cpp:80
T max(T... args)
T min(T... args)
Here is the call graph for this function:

◆ range_comb() [2/2]

template<class T >
T data_structures::SegmentTree< T >::range_comb ( int l,
int r )
inline

Returns comb across all values between l and r.

Parameters
lThe left endpoint of the range
rThe right endpoint of the range
Returns
The value of the comb operations
102 {
103 return range_comb(1, 1, size, l, r);
104 }
Here is the call graph for this function:

◆ update() [1/2]

template<class T >
void data_structures::SegmentTree< T >::update ( int i,
int l,
int r,
int pos,
T val )
inlineprivate

Helper method for update method below.

Parameters
iThe index of the current node
lThe leftmost node of the current node
rThe rightmost node of the current node
posThe position to update
valThe value to update it to
62 {
63 if(l == r) t[i] = val;
64 else {
65 int m = mid(l, r);
66 if(pos <= m) update(i * 2, l, m, pos, val);
67 else update(i * 2 + 1, m + 1, r, pos, val);
68 t[i] = comb(t[i * 2], t[i * 2 + 1]);
69 }
70 }
void update(int i, int l, int r, int pos, T val)
Helper method for update method below.
Definition segment_tree.cpp:62
Here is the call graph for this function:

◆ update() [2/2]

template<class T >
void data_structures::SegmentTree< T >::update ( int pos,
T val )
inline

Updates a value at a certain position.

Parameters
posThe position to update
valThe value to update it to
93 {
94 update(1, 1, size, pos, val);
95 }
Here is the call graph for this function:

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