//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include "factories.h"
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'
const ll INF = (1ll * 1e18);
int n;
vector<arr(2)> tr[500000];
int p[500000] , id[500000] , r[500000] , cmp[500000];
ll h[500000] , seg[2][2000001];
int seginf[2000001][2] , is[500000];
int bck[30000000][2] , bckcnt = 0;
void buildSeg(int l = 0 , int r = n - 1 , int i = 1){
seginf[i][0] = l , seginf[i][1] = r , seg[0][i] = INF , seg[1][i] = INF;
if(r > l){
int c = (r + l - 1) >> 1;
buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
}else is[l] = i;
}
void updF(int l , int r , int i , ll val){
if(seginf[i][0] >= l and seginf[i][1] <= r) seg[0][i] = min(seg[0][i] , val) , bck[bckcnt][0] = 0 , bck[bckcnt++][1] = i;
else if(seginf[i][1] >= l and seginf[i][0] <= r) updF(l , r , i << 1 , val) , updF(l , r , (i << 1) | 1 , val);
}
ll getF(int i){
ll res = seg[0][i];
while(i > 1) i /= 2 , res = min(res , seg[0][i]);
return res;
}
void updPt(int i , ll val){
while(i >= 1) seg[1][i] = min(seg[1][i] , val) , bck[bckcnt][0] = 1 , bck[bckcnt++][1] = i , i /= 2;
}
ll getPt(int l , int r , int i){
if(seginf[i][0] >= l and seginf[i][1] <= r) return seg[1][i];
if(seginf[i][1] >= l and seginf[i][0] <= r) return min(getPt(l , r , i << 1) , getPt(l , r , (i << 1) | 1));
return INF;
}
void ersSeg(){
for(int i = 0 ; i < bckcnt ; i++) seg[bck[i][0]][bck[i][1]] = INF;
bckcnt = 0;
}
void dfs1(int v = 0){
if(v == 0) p[0] = -1;
cmp[v] = 1;
for(auto d : tr[v]){
int u = d[0] , c = d[1];
if(u == p[v]) continue;
p[u] = v , h[u] = h[v] + (1ll * c);
dfs1(u);
cmp[v] += cmp[u];
}
}
int timer = 0;
void dfs2(int v = 0){
id[v] = timer++;
int u = -1;
for(auto el : tr[v]){
if(el[0] == p[v]) continue;
if(u == -1 or cmp[u] < cmp[el[0]]) u = el[0];
}
if(u == -1) return;
r[u] = r[v] , dfs2(u);
for(auto el : tr[v]){
if(el[0] == p[v] or el[0] == u) continue;
r[el[0]] = el[0] , dfs2(el[0]);
}
}
void Init(int N , int A[] , int B[] , int D[]){
n = N;
for(int i = 0 ; i < n - 1 ; i++) tr[A[i]].pb({B[i] , D[i]}) , tr[B[i]].pb({A[i] , D[i]});
dfs1() , dfs2() , buildSeg();
}
long long Query(int S , int X[] , int T , int Y[]){
for(int i = 0 ; i < T ; i++){
int v = Y[i] , cur = v;
while(cur != -1){
updF(id[r[cur]] , id[cur] , 1 , h[v]);
updPt(is[id[cur]] , h[v] - h[cur] - h[cur]);
cur = p[r[cur]];
}
}
ll res = INF;
for(int i = 0 ; i < S ; i++){
int v = X[i] , cur = v;
while(cur != -1){
res = min(res , h[v] + getF(is[id[cur]]) - h[cur] - h[cur]);
res = min(res , h[v] + getPt(id[r[cur]] , id[cur] , 1));
cur = p[r[cur]];
}
}
ersSeg();
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
43612 KB |
Output is correct |
2 |
Correct |
987 ms |
61360 KB |
Output is correct |
3 |
Correct |
862 ms |
59244 KB |
Output is correct |
4 |
Correct |
876 ms |
61376 KB |
Output is correct |
5 |
Correct |
445 ms |
61780 KB |
Output is correct |
6 |
Correct |
605 ms |
61268 KB |
Output is correct |
7 |
Correct |
888 ms |
59268 KB |
Output is correct |
8 |
Correct |
775 ms |
61264 KB |
Output is correct |
9 |
Correct |
468 ms |
61520 KB |
Output is correct |
10 |
Correct |
564 ms |
60244 KB |
Output is correct |
11 |
Correct |
829 ms |
58292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
43612 KB |
Output is correct |
2 |
Correct |
3790 ms |
114408 KB |
Output is correct |
3 |
Correct |
2688 ms |
117692 KB |
Output is correct |
4 |
Correct |
1706 ms |
115012 KB |
Output is correct |
5 |
Correct |
1762 ms |
145476 KB |
Output is correct |
6 |
Correct |
3033 ms |
118456 KB |
Output is correct |
7 |
Correct |
1436 ms |
72016 KB |
Output is correct |
8 |
Correct |
897 ms |
71624 KB |
Output is correct |
9 |
Correct |
904 ms |
75212 KB |
Output is correct |
10 |
Correct |
1499 ms |
72384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
43612 KB |
Output is correct |
2 |
Correct |
987 ms |
61360 KB |
Output is correct |
3 |
Correct |
862 ms |
59244 KB |
Output is correct |
4 |
Correct |
876 ms |
61376 KB |
Output is correct |
5 |
Correct |
445 ms |
61780 KB |
Output is correct |
6 |
Correct |
605 ms |
61268 KB |
Output is correct |
7 |
Correct |
888 ms |
59268 KB |
Output is correct |
8 |
Correct |
775 ms |
61264 KB |
Output is correct |
9 |
Correct |
468 ms |
61520 KB |
Output is correct |
10 |
Correct |
564 ms |
60244 KB |
Output is correct |
11 |
Correct |
829 ms |
58292 KB |
Output is correct |
12 |
Correct |
11 ms |
43612 KB |
Output is correct |
13 |
Correct |
3790 ms |
114408 KB |
Output is correct |
14 |
Correct |
2688 ms |
117692 KB |
Output is correct |
15 |
Correct |
1706 ms |
115012 KB |
Output is correct |
16 |
Correct |
1762 ms |
145476 KB |
Output is correct |
17 |
Correct |
3033 ms |
118456 KB |
Output is correct |
18 |
Correct |
1436 ms |
72016 KB |
Output is correct |
19 |
Correct |
897 ms |
71624 KB |
Output is correct |
20 |
Correct |
904 ms |
75212 KB |
Output is correct |
21 |
Correct |
1499 ms |
72384 KB |
Output is correct |
22 |
Correct |
6562 ms |
161980 KB |
Output is correct |
23 |
Correct |
5987 ms |
124036 KB |
Output is correct |
24 |
Correct |
5146 ms |
144064 KB |
Output is correct |
25 |
Correct |
5216 ms |
146328 KB |
Output is correct |
26 |
Correct |
4587 ms |
121600 KB |
Output is correct |
27 |
Correct |
2938 ms |
155864 KB |
Output is correct |
28 |
Correct |
2954 ms |
137056 KB |
Output is correct |
29 |
Correct |
4752 ms |
121576 KB |
Output is correct |
30 |
Correct |
4936 ms |
118568 KB |
Output is correct |
31 |
Correct |
5124 ms |
119568 KB |
Output is correct |
32 |
Correct |
977 ms |
89552 KB |
Output is correct |
33 |
Correct |
991 ms |
87236 KB |
Output is correct |
34 |
Correct |
2080 ms |
71484 KB |
Output is correct |
35 |
Correct |
2189 ms |
71420 KB |
Output is correct |
36 |
Correct |
1618 ms |
72036 KB |
Output is correct |
37 |
Correct |
1555 ms |
71896 KB |
Output is correct |