#include"factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using ii = pair<int, int>;
#define fi first
#define se second
#define pb push_back
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
template<class X, class Y>
bool minimize(X &x, const Y &y) {
X eps = 1e-9;
if (x > y + eps) {
x = y;
return true;
} else return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
X eps = 1e-9;
if (x + eps < y) {
x = y;
return true;
} else return false;
}
const int N = 5e5 + 7, LOG = 21;
long long oo = 1e14 + 7;
//sample program
int n, m, sz[N], par[N];
ll ans[N], d[N];
bool done[N];
vector<ii> adj[N];
// LCA O(1)
int nodes[2*N], rmq[2*N][LOG];
int _log2[2*N]{-1}, h[N];
int cnt = 0, _cnt = 0;
int pos[N];
void get_sz(int u, int par) {
sz[u] = 1;
for (ii e: adj[u]) if (e.fi != par && !done[e.fi]) {
get_sz(e.fi, u); sz[u] += sz[e.fi];
}
}
int centroid(int u, int par, int n) {
for (ii e: adj[u]) if (e.fi != par && !done[e.fi] && sz[e.fi] * 2 > n)
return centroid(e.fi, u, n);
return u;
}
void solve(int u, int pa) {
get_sz(u, -1); int rt = centroid(u, -1, sz[u]); done[rt] = 1;
par[rt] = pa;
for (ii e: adj[rt]) if (!done[e.fi])
solve(e.fi, rt);
}
void dfs(int u, int par = -1) {
nodes[++_cnt] = u, pos[u] = _cnt; rmq[_cnt][0] = u; _log2[_cnt] = _log2[_cnt >> 1] + 1;
for(ii e: adj[u]) if (e.fi != par) {
d[e.fi] = d[u] + e.se;
h[e.fi] = h[u] + 1; dfs(e.fi, u);
nodes[++_cnt] = u; rmq[_cnt][0] = u; _log2[_cnt] = _log2[_cnt >> 1] + 1;
}
}
#define MASK(i) (1LL<<(i))
#define MIN_HIGH(x, y) (h[x] < h[y] ? (x):(y))
int LCA(int u, int v) {
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = _log2[r - l + 1];
return MIN_HIGH(rmq[l][k], rmq[r - (1 << k) + 1][k]);
}
long long dis(int u, int v) {
return d[u] + d[v] - 2 * d[LCA(u, v)];
}
void Init(int N, int A[], int B[], int D[]){
n = N;
for (int i = 0; i < n - 1; i++) {
adj[A[i]].emplace_back(ii(B[i], D[i])); adj[B[i]].emplace_back(ii(A[i], D[i]));
ans[i] = oo;
}
ans[n - 1] = oo; solve(0, -1); dfs(0);
//preprocess LCA O(1)
for(int j = 1; j <= _log2[_cnt]; j++) {
for(int i = 1; j <= _log2[_cnt - i + 1]; i++) {
rmq[i][j] = MIN_HIGH(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
}
}
}
void upd(int u, bool t) {
for (int v = u; v >= 0; v = par[v])
if (t) minimize(ans[v], dis(u, v)); else ans[v] = oo;
}
long long get(int u) {
long long res = oo;
for (int v = u; v >= 0; v = par[v])
minimize(res, ans[v] + dis(u, v));
return res;
}
long long Query(int S, int X[], int T, int Y[]) {
long long res = 1e18;
for (int i = 0; i < S; i++) upd(X[i], 1);
for (int i = 0; i < T; i++) minimize(res, get(Y[i]));
for (int i = 0; i < S; i++) upd(X[i], 0);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12628 KB |
Output is correct |
2 |
Correct |
443 ms |
24388 KB |
Output is correct |
3 |
Correct |
488 ms |
24440 KB |
Output is correct |
4 |
Correct |
556 ms |
24388 KB |
Output is correct |
5 |
Correct |
580 ms |
24668 KB |
Output is correct |
6 |
Correct |
291 ms |
24388 KB |
Output is correct |
7 |
Correct |
505 ms |
24524 KB |
Output is correct |
8 |
Correct |
493 ms |
24420 KB |
Output is correct |
9 |
Correct |
591 ms |
24684 KB |
Output is correct |
10 |
Correct |
259 ms |
24468 KB |
Output is correct |
11 |
Correct |
473 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12372 KB |
Output is correct |
2 |
Correct |
2613 ms |
165020 KB |
Output is correct |
3 |
Correct |
3090 ms |
166308 KB |
Output is correct |
4 |
Correct |
990 ms |
150792 KB |
Output is correct |
5 |
Correct |
4337 ms |
180868 KB |
Output is correct |
6 |
Correct |
3470 ms |
153792 KB |
Output is correct |
7 |
Correct |
1869 ms |
51376 KB |
Output is correct |
8 |
Correct |
568 ms |
52024 KB |
Output is correct |
9 |
Correct |
1897 ms |
55632 KB |
Output is correct |
10 |
Correct |
1613 ms |
52636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12628 KB |
Output is correct |
2 |
Correct |
443 ms |
24388 KB |
Output is correct |
3 |
Correct |
488 ms |
24440 KB |
Output is correct |
4 |
Correct |
556 ms |
24388 KB |
Output is correct |
5 |
Correct |
580 ms |
24668 KB |
Output is correct |
6 |
Correct |
291 ms |
24388 KB |
Output is correct |
7 |
Correct |
505 ms |
24524 KB |
Output is correct |
8 |
Correct |
493 ms |
24420 KB |
Output is correct |
9 |
Correct |
591 ms |
24684 KB |
Output is correct |
10 |
Correct |
259 ms |
24468 KB |
Output is correct |
11 |
Correct |
473 ms |
24412 KB |
Output is correct |
12 |
Correct |
7 ms |
12372 KB |
Output is correct |
13 |
Correct |
2613 ms |
165020 KB |
Output is correct |
14 |
Correct |
3090 ms |
166308 KB |
Output is correct |
15 |
Correct |
990 ms |
150792 KB |
Output is correct |
16 |
Correct |
4337 ms |
180868 KB |
Output is correct |
17 |
Correct |
3470 ms |
153792 KB |
Output is correct |
18 |
Correct |
1869 ms |
51376 KB |
Output is correct |
19 |
Correct |
568 ms |
52024 KB |
Output is correct |
20 |
Correct |
1897 ms |
55632 KB |
Output is correct |
21 |
Correct |
1613 ms |
52636 KB |
Output is correct |
22 |
Correct |
3162 ms |
151640 KB |
Output is correct |
23 |
Correct |
3230 ms |
153996 KB |
Output is correct |
24 |
Correct |
4846 ms |
154248 KB |
Output is correct |
25 |
Correct |
4808 ms |
157896 KB |
Output is correct |
26 |
Correct |
4843 ms |
154412 KB |
Output is correct |
27 |
Correct |
6023 ms |
177688 KB |
Output is correct |
28 |
Correct |
1224 ms |
154696 KB |
Output is correct |
29 |
Correct |
4692 ms |
154256 KB |
Output is correct |
30 |
Correct |
4667 ms |
155848 KB |
Output is correct |
31 |
Correct |
4727 ms |
154304 KB |
Output is correct |
32 |
Correct |
1906 ms |
59220 KB |
Output is correct |
33 |
Correct |
559 ms |
54888 KB |
Output is correct |
34 |
Correct |
1249 ms |
52116 KB |
Output is correct |
35 |
Correct |
1344 ms |
51960 KB |
Output is correct |
36 |
Correct |
1632 ms |
52648 KB |
Output is correct |
37 |
Correct |
1579 ms |
52664 KB |
Output is correct |