#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define el "\n"
#define f first
#define s second
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
typedef long long ll;
const ll MM=5e5+5, MOD=1e9+7, LOG=20, INF=1e18;
int dep[MM], sz[MM], nxt[MM], in[MM], ot[MM], head[2*MM], ct=1, chn; ll dst[MM], spc[MM];
pair<int,int> st[LOG][4*MM];
bool blk[MM];
struct E {int nxt; pair<int,int> v; } e[2*MM];
void add(int u, int v, int w){
e[chn].v={v,w}; e[chn].nxt=head[u]; head[u]=chn++; }
int lg2(int x) {
return 32-__builtin_clz(x)-1; }
void df1(int u, int pa) {
dep[u]=dep[pa]+1; st[0][ct]={dep[u], u}; in[u]=ct++;
for(int i=head[u]; i!=-1; i=e[i].nxt) {
int v=e[i].v.f, w=e[i].v.s; if(v==pa) continue;
dst[v]=dst[u]+w; df1(v, u); sz[u]+=sz[v];
st[0][ct++]={dep[u], u}; }
ot[u]=ct; }
inline int qry(int l, int r) {
int lg=lg2(r-l+1);
int x=min(st[lg][l], st[lg][r-(1<<lg)+1]).s; return x; }
inline int lca(int u, int v) {
if(in[u]>in[v]) swap(u, v);
return qry(in[u], in[v]); }
void gsz(int u, int pa) {
sz[u]=1;
for(int i=head[u]; i!=-1; i=e[i].nxt) {
int v=e[i].v.f; if(blk[v]||v==pa) continue;
gsz(v, u); sz[u]+=sz[v]; } }
int ctr(int u, int pa, int nd) {
for(int i=head[u]; i!=-1; i=e[i].nxt) {
int v=e[i].v.f; if(blk[v]||v==pa) continue;
if(sz[v]*2>nd) return ctr(v, u, nd); }
return u; }
inline ll dist(int u, int v) {
return dst[u]+dst[v]-2LL*dst[lca(u, v)]; }
void slv(int u, int pa) {
gsz(u, -1); u=ctr(u, -1, sz[u]); blk[u]=1; nxt[u]=pa;
for(int i=head[u]; i!=-1; i=e[i].nxt) {
int v=e[i].v.f; if(!blk[v]) slv(v, u); } }
void Init(int N, int A[], int B[], int D[]) {
memset(head, -1, sizeof(head));
for(int i=0; i<N; ++i) spc[i]=INF;
for(int i=0; i<N-1; ++i) {
add(A[i], B[i], D[i]); add(B[i], A[i], D[i]); }
df1(0, 0); slv(0, -1);
for(int i=1; i<LOG; ++i)
for(int j=1; j+(1<<i)-1<=ct; ++j)
st[i][j]=min(st[i-1][j], st[i-1][j+(1<<(i-1))]); }
long long Query(int S, int X[], int T, int Y[]) {
vector<int> vs;
for(int i=0, c; i<S; ++i) {
c=X[i];
while(c!=-1)
spc[c]=min(spc[c], dist(X[i], c)), vs.pb(c), c=nxt[c]; }
long long ans=INF;
for(int i=0, c; i<T; ++i) {
c=Y[i];
while(c!=-1)
ans=min(ans, dist(Y[i], c)+spc[c]), c=nxt[c]; }
for(int &x:vs) assert(x!=-1), spc[x]=INF;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
16440 KB |
Output is correct |
2 |
Correct |
513 ms |
25496 KB |
Output is correct |
3 |
Correct |
682 ms |
25332 KB |
Output is correct |
4 |
Correct |
683 ms |
25804 KB |
Output is correct |
5 |
Correct |
782 ms |
25864 KB |
Output is correct |
6 |
Correct |
342 ms |
25348 KB |
Output is correct |
7 |
Correct |
675 ms |
25500 KB |
Output is correct |
8 |
Correct |
687 ms |
25472 KB |
Output is correct |
9 |
Correct |
713 ms |
25984 KB |
Output is correct |
10 |
Correct |
362 ms |
25292 KB |
Output is correct |
11 |
Correct |
687 ms |
25236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
16204 KB |
Output is correct |
2 |
Correct |
2339 ms |
195368 KB |
Output is correct |
3 |
Correct |
2937 ms |
198316 KB |
Output is correct |
4 |
Correct |
847 ms |
195276 KB |
Output is correct |
5 |
Correct |
3913 ms |
229204 KB |
Output is correct |
6 |
Correct |
3096 ms |
199508 KB |
Output is correct |
7 |
Correct |
2036 ms |
57096 KB |
Output is correct |
8 |
Correct |
533 ms |
57300 KB |
Output is correct |
9 |
Correct |
2315 ms |
61780 KB |
Output is correct |
10 |
Correct |
2081 ms |
58304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
16440 KB |
Output is correct |
2 |
Correct |
513 ms |
25496 KB |
Output is correct |
3 |
Correct |
682 ms |
25332 KB |
Output is correct |
4 |
Correct |
683 ms |
25804 KB |
Output is correct |
5 |
Correct |
782 ms |
25864 KB |
Output is correct |
6 |
Correct |
342 ms |
25348 KB |
Output is correct |
7 |
Correct |
675 ms |
25500 KB |
Output is correct |
8 |
Correct |
687 ms |
25472 KB |
Output is correct |
9 |
Correct |
713 ms |
25984 KB |
Output is correct |
10 |
Correct |
362 ms |
25292 KB |
Output is correct |
11 |
Correct |
687 ms |
25236 KB |
Output is correct |
12 |
Correct |
12 ms |
16204 KB |
Output is correct |
13 |
Correct |
2339 ms |
195368 KB |
Output is correct |
14 |
Correct |
2937 ms |
198316 KB |
Output is correct |
15 |
Correct |
847 ms |
195276 KB |
Output is correct |
16 |
Correct |
3913 ms |
229204 KB |
Output is correct |
17 |
Correct |
3096 ms |
199508 KB |
Output is correct |
18 |
Correct |
2036 ms |
57096 KB |
Output is correct |
19 |
Correct |
533 ms |
57300 KB |
Output is correct |
20 |
Correct |
2315 ms |
61780 KB |
Output is correct |
21 |
Correct |
2081 ms |
58304 KB |
Output is correct |
22 |
Correct |
3203 ms |
200820 KB |
Output is correct |
23 |
Correct |
3412 ms |
205344 KB |
Output is correct |
24 |
Correct |
4445 ms |
207304 KB |
Output is correct |
25 |
Correct |
4514 ms |
208556 KB |
Output is correct |
26 |
Correct |
4474 ms |
200292 KB |
Output is correct |
27 |
Correct |
5663 ms |
231436 KB |
Output is correct |
28 |
Correct |
1125 ms |
201780 KB |
Output is correct |
29 |
Correct |
4395 ms |
202520 KB |
Output is correct |
30 |
Correct |
4468 ms |
201860 KB |
Output is correct |
31 |
Correct |
4428 ms |
202508 KB |
Output is correct |
32 |
Correct |
2350 ms |
81532 KB |
Output is correct |
33 |
Correct |
510 ms |
72116 KB |
Output is correct |
34 |
Correct |
1502 ms |
68588 KB |
Output is correct |
35 |
Correct |
1485 ms |
68616 KB |
Output is correct |
36 |
Correct |
1901 ms |
69416 KB |
Output is correct |
37 |
Correct |
1924 ms |
69388 KB |
Output is correct |