//#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 1;
int st[M], fn[M], t, cnt, n , loc[M], ql, qr , TV = 1, lg[2 * M];
vector<pair<int, long long>> adj[M];
long long dis[M], seg[2][M << 2];
pair<int, int> par[20][M << 1];
void dfs(int v, int p, long long d1, int h) {
dis[v] = d1;
st[v] = t++;
loc[v] = cnt;
par[0][cnt++] = {h, v};
for (auto u : adj[v])
if (u.first != p) {
dfs(u.first, v, d1 + 1ll * u.second, h + 1);
par[0][cnt++] = {h, v};
}
fn[v] = t - 1;
return;
}
void update(int i, long long k, bool g) {
i = i + TV;
seg[g][i] = k;
i /= 2;
while (i) {
seg[g][i] = min(seg[g][i << 1], seg[g][i << 1 | 1]);
i /= 2;
}
}
long long get(bool g) {
ql = ql + TV;
qr = qr + 1 + TV;
long long ret = 1e18;
while (ql < qr) {
if (ql & 1)
ret = min(ret, seg[g][ql++]);
if (qr & 1)
ret = min(ret, seg[g][--qr]);
ql /= 2;
qr /= 2;
}
return ret;
}
void rem(int i, bool g) {
i = i + TV;
seg[g][i] = 1e18;
i /= 2;
while (i) {
seg[g][i] = 1e18;
i /= 2;
}
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for (int i = 0; i < n - 1; i++) {
adj[A[i]].push_back({B[i], D[i]});
adj[B[i]].push_back({A[i], D[i]});
}
dfs(0, 0, 0, 0);
for (int i = 2; i < 2 * n; i++)
lg[i] = lg[i / 2] + 1;
while (TV < N)
TV *= 2;
for (int i = 0; i < 2 * TV; i++)
seg[0][i] = seg[1][i] = 1e18;
for (int i = 1; i < 20; i++)
for (int j = 0; j + (1 << i) < 2 * n; j++)
par[i][j] = min(par[i - 1][j], par[i - 1][j + (1 << (i - 1))]);
}
long long Query(int S, int X[], int T, int Y[]) {
vector<pair<int, int>> sv;
for (int i = 0; i < S; i++) {
update(st[X[i]], dis[X[i]], 0);
sv.push_back({st[X[i]], X[i]});
}
for (int i = 0; i < T; i++) {
update(st[Y[i]], dis[Y[i]], 1);
sv.push_back({st[Y[i]], Y[i]});
}
sort(sv.begin(), sv.end());
long long ans = 1e18 ;
for (int i = 0; i < int(sv.size()); i++) {
int v = sv[i].second;
ql = st[v];
qr = fn[v];
long long f = get(0);
ql = st[v];
qr = fn[v];
f += get(1);
f -= (2ll * dis[v]);
ans = min(ans, f);
if (i < int(sv.size()) - 1) {
int vv = sv[i].second, uu = sv[i + 1].second;
if (loc[vv] > loc[uu])
swap(vv, uu);
int SV = loc[vv], SU = loc[uu], LG = lg[SU - SV + 1];
pair<int, int> lca = min(par[LG][SV], par[LG][SU - (1 << LG) + 1]);
v = lca.second;
ql = st[v];
qr = fn[v];
f = get(0);
ql = st[v];
qr = fn[v];
f += get(1);
f -= (2ll * dis[v]);
ans = min(ans, f);
}
}
for (int i = 0; i < S; i++)
rem(st[X[i]], 0);
for (int i = 0; i < T; i++)
rem(st[Y[i]], 1);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
64088 KB |
Output is correct |
2 |
Correct |
529 ms |
81484 KB |
Output is correct |
3 |
Correct |
682 ms |
81732 KB |
Output is correct |
4 |
Correct |
641 ms |
81684 KB |
Output is correct |
5 |
Correct |
789 ms |
82064 KB |
Output is correct |
6 |
Correct |
496 ms |
81592 KB |
Output is correct |
7 |
Correct |
671 ms |
81860 KB |
Output is correct |
8 |
Correct |
634 ms |
81696 KB |
Output is correct |
9 |
Correct |
791 ms |
82260 KB |
Output is correct |
10 |
Correct |
493 ms |
81480 KB |
Output is correct |
11 |
Correct |
669 ms |
81492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
64092 KB |
Output is correct |
2 |
Correct |
1440 ms |
259784 KB |
Output is correct |
3 |
Correct |
1742 ms |
265652 KB |
Output is correct |
4 |
Correct |
1305 ms |
257708 KB |
Output is correct |
5 |
Correct |
1911 ms |
301168 KB |
Output is correct |
6 |
Correct |
2091 ms |
265648 KB |
Output is correct |
7 |
Correct |
1448 ms |
131580 KB |
Output is correct |
8 |
Correct |
916 ms |
130492 KB |
Output is correct |
9 |
Correct |
1307 ms |
135840 KB |
Output is correct |
10 |
Correct |
1403 ms |
131936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
64088 KB |
Output is correct |
2 |
Correct |
529 ms |
81484 KB |
Output is correct |
3 |
Correct |
682 ms |
81732 KB |
Output is correct |
4 |
Correct |
641 ms |
81684 KB |
Output is correct |
5 |
Correct |
789 ms |
82064 KB |
Output is correct |
6 |
Correct |
496 ms |
81592 KB |
Output is correct |
7 |
Correct |
671 ms |
81860 KB |
Output is correct |
8 |
Correct |
634 ms |
81696 KB |
Output is correct |
9 |
Correct |
791 ms |
82260 KB |
Output is correct |
10 |
Correct |
493 ms |
81480 KB |
Output is correct |
11 |
Correct |
669 ms |
81492 KB |
Output is correct |
12 |
Correct |
12 ms |
64092 KB |
Output is correct |
13 |
Correct |
1440 ms |
259784 KB |
Output is correct |
14 |
Correct |
1742 ms |
265652 KB |
Output is correct |
15 |
Correct |
1305 ms |
257708 KB |
Output is correct |
16 |
Correct |
1911 ms |
301168 KB |
Output is correct |
17 |
Correct |
2091 ms |
265648 KB |
Output is correct |
18 |
Correct |
1448 ms |
131580 KB |
Output is correct |
19 |
Correct |
916 ms |
130492 KB |
Output is correct |
20 |
Correct |
1307 ms |
135840 KB |
Output is correct |
21 |
Correct |
1403 ms |
131936 KB |
Output is correct |
22 |
Correct |
2562 ms |
263360 KB |
Output is correct |
23 |
Correct |
2810 ms |
263880 KB |
Output is correct |
24 |
Correct |
3126 ms |
268360 KB |
Output is correct |
25 |
Correct |
3203 ms |
269536 KB |
Output is correct |
26 |
Correct |
3870 ms |
267308 KB |
Output is correct |
27 |
Correct |
3315 ms |
283816 KB |
Output is correct |
28 |
Correct |
2306 ms |
250088 KB |
Output is correct |
29 |
Correct |
3478 ms |
254488 KB |
Output is correct |
30 |
Correct |
3592 ms |
253444 KB |
Output is correct |
31 |
Correct |
3635 ms |
254548 KB |
Output is correct |
32 |
Correct |
1442 ms |
130664 KB |
Output is correct |
33 |
Correct |
1086 ms |
124076 KB |
Output is correct |
34 |
Correct |
1215 ms |
122968 KB |
Output is correct |
35 |
Correct |
1303 ms |
122728 KB |
Output is correct |
36 |
Correct |
1459 ms |
123732 KB |
Output is correct |
37 |
Correct |
1516 ms |
123752 KB |
Output is correct |