/
tree_monoid.hpp
159 lines (144 loc) · 4.56 KB
/
tree_monoid.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
152
153
154
155
156
157
158
159
#pragma once
#include "ds/segtree/segtree.hpp"
#include "graph/tree.hpp"
#include "alg/monoid/monoid_reverse.hpp"
template <typename TREE, typename Monoid, bool edge>
struct Tree_Monoid {
using MX = Monoid;
using X = typename MX::value_type;
TREE &tree;
int N;
SegTree<MX> seg;
SegTree<Monoid_Reverse<MX>> seg_r;
Tree_Monoid(TREE &tree) : tree(tree), N(tree.N) {
build([](int i) -> X { return MX::unit(); });
}
Tree_Monoid(TREE &tree, vc<X> &dat) : tree(tree), N(tree.N) {
build([&](int i) -> X { return dat[i]; });
}
template <typename F>
Tree_Monoid(TREE &tree, F f) : tree(tree), N(tree.N) {
build(f);
}
template <typename F>
void build(F f) {
if (!edge) {
auto f_v = [&](int i) -> X { return f(tree.V[i]); };
seg.build(N, f_v);
if constexpr (!MX::commute) { seg_r.build(N, f_v); }
} else {
auto f_e = [&](int i) -> X {
return (i == 0 ? MX::unit() : f(tree.v_to_e(tree.V[i])));
};
seg.build(N, f_e);
if constexpr (!MX::commute) { seg_r.build(N, f_e); }
}
}
void set(int i, X x) {
if constexpr (edge) i = tree.e_to_v(i);
i = tree.LID[i];
seg.set(i, x);
if constexpr (!MX::commute) seg_r.set(i, x);
}
void multiply(int i, X x) {
if constexpr (edge) i = tree.e_to_v(i);
i = tree.LID[i];
seg.multiply(i, x);
if constexpr (!MX::commute) seg_r.multiply(i, x);
}
X prod_path(int u, int v) {
auto pd = tree.get_path_decomposition(u, v, edge);
X val = MX::unit();
for (auto &&[a, b]: pd) { val = MX::op(val, get_prod(a, b)); }
return val;
}
// uv path 上で prod_path(u, x) が check を満たす最後の x
// なければ (つまり path(u,u) が ng )-1
template <class F>
int max_path(F check, int u, int v) {
if constexpr (edge) return max_path_edge(check, u, v);
if (!check(prod_path(u, u))) return -1;
auto pd = tree.get_path_decomposition(u, v, edge);
X val = MX::unit();
for (auto &&[a, b]: pd) {
X x = get_prod(a, b);
if (check(MX::op(val, x))) {
val = MX::op(val, x);
u = (tree.V[b]);
continue;
}
auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); };
if (a <= b) {
// 下り
auto i = seg.max_right(check_tmp, a);
return (i == a ? u : tree.V[i - 1]);
} else {
// 上り
int i = 0;
if constexpr (MX::commute) i = seg.min_left(check_tmp, a + 1);
if constexpr (!MX::commute) i = seg_r.min_left(check_tmp, a + 1);
if (i == a + 1) return u;
return tree.V[i];
}
}
return v;
}
X prod_subtree(int u, int root = -1) {
if (root == u) return prod_all();
if (root == -1 || tree.in_subtree(u, root)) {
int l = tree.LID[u], r = tree.RID[u];
return seg.prod(l + edge, r);
}
assert(!edge); // さぼり
u = tree.jump(u, root, 1);
int L = tree.LID[u], R = tree.RID[u];
return MX::op(seg.prod(0, L), seg.prod(R, N));
}
X prod_all() { return prod_subtree(tree.V[0]); }
inline X get_prod(int a, int b) {
if constexpr (MX::commute) {
return (a <= b) ? seg.prod(a, b + 1) : seg.prod(b, a + 1);
}
return (a <= b) ? seg.prod(a, b + 1) : seg_r.prod(b, a + 1);
}
private:
template <class F>
int max_path_edge(F check, int u, int v) {
static_assert(edge);
if (!check(MX::unit())) return -1;
int lca = tree.lca(u, v);
auto pd = tree.get_path_decomposition(u, lca, edge);
X val = MX::unit();
// climb
for (auto &&[a, b]: pd) {
assert(a >= b);
X x = get_prod(a, b);
if (check(MX::op(val, x))) {
val = MX::op(val, x);
u = (tree.parent[tree.V[b]]);
continue;
}
auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); };
int i = 0;
if constexpr (MX::commute) i = seg.min_left(check_tmp, a + 1);
if constexpr (!MX::commute) i = seg_r.min_left(check_tmp, a + 1);
if (i == a + 1) return u;
return tree.parent[tree.V[i]];
}
// down
pd = tree.get_path_decomposition(lca, v, edge);
for (auto &&[a, b]: pd) {
assert(a <= b);
X x = get_prod(a, b);
if (check(MX::op(val, x))) {
val = MX::op(val, x);
u = (tree.V[b]);
continue;
}
auto check_tmp = [&](X x) -> bool { return check(MX::op(val, x)); };
auto i = seg.max_right(check_tmp, a);
return (i == a ? u : tree.V[i - 1]);
}
return v;
}
};