线段树合并入门题(然而还是因为$sb$错误交了好几次),考虑每个节点建一棵动态开点的线段树,每次进行树上差分,这里给出一些主要代码片段:

更新:

1
2
3
4
5
6
inl void maintain(int x) {
if (t[ls(x)].w >= t[rs(x)].w) {
t[x].w = t[ls(x)].w, t[x].c = t[ls(x)].c;
}
else t[x].w = t[rs(x)].w, t[x].c = t[rs(x)].c;//c表示颜色,w表示权值
}

合并:

1
2
3
4
5
6
7
8
9
10
11
12
inl int merge(int p, int q, int l, int r) {
if (!p || !q)return p + q;
if (l == r) {
t[p].w += t[q].w, t[p].c = t[q].c;
return p;
}
re mid = l + r >> 1;
ls(p) = merge(ls(p), ls(q), l, mid);
rs(p) = merge(rs(p), rs(q), mid + 1, r);
maintain(p);
return p;
}

还有一个比较迷的点就是当我没有离散化时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
inl void maintain(int x) {
if (t[ls(x)].w >= t[rs(x)].w) {
t[x].w = t[ls(x)].w, t[x].c = t[ls(x)].c;
}
else t[x].w = t[rs(x)].w, t[x].c = t[rs(x)].c;
if(!t[x].w)t[x].c = 0;
}
inl void solve(int x) {
for (re i = head[x]; i; i = e[i].next) {
if (e[i].to != fa[x]) {
solve(e[i].to);
root[x] = merge(root[x], root[e[i].to], 1, s);
}
}
ans[x] = t[root[x]].w;
}

这样写是对的

但是如果离散化以后,这样就只有95$pts$???

于是乎经多次测试,离散化以后应该这样写:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
inl void maintain(int x) {
if (t[ls(x)].w >= t[rs(x)].w) {
t[x].w = t[ls(x)].w, t[x].c = t[ls(x)].c;
}
else t[x].w = t[rs(x)].w, t[x].c = t[rs(x)].c;
}
inl void solve(int x) {
for (re i = head[x]; i; i = e[i].next) {
if (e[i].to != fa[x]) {
solve(e[i].to);
root[x] = merge(root[x], root[e[i].to], 1, s);
}
}
ans[x] = t[root[x]].w ? mp[t[root[x]].c] : 0;
}

我认为这两种写法应该是完全没有差别的,但是结果并不一样$emmm$。

全部代码:

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
#pragma region revive
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define inl inline
#define re register int
#define fa(x) t[x].fa
#define son(x,y) t[x].child[y]
#define ls(x) t[x].child[0]
#define rs(x) t[x].child[1]
#define ll long long
const int inf = 0x3f3f3f3f;
#define lowbit(x) ((x) & (-x))
using namespace std;
#ifndef _DEBUG
#define getchar() (*(IOB.in.p++))
#define putchar(c) (*(IOB.out.p++)=(c))
#define io_eof() (IOB.in.p>=IOB.in.pend)
struct IOBUF { struct { char buff[1 << 26], *p, *pend; }in; struct { char buff[1 << 26], *p; }out; IOBUF() { in.p = in.buff; out.p = out.buff; in.pend = in.buff + fread(in.buff, 1, 1 << 26, stdin); }~IOBUF() { fwrite(out.buff, 1, out.p - out.buff, stdout); } }IOB;
#endif
template<typename IO>
inl void write(IO x) {
if (x == 0) return (void)putchar('0');
if (x < 0)putchar('-'), x = -x;
static char buf[30];
char* p = buf;
while (x) {
*(p++) = x % 10 + '0';
x /= 10;
}
while (p > buf)putchar(*(--p));
}
inl void writestr(const char *s) { while (*s != 0)putchar(*(s++)); }
template<typename IO>inl void writeln(IO x) { write(x), putchar('\n'); }
template<typename IO>inl void writesp(IO x) { write(x), putchar(' '); }
inl int readstr(char *s) {
char *begin = s, c = getchar();
while (c < 33 || c>127) {
c = getchar();
}
while (c >= 33 && c <= 127) {
*(s++) = c;
c = getchar();
}
*s = 0;
return s - begin;
}
template<typename IO>
inl IO read() {
IO x = 0;
register bool w = 0;
register char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') w = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return w ? -x : x;
}
#pragma endregion
#undef ls
#undef rs
#define ls(x) t[x].l
#define rs(x) t[x].r
int cnt, s;
struct node {
int l, r, w, c;
}t[10000001];
inl void maintain(int x) {
if (t[ls(x)].w >= t[rs(x)].w) {
t[x].w = t[ls(x)].w, t[x].c = t[ls(x)].c;
}
else t[x].w = t[rs(x)].w, t[x].c = t[rs(x)].c;
}
inl void add(int &k, int l, int r, int p, int w) {
if (!k)k = ++cnt;
if (l == r) {
t[k].w += w, t[k].c = p;
return;
}
re mid = l + r >> 1;
if (p <= mid)add(ls(k), l, mid, p, w);
else add(rs(k), mid + 1, r, p, w);
maintain(k);
}
inl int merge(int p, int q, int l, int r) {
if (!p || !q)return p + q;
if (l == r) {
t[p].w += t[q].w, t[p].c = t[q].c;
return p;
}
re mid = l + r >> 1;
ls(p) = merge(ls(p), ls(q), l, mid);
rs(p) = merge(rs(p), rs(q), mid + 1, r);
maintain(p);
return p;
}
int head[1000001], tot, top[1000001], dep[1000001], h[1000001], fa[1000001], siz[1000001], root[1000001], a[1000001], mp[1000001], ans[1000001];
struct edge {
int next, to;
}e[1000001];
inl void adde(int x, int y) {
e[++tot] = edge{ head[x],y }, head[x] = tot;
e[++tot] = edge{ head[y],x }, head[y] = tot;
}
inl void dfs1(int x) {
dep[x] = dep[fa[x]] + 1, siz[x] = 1;
for (re i = head[x]; i; i = e[i].next) {
if (fa[x] != e[i].to) {
fa[e[i].to] = x;
dfs1(e[i].to);
siz[x] += siz[e[i].to];
siz[e[i].to] > siz[h[x]] ? h[x] = e[i].to : 0;
}
}
}
inl void dfs2(int x) {
if (h[x]) {
top[h[x]] = top[x];
dfs2(h[x]);
}
for (re i = head[x]; i; i = e[i].next) {
if (!top[e[i].to]) {
top[e[i].to] = e[i].to;
dfs2(e[i].to);
}
}
}
inl int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]])swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
inl void solve(int x) {
for (re i = head[x]; i; i = e[i].next) {
if (e[i].to != fa[x]) {
solve(e[i].to);
root[x] = merge(root[x], root[e[i].to], 1, s);
}
}
ans[x] = t[root[x]].w ? mp[t[root[x]].c] : 0;
}
struct quiz {
int x, y, z;
}q[1000001];
signed main() {
re n = read<int>(), m = read<int>(), x, y, z, f;
top[1] = 1;
for (re i = 1; i < n; i++)adde(read<int>(), read<int>());
for (re i = 1; i <= m; i++) x = read<int>(), y = read<int>(), a[i] = z = read<int>(), q[i] = quiz{ x,y,z };
sort(a + 1, a + 1 + m);
s = unique(a + 1, a + 1 + m) - a - 1;
dfs1(1), dfs2(1);
for (re i = 1, k; i <= m; i++) k = q[i].z, mp[q[i].z = lower_bound(a + 1, a + 1 + s, q[i].z) - a] = k;
for (re i = 1; i <= m; i++) {
x = q[i].x, y = q[i].y, z = q[i].z, f = lca(x, y);
add(root[x], 1, s, z, 1), add(root[y], 1, s, z, 1), add(root[f], 1, s, z, -1);
if (fa[f])add(root[fa[f]], 1, s, z, -1);
}
solve(1);
for (re i = 1; i <= n; i++)writeln(ans[i]);
}