Repository navigation
Expand file tree
/
Copy pathstruct.cpp
More file actions
103 lines (93 loc) · 3.04 KB
/
Copy pathstruct.cpp
File metadata and controls
103 lines (93 loc) · 3.04 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
/*8<
@Title:
Lazy Sparse Segment Tree
@Description:
Range update and range query over large or
uncompressed index spaces (up to $10^{18}$).
$S$ must satisfy:
\begin{compactitem}
\item \textbf{T}: query value type
\item \textbf{L}: lazy value type
\item \textbf{id}: identity element of $T$
\item \textbf{op(a, b)}: associative merge of two $T$ values
\item \textbf{ch(t, l, lx, rx)}: apply lazy $l$ to node $t$ covering $[lx,
rx]$
\item \textbf{cmp(a, b)}: compose lazies: apply $a$ then $b$
\end{compactitem}
Node 0 is a blank/null node (value $= id$); all nodes
have both or none of their children.
\texttt{MXN} should be at least
$\textit{queries} \cdot \log(n) \cdot 2$.
@Usage:
LazySparseSeg<I, S> seg(n); // indices in [0, n-1]
seg.update(lazy, l, r); // range update [l, r]
seg.query(l, r); // range query [l, r]
@Time:
Build $O(1)$, update/query $O(\log N)$
@Warning:
Implementation comes from arthur9548 personal library, so it has the
potential to be extremely good !
>8*/
#pragma once
#include "../../Contest/template.cpp"
#define GET(a, x) a = x.a
#define UT(s, x) using x = typename s::x
#define midpoint(l, r) (l + (r - l) / 2)
const int MXN = 1e8;
template <class I, class S>
struct LazySparseSeg {
using P = int;
UT(S, T);
UT(S, L);
struct Node {
P lc, rc;
T val;
L lz;
bool ig;
};
I n;
vector<Node> v;
P new_node() { return v.eb(0, 0, S::id, L(), 1), len(v) - 1; }
LazySparseSeg(I s) : n(s) {
v.reserve(MXN);
new_node(); // blank node (index 0, value = id)
new_node(); // root node (index 1)
}
void make_children(P i) {
if (!v[i].lc) v[i].lc = new_node(), v[i].rc = new_node();
}
void put(L x, P i) {
v[i].lz = v[i].ig ? x : S::cmp(v[i].lz, x);
v[i].ig = 0;
}
void prop(P i, I lx, I rx) {
if (v[i].ig) return;
v[i].ig = 1;
v[i].val = S::ch(v[i].val, v[i].lz, lx, rx);
if (lx == rx) return;
make_children(i);
put(v[i].lz, v[i].lc);
put(v[i].lz, v[i].rc);
}
void update(L x, I l, I r) { return update(x, l, r, 1, 0, n - 1); }
void update(L x, I l, I r, P i, I lx, I rx) {
if (lx > r or rx < l) return prop(i, lx, rx);
if (l <= lx and rx <= r) return put(x, i), prop(i, lx, rx);
I mx = midpoint(lx, rx);
prop(i, lx, rx);
make_children(i);
P GET(lc, v[i]), GET(rc, v[i]); // copy before potential realloc
update(x, l, r, lc, lx, mx);
update(x, l, r, rc, mx + 1, rx);
v[i].val = S::op(v[lc].val, v[rc].val);
}
T query(I l, I r) { return query(l, r, 1, 0, n - 1); }
T query(I l, I r, P i, I lx, I rx) {
if (lx > r or rx < l) return S::id;
prop(i, lx, rx);
if (l <= lx and rx <= r) return v[i].val;
I mx = midpoint(lx, rx);
return S::op(query(l, r, v[i].lc, lx, mx),
query(l, r, v[i].rc, mx + 1, rx));
}
};