【题解】LOJ-2179-Luogu-3714-BZOJ-3257-[BJOI2017]树的难题

题目

https://loj.ac/problem/2179

题意

给一棵边上有颜色的数,每种颜色对应一个边权。定义路径的权值是不同颜色段的对应权值之和。求路径长度在 L 和 R 之间的最大值。

题解

  • 点分治。
  • 得到某个中心的所有直接儿子的子树中的所有结点的深度和到中心的权值。
  • 先对同色儿子中的子树中的结点进行查询和合并,在将不同色的结点进行查询和合并。
  • 查询和合并可以用单调队列,或者增加一个 log 来使用动态开点的线段树(可以以 O(两棵树存在点的个数的较小值) 的代价合并)或带 clear 标签的线段树。

代码

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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------
const int maxn = 2E5 + 100, INF = 2E9 + 3;
int w[maxn];
struct E {
int to, c;
};
vector<E> G[maxn];
int n, m, L, R, ans = -INF;

namespace tree {
#define mid ((l + r) >> 1)
#define lson l, mid
#define rson mid + 1, r
extern struct P *null, *pit;
struct P {
int max;
P *ls, *rs;
} *null = new P {-INF, nullptr, nullptr}, pool[maxn * 20], *pit = pool;

P* ins(P* t, int p, int v, int l = 1, int r = n) {
if (p < l || r < p) return t;
if (t == null) t = new (pit++) P{-INF, null, null};
t->max = max(t->max, v);
if (l != r) {
t->ls = ins(t->ls, p, v, lson);
t->rs = ins(t->rs, p, v, rson);
}
return t;
}

int query(P* t, int ql, int qr, int l = 1, int r = n) {
if (t == null || qr < l || r < ql) return -INF;
if (ql <= l && r <= qr) return t->max;
return max(query(t->ls, ql, qr, lson), query(t->rs, ql, qr, rson));
}

P* merge(P* a, P* b) {
if (a == null) return b; if (b == null) return a;
a->max = max(a->max, b->max);
a->ls = merge(a->ls, b->ls); a->rs = merge(a->rs, b->rs);
return a;
}

inline void clear() { pit = pool; }
}


int sz[maxn];
bool vis[maxn];
int get_sz(int u, int fa) {
int& s = sz[u] = 1;
for (E& e: G[u]) {
int v = e.to;
if (vis[v] || v == fa) continue;
s += get_sz(v, u);
}
return s;
}

void get_rt(int u, int fa, int s, int& m, int& rt) {
int t = s - sz[u];
for (E& e: G[u]) {
int v = e.to;
if (vis[v] || v == fa) continue;
get_rt(v, u, s, m, rt);
t = max(t, sz[v]);
}
if (t < m) { m = t; rt = u; }
}

void solve(void f(int)) {
static stack<int> S;
S.push(1);
while (!S.empty()) {
int u = S.top(); S.pop();
int tmp = INF; get_rt(u, -1, get_sz(u, -1), tmp, u);
vis[u] = true;

f(u);

for (E& e: G[u]) {
int v = e.to;
if (vis[v]) continue;
S.push(v);
}
}
}

void fuck(int u) {
using namespace tree;
sort(G[u].begin(), G[u].end(), [](const E& a, const E& b) { return a.c < b.c; });

clear();
P *rt = null, *cur = null;
int last_c = -1;

static vector<pair<int, int>> X;
function<void(int, int, int, int, int)> go =
[&](int u, int fa, int d, int last_c, int val) {
if (d > R) return;
X.emplace_back(d, val);
for (E& e: G[u]) {
int v = e.to;
if (v == fa || vis[v]) continue;
go(v, u, d + 1, e.c, val + (e.c == last_c ? 0 : w[e.c]));
}
};

for (E& e: G[u]) {
X.clear();
int v = e.to;
if (vis[v]) continue;
if (e.c != last_c) { rt = merge(rt, cur); cur = null; last_c = e.c; }
go(v, -1, 1, e.c, w[e.c]);
for (auto& x: X) {
int d = x.first, val = x.second;
if (d > R) continue;
if (d >= L) ans = max(ans, val);
ans = max(ans, query(rt, L - d, R - d) + val);
ans = max(ans, query(cur, L - d, R - d) + val - w[e.c]);
}
for (auto& x: X)
if (x.first <= R)
cur = ins(cur, x.first, x.second);
}


}

int main() {
#ifdef zerol
freopen("journey2.in", "r", stdin);
#endif
cin >> n >> m >> L >> R;
FOR (i, 1, m + 1) scanf("%d", &w[i]);
FOR (_, 1, n) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
G[u].push_back({v, c}); G[v].push_back({u, c});
}
solve(fuck);
cout << ans << endl;
}