P4292 [WC2010]重建计划

树上求一条链,求

最大。

显然分数规划,二分就是否存在链长符合要求$\sum e_i.v-mid\geq 0$

点分治可以做,常数有点大。

长链剖分,对一颗子树$u$来说$f[v][i]+f[u][j]+e.v\geq 0,L\leq i+j+1\leq U$。 即可

为了避免重复,选择边插入边计算,然后一遍合并,但是这样是$n^2$,可以选择用线段树维护$dfn[v]+i,dfn[u]+j$。

  • 在长链转移的时候,询问最小值,线段树也容易操作。
  • 对比使用指针,更加明确操作的数组在哪,更好维护。$dp[dfn[x]+y]\rightarrow dp[x][y]$
  • 由于边权可能$\leq -10^6$,线段树以及$-INF$,注意开大。

$BZOJ$过得去,$luogu,TLE,90$

代码
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, double>
#define mk make_pair
const double INF = 1e18;
const int N = 1e6 + 10;
const int mod = INF + 7;
const double eps = 1e-4;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
const int SEN = 1e5 + 10;
struct segmentTree
{
double maxx[SEN << 2], lazy[SEN << 2];
void addtag(int pos, int l, int r, double w)
{
lazy[pos] += w;
maxx[pos] += w;
}
void pushdown(int pos, int l, int r)
{
if (lazy[pos] != 0)
{
double w = lazy[pos];
int mid = (l + r) >> 1;
addtag(pos << 1, l, mid, w);
addtag(pos << 1 | 1, mid + 1, r, w);
lazy[pos] = 0;
}
}
void pushup(int pos)
{
maxx[pos] = max(maxx[pos << 1 | 1], maxx[pos << 1]);
}
double query(int ql, int qr, int pos, int l, int r)
{
if (ql > qr || ql <= 0 || qr <= 0)
return -INF;
if (ql <= l && r <= qr)
{
return maxx[pos];
}
pushdown(pos, l, r);
int mid = (l + r) >> 1;
double ans = -INF;
if (ql <= mid)
ans = max(ans, query(ql, qr, pos << 1, l, mid));
if (qr > mid)
ans = max(ans, query(ql, qr, pos << 1 | 1, mid + 1, r));
return ans;
}
void update(int ql, int qr, double w, int pos, int l, int r)
{
if (ql <= l && r <= qr)
{
addtag(pos, l, r, w);
return;
}
pushdown(pos, l, r);
int mid = (l + r) >> 1;

if (ql <= mid)
update(ql, qr, w, pos << 1, l, mid);
if (qr > mid)
update(ql, qr, w, pos << 1 | 1, mid + 1, r);
pushup(pos);
}
void modify(int k, double w, int pos, int l, int r)
{
if (l == r)
{
maxx[pos] = max(w, maxx[pos]);
lazy[pos] = 0;
return;
}
pushdown(pos, l, r);
int mid = (l + r) >> 1;

if (k <= mid)
modify(k, w, pos << 1, l, mid);
if (k > mid)
modify(k, w, pos << 1 | 1, mid + 1, r);
pushup(pos);
}
void init(int pos, int l, int r)
{
lazy[pos] = 0;
maxx[pos] = -INF;
if (l == r)
{
return;
}
int mid = (l + r) >> 1;
init(pos << 1, l, mid);
init(pos << 1 | 1, mid + 1, r);
}
} T;
int L, R;
int n;
bool Flag;
vector<pii> g[N];

double dp[N];
int dfn[N];
int len[N], son[N];
int tot;
void dfs1(int x, int fa)
{
for (pii now : g[x])
{
int to = now.first;
if (to == fa)
continue;
dfs1(to, x);
son[x] = len[to] > len[son[x]] ? to : son[x];
}
len[x] = len[son[x]] + 1;
}
void dfs2(int x, int fa)
{
dfn[x] = ++tot;
if (son[x])
dfs2(son[x], x);
for (pii now : g[x])
{
int to = now.first;
if (to == fa || son[x] == to)
continue;
dfs2(to, x);
}
}
void solve(int x, int fa)
{
if (Flag)
return;

for (pii now : g[x])
{
int to = now.first;
if (son[x] == to)
{
solve(son[x], x);

T.update(dfn[x] + 1, dfn[x] + len[x] - 1, now.second, 1, 1, n);
}
}
T.modify(dfn[x], 0, 1, 1, n);
double ans = -INF;
for (pii now : g[x])
{
int to = now.first;
if (to == fa || son[x] == to)
continue;
solve(to, x);
if (Flag)
return;
for (int i = 0; i < len[to]; i++)
{
int l = max(L - 1 - i, 0), r = min(len[x] - 1, (R - 1 - i));

ans = max(ans, T.query(dfn[to] + i, dfn[to] + i, 1, 1, n) + T.query(dfn[x] + l, dfn[x] + r, 1, 1, n) + now.second);
}

for (int i = 0; i < len[to]; i++)
T.modify(dfn[x] + i + 1, T.query(dfn[to] + i, dfn[to] + i, 1, 1, n) + now.second, 1, 1, n);
}

int l = max(L, 0), r = min(len[x] - 1, R);

ans = max(ans, T.query(dfn[x] + l, dfn[x] + r, 1, 1, n));
if (ans >= 0)
Flag = 1;
}

bool check(double x)
{
for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
g[i][j].second -= x;

Flag = 0;
T.init(1, 1, n);
solve(1, 0);

for (int i = 1; i <= n; i++)
for (int j = 0; j < g[i].size(); j++)
g[i][j].second += x;
return Flag;
}
int main()
{
n = read();
L = read(), R = read();
for (int i = 1; i < n; i++)
{
int u = read(), v = read(), w = read();
g[u].push_back(mk(v, w));
g[v].push_back(mk(u, w));
}

dfs1(1, 0);
dfs2(1, 0);
//cout << check(4) << endl;
double l = 0, r = 1e6;
while (l + eps < r)
{
double mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}

printf("%.3f", l);
}