#include "factories.h"
#include<bits/stdc++.h>
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
const int N = 5e5 + 12;
int n, sz[N], head[N], p[N];
ll dep[N];
vector<array<int, 2>> g[N];
struct MNode { ll a = 1ll<<55;
MNode() {}
MNode(ll x) : a(x) {}
friend MNode operator+(const MNode &a, const MNode &b) {
return MNode(min(a.a, b.a));
}
};
template<class Node>
struct SegTree {
int n;
vector<Node> tree;
SegTree(int N) : n(N), tree(2*n) {}
void upd(int pos, Node val) {
for(tree[pos+=n]=val;pos/=2;)
tree[pos] = tree[2*pos]+tree[2*pos+1];
}
Node query(int l, int r) {
Node res;
for(l += n, r += n; l < r; l>>=1,r>>=1) {
if(l&1) res = res + tree[l++];
if(r&1) res = tree[--r] + res;
}
return res;
}
};
int tin[N], tout[N], timer = 0;
void sizes(int v) {
sz[v] = 1;
for(auto &F : g[v]) {
auto i = F[0];
auto w = F[1];
p[i] = v;
dep[i] = dep[v]+w;
g[i].erase(find(all(g[i]), array<int, 2>{v, w}));
sizes(i);
sz[v] += sz[i];
if(sz[i] > sz[g[v][0][0]])
swap(g[v][0], F);
}
}
void hld(int v) {
tin[v] = timer++;
for(auto &[i, w] : g[v]) {
head[i] = i == g[v][0][0] ? head[v] : i;
hld(i);
}
tout[v] = timer;
}
SegTree<MNode> st(0);
void Init(int N, int A[], int B[], int D[]) {
n = N;
st = SegTree<MNode>(n);
for(int i = 0; i+1 < n; i++) {
g[A[i]].push_back({B[i], D[i]});
g[B[i]].push_back({A[i], D[i]});
}
sizes(0);
hld(0);
}
template<int undo>
void update(int v) {
st.upd(tin[v], undo ? MNode() : MNode(dep[v]));
//cout << tin[v] << " _ " << dep[v] << " " << st.query(0, n).a << endl;
}
void query(int v, ll &ans) {
ll md = dep[v];
while(true) {
ans = min(ans, st.query(tin[v], tout[v]).a+md-2*dep[v]);
if(!v) break;
v = p[head[v]];
}
}
void solve(int s, int x[], int t, int y[], ll &ans) {
for(int i = 0; i < s; i++)
update<0>(x[i]);
//cout << "F " << tin[0] << " " << tout[0] << endl;
for(int i = 0; i < t; i++)
query(y[i], ans);
for(int i = 0; i < s; i++)
update<1>(x[i]);
}
long long Query(int s, int x[], int t, int y[]) {
ll ans = 1ll<<60;
solve(s, x, t, y, ans);
solve(t, y, s, x, ans);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12364 KB |
Output is correct |
2 |
Correct |
798 ms |
20512 KB |
Output is correct |
3 |
Correct |
783 ms |
20640 KB |
Output is correct |
4 |
Correct |
795 ms |
20688 KB |
Output is correct |
5 |
Correct |
654 ms |
21128 KB |
Output is correct |
6 |
Correct |
527 ms |
20544 KB |
Output is correct |
7 |
Correct |
784 ms |
20676 KB |
Output is correct |
8 |
Correct |
756 ms |
20548 KB |
Output is correct |
9 |
Correct |
616 ms |
21196 KB |
Output is correct |
10 |
Correct |
557 ms |
20484 KB |
Output is correct |
11 |
Correct |
773 ms |
20676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12236 KB |
Output is correct |
2 |
Correct |
3176 ms |
65144 KB |
Output is correct |
3 |
Correct |
2554 ms |
71248 KB |
Output is correct |
4 |
Correct |
1527 ms |
84044 KB |
Output is correct |
5 |
Correct |
2187 ms |
141548 KB |
Output is correct |
6 |
Correct |
2510 ms |
90716 KB |
Output is correct |
7 |
Correct |
1586 ms |
45824 KB |
Output is correct |
8 |
Correct |
950 ms |
45640 KB |
Output is correct |
9 |
Correct |
1069 ms |
52800 KB |
Output is correct |
10 |
Correct |
1586 ms |
47064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
12364 KB |
Output is correct |
2 |
Correct |
798 ms |
20512 KB |
Output is correct |
3 |
Correct |
783 ms |
20640 KB |
Output is correct |
4 |
Correct |
795 ms |
20688 KB |
Output is correct |
5 |
Correct |
654 ms |
21128 KB |
Output is correct |
6 |
Correct |
527 ms |
20544 KB |
Output is correct |
7 |
Correct |
784 ms |
20676 KB |
Output is correct |
8 |
Correct |
756 ms |
20548 KB |
Output is correct |
9 |
Correct |
616 ms |
21196 KB |
Output is correct |
10 |
Correct |
557 ms |
20484 KB |
Output is correct |
11 |
Correct |
773 ms |
20676 KB |
Output is correct |
12 |
Correct |
9 ms |
12236 KB |
Output is correct |
13 |
Correct |
3176 ms |
65144 KB |
Output is correct |
14 |
Correct |
2554 ms |
71248 KB |
Output is correct |
15 |
Correct |
1527 ms |
84044 KB |
Output is correct |
16 |
Correct |
2187 ms |
141548 KB |
Output is correct |
17 |
Correct |
2510 ms |
90716 KB |
Output is correct |
18 |
Correct |
1586 ms |
45824 KB |
Output is correct |
19 |
Correct |
950 ms |
45640 KB |
Output is correct |
20 |
Correct |
1069 ms |
52800 KB |
Output is correct |
21 |
Correct |
1586 ms |
47064 KB |
Output is correct |
22 |
Correct |
4034 ms |
90836 KB |
Output is correct |
23 |
Correct |
4655 ms |
93520 KB |
Output is correct |
24 |
Correct |
3810 ms |
96280 KB |
Output is correct |
25 |
Correct |
3181 ms |
100548 KB |
Output is correct |
26 |
Correct |
3053 ms |
96708 KB |
Output is correct |
27 |
Correct |
3138 ms |
138312 KB |
Output is correct |
28 |
Correct |
3004 ms |
94396 KB |
Output is correct |
29 |
Correct |
4508 ms |
95892 KB |
Output is correct |
30 |
Correct |
3407 ms |
94836 KB |
Output is correct |
31 |
Correct |
3343 ms |
96068 KB |
Output is correct |
32 |
Correct |
1107 ms |
54088 KB |
Output is correct |
33 |
Correct |
887 ms |
46128 KB |
Output is correct |
34 |
Correct |
1611 ms |
42852 KB |
Output is correct |
35 |
Correct |
1677 ms |
42792 KB |
Output is correct |
36 |
Correct |
1540 ms |
43976 KB |
Output is correct |
37 |
Correct |
1445 ms |
43944 KB |
Output is correct |