-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazySegmentTree_MonoidTemplate.cpp
More file actions
202 lines (172 loc) · 4.77 KB
/
Copy pathLazySegmentTree_MonoidTemplate.cpp
File metadata and controls
202 lines (172 loc) · 4.77 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
OperatorMonoid:作用素モノイド
act:T x U (x {size})->T
*/
template<class Monoid, class OperatorMonoid>
struct LazySegmentTree
{
using M = Monoid;
using OM = OperatorMonoid;
static_assert(is_same<typename M::type, typename OM::type>::value, "");
using T = typename M::type;
using U = typename OM::act_type;
const int SIZE;
int tree_size, leaf_number;
vector<T> node;
vector<U> lazy_node;
LazySegmentTree(const vector<T> &ary);
const int child_l(const int k) const
{
return k*2+1;
}
const int child_r(const int k) const
{
return k*2+2;
}
const int parent(const int k) const
{
return (k-1)/2;
}
const int least_square(const int k) const
{
int tmp=k;
for(int i=1; 64>i; i<<=1)
tmp |= (tmp >> i);
return tmp+1;
}
const T init(const int k);
const void update(const int a, const int b, const U x, const int k, const int l, const int r);
const void update(const int a, const int b, const U x)
{
return update(a, b, x, 0, 0, leaf_number);
}
const T fold(const int a, const int b, const int k, const int l, const int r);
const T fold(const int a, const int b)
{
return fold(a, b, 0, 0, leaf_number);
}
const void add_lazy(const int k, const U &x)
{
lazy_node[k] = OM::op(lazy_node[k], x);
};
const void evalute_lazy(const int k, const int size)
{
if(size>1)
{
add_lazy(child_l(k), lazy_node[k]);
add_lazy(child_r(k), lazy_node[k]);
}
//cout<<k<<" "<<lazies[i].lazy[k]<<" "<<node[k]<<" ";
node[k] = OM::act(node[k], lazy_node[k], size);
lazy_node[k] = OM::id();
//cout<<node[k]<<endl;
};
const void print() const
{
for(int i=0;i<tree_size;i++)
cout<<node[i]<<" \n"[i==tree_size-1];
}
};
template<class M, class OM>
LazySegmentTree<M, OM>::LazySegmentTree(const vector<T> &ary)
:SIZE(ary.size())
{
tree_size=SIZE;
leaf_number=least_square(tree_size);
tree_size=leaf_number*2;
node=vector<T>(tree_size-1);
lazy_node=vector<U>(tree_size-1, OM::id());
for(int i=0;i<leaf_number;i++)
if(i<SIZE)
node[i+leaf_number-1]=ary[i];
else
node[i+leaf_number-1]=M::id();
init(0);
}
template<class M, class OM>
const typename M::type LazySegmentTree<M, OM>::init(const int k)
{
if(k>=leaf_number-1) return node[k];
return node[k] = M::op(init(child_l(k)),init(child_r(k)));
}
template<class M, class OM>
const void LazySegmentTree<M, OM>::update(const int a, const int b, const U x, const int k, const int l, const int r)
{
if(r <= a || b <= l) //[a,b)と[l,r)が交わらない
{
evalute_lazy(k, r-l);
return;
}
if(a <= l && r <= b) //[a,b)が[l,r)を含む
{
add_lazy(k, x);
evalute_lazy(k, r-l);
}
else
{
evalute_lazy(k, r-l);
update(a,b,x,child_l(k),l,(l+r)/2);
update(a,b,x,child_r(k),(l+r)/2, r);
node[k] = M::op(node[child_l(k)], node[child_r(k)]);
}
}
template<class M, class OM>
const typename M::type LazySegmentTree<M, OM>::fold(const int a, const int b, const int k, const int l, const int r)
{
if(r <= a || b <= l)
return M::id(); //[a,b)と[l,r)が交わらない
evalute_lazy(k, r-l);
if(a <= l && r <= b)
return node[k]; //[a,b)が[l,r)を含む
return M::op(fold(a,b,child_l(k),l,(l+r)/2), fold(a,b,child_r(k),(l+r)/2, r));
}
template <typename T>
struct MinMonoid
{
using type = T;
static constexpr T id() { return numeric_limits<T>::max(); }
static T op(const T &a, const T &b) { return min(a, b); }
};
template <typename T>
struct MaxMonoid
{
using type = T;
static constexpr T id() { return numeric_limits<T>::min(); }
static T op(const T &a, const T &b) { return max(a, b); }
};
template <typename T>
struct PlusMonoid
{
using type = T;
static constexpr T id() { return 0; }
static T op(const T &a, const T &b) { return a + b; }
};
template <typename T>
struct MultMonoid
{
using type = T;
static constexpr T id() { return 1; }
static T op(const T &a, const T &b) { return a * b; }
};
template <typename T, typename U>
struct RangeAddOperatorMonoid
{
using type = T;
using act_type = U;
static constexpr U id() { return 0; }
static U op(const U &a, const U &b) { return a + b; }
static T act(const T &a, const U &b, const int size) { return a + b * size; }
};
template <typename T, typename U>
struct RangeSubstOperatorMonoid
{
using type = T;
using act_type = U;
static constexpr U id() { return -1; }
static U op(const U &a, const U &b) { return b; }
static T act(const T &a, const U &b, const int size) { return b == -1 ? a : b; }
};
template <typename T>
using RARS = LazySegmentTree<PlusMonoid<T>, RangeAddOperatorMonoid<T, T>>;
template <typename T>
using RARM = LazySegmentTree<MaxMonoid<T>, RangeAddOperatorMonoid<T, T>>;