-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlazy_segment_tree.hpp
151 lines (146 loc) · 4.97 KB
/
lazy_segment_tree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "mrpython/bits.hpp"
#ifndef MP_LIBRARY_LAZY_SEGMENT_TREE_HPP
#define MP_LIBRARY_LAZY_SEGMENT_TREE_HPP
#include <algorithm>
#include <functional>
#include <vector>
namespace mrpython {
using std::size_t;
template <typename T, typename MergeFunction, typename Lazy,
typename OperateFunction, typename MergeLazyFunction>
class lazy_segment_tree {
std::vector<T> data;
std::vector<Lazy> lazy;
std::vector<size_t> size;
size_t n;
Lazy lazyInit;
MergeFunction mergeData;
OperateFunction operate;
MergeLazyFunction mergeLazy;
void build(void) {
data.reserve(2 * n - 1), size.reserve(2 * n - 1);
for (size_t i = n; i < 2 * n - 1; ++i) {
size_t d = 2 * n - 1 - i;
size_t l = d * 2, r = d * 2 + 1;
data.emplace_back(mergeData(data[2 * n - 1 - l], data[2 * n - 1 - r]));
size.emplace_back(size[2 * n - 1 - l] + size[2 * n - 1 - r]);
}
std::reverse(data.begin(), data.end());
std::reverse(size.begin(), size.end());
}
void pushdown(size_t pos) {
if (size[pos] == 1) {
lazy[pos] = lazyInit;
return;
}
add_tag_for_node(2 * pos + 1, lazy[pos]);
add_tag_for_node(2 * pos + 2, lazy[pos]);
lazy[pos] = lazyInit;
}
void add_tag_for_node(size_t pos, Lazy const& lazyVal) {
data[pos] = operate(lazyVal, data[pos], size[pos]);
lazy[pos] = mergeLazy(lazy[pos], lazyVal);
}
T get_impl(size_t l, size_t r, size_t pos) {
if (l == 0 && r == size[pos]) return data[pos];
pushdown(pos);
size_t m = size[pos * 2 + 1];
if (l < m && r > m)
return mergeData(get_impl(l, m, pos * 2 + 1),
get_impl(0, r - m, pos * 2 + 2));
else if (l < m)
return get_impl(l, r, pos * 2 + 1);
else if (r > m)
return get_impl(l - m, r - m, pos * 2 + 2);
else
__builtin_unreachable();
}
void set_impl(size_t l, size_t r, Lazy const& operateVal, size_t pos) {
if (l == 0 && r == size[pos]) return add_tag_for_node(pos, operateVal);
pushdown(pos);
size_t m = size[pos * 2 + 1];
if (l < m && r > m)
set_impl(l, m, operateVal, pos * 2 + 1),
set_impl(0, r - m, operateVal, pos * 2 + 2);
else if (l < m)
set_impl(l, r, operateVal, pos * 2 + 1);
else if (r > m)
set_impl(l - m, r - m, operateVal, pos * 2 + 2);
else
__builtin_unreachable();
data[pos] = mergeData(data[pos * 2 + 1], data[pos * 2 + 2]);
}
public:
template <typename InputIterator>
lazy_segment_tree(InputIterator first, InputIterator last,
Lazy const& lazyInitVal,
MergeFunction const& mergeDataFun = MergeFunction(),
OperateFunction const& OperateFun = OperateFunction(),
MergeLazyFunction const& mergeTagFun = MergeLazyFunction())
: data(first, last),
lazy(2 * data.size() - 1, lazyInitVal),
size(data.size(), 1),
n(data.size()),
lazyInit(lazyInitVal),
mergeData(mergeDataFun),
operate(OperateFun),
mergeLazy(mergeTagFun) {
rotate(data.begin(), data.begin() + (2 * n - 1) - (highbit(2 * n - 1) - 1),
data.end());
reverse(data.begin(), data.end());
build();
}
lazy_segment_tree(size_t len, T const& init, Lazy const& lazyInitVal,
MergeFunction const& mergeDataFun = MergeFunction(),
OperateFunction const& OperateFun = OperateFunction(),
MergeLazyFunction const& mergeTagFun = MergeLazyFunction())
: data(len, init),
lazy(2 * len - 1, lazyInitVal),
size(len, 1),
n(len),
lazyInit(lazyInitVal),
mergeData(mergeDataFun),
operate(OperateFun),
mergeLazy(mergeTagFun) {
build();
}
T get(size_t l, size_t r) { return get_impl(l, r, 0); }
void set(size_t l, size_t r, Lazy const& operateVal) {
set_impl(l, r, operateVal, 0);
}
};
template <typename T> struct lazy_segment_tree_add_add_operate_function {
T operator()(T const& lazy, T const& data, size_t size) const {
return data + lazy * size;
}
};
template <typename T>
using lazy_segment_tree_add_add =
lazy_segment_tree<T, std::plus<T>, T,
lazy_segment_tree_add_add_operate_function<T>,
std::plus<T>>;
template <typename NodeStruct> class lazy_segment_tree_from_node {
using T = typename NodeStruct::T;
using Lazy = typename NodeStruct::Lazy;
struct MergeFunction {
T operator()(T const& a, T const& b) const {
return NodeStruct::merge_data(a, b);
}
};
struct OperateFunction {
T operator()(Lazy const& lazy, T const& data, size_t size) const {
return NodeStruct::operate(lazy, data, size);
}
};
struct MergeLazyFunction {
Lazy operator()(Lazy const& a, Lazy const& b) const {
return NodeStruct::merge_lazy(a, b);
}
};
lazy_segment_tree_from_node() = delete;
public:
using type = lazy_segment_tree<T, MergeFunction, Lazy, OperateFunction,
MergeLazyFunction>;
};
} // namespace mrpython
#endif