// #define _GLIBCXX_DEBUG
#include "factories.h"
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
ll n, Time;
ll tin[500005], tout[500005], dist[500005], dis[500005];
vector <pll> euler;
vector <pll> a[500005];
priority_queue <pll, vector <pll>, greater<pll>> q;
void dfs(ll u, ll pa)
{
tin[u]=euler.size();
euler.pb({dist[u], u});
for (pll ed:a[u])
{
ll v=ed.fi, w=ed.se;
if (v==pa)
continue;
dist[v]=dist[u]+w;
dfs(v, u);
euler.pb({dist[u], u});
}
tout[u]=euler.size()-1;
}
struct Sparse
{
ll n;
vector <ll> lg;
vector <vector <pll>> sp;
void init(ll n)
{
this->n=n;
lg.assign(n+1, 0);
for (ll i=2; i<=n; i++)
lg[i]=lg[i/2]+1;
sp.resize(lg[n]+1);
for (ll i=0; i<=lg[n]; i++)
sp[i].assign(n+1, {0, 0});
for (ll i=0; i<n; i++)
sp[0][i]=euler[i];
for (ll i=1; i<=lg[n]; i++)
for (ll j=0; j+(1<<i)<=n; j++)
sp[i][j]=min(sp[i-1][j], sp[i-1][j+(1<<(i-1))]);
}
ll LCA(ll l, ll r)
{
l=tin[l], r=tin[r];
if (l>r) swap(l, r);
ll j=lg[r-l+1];
return min(sp[j][l], sp[j][r-(1<<j)+1]).se;
}
ll distance(ll u, ll v)
{
ll p=LCA(u, v);
ll ans=dist[u]+dist[v]-dist[p]*2;
return ans;
}
} b;
void Init(int N, int A[], int B[], int C[])
{
n=N;
for (ll i=0; i<n-1; i++)
a[A[i]].pb({B[i], C[i]}), a[B[i]].pb({A[i], C[i]});
dfs(0, 0);
b.init(euler.size());
}
bool cmp(ll a, ll b)
{
return tin[a]<tin[b];
}
ll Query(int S, int X[], int T, int Y[])
{
map <ll, ll> Map;
vector <ll> ver;
vector <vector <pll>> edge;
for (ll i=0; i<S; i++)
ver.pb(X[i]);
for (ll i=0; i<T; i++)
ver.pb(Y[i]);
sort(ver.begin(), ver.end(), cmp);
for (ll i=ver.size()-1; i>=1; i--)
ver.pb(b.LCA(ver[i], ver[i-1]));
sort(ver.begin(), ver.end(), cmp);
ver.resize(unique(ver.begin(), ver.end())-ver.begin());
edge.resize(ver.size());
vector <ll> st;
for (ll i=0; i<ver.size(); i++)
{
dis[i]=-1;
Map[ver[i]]=i;
if (i==0)
{
st.pb(i);
continue;
}
while (tin[ver[st.back()]]>tin[ver[i]] || tout[ver[st.back()]]<tout[ver[i]])
st.pop_back();
edge[i].pb({st.back(), b.distance(ver[i], ver[st.back()])});
edge[st.back()].pb({i, b.distance(ver[i], ver[st.back()])});
st.pb(i);
}
for (ll i=0; i<S; i++)
dis[Map[X[i]]]=0, q.push({0, Map[X[i]]});
while (!q.empty())
{
ll u=q.top().se, disu=q.top().fi; q.pop();
if (dis[u]!=disu)
continue;
for (pll ed:edge[u])
{
ll v=ed.fi, w=ed.se;
if (dis[v]==-1 || dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
q.push({dis[v], v});
}
}
}
ll ans=1e18;
for (ll i=0; i<T; i++)
ans=min(ans, dis[Map[Y[i]]]);
return ans;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:106:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (ll i=0; i<ver.size(); i++)
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
12608 KB |
Output is correct |
2 |
Correct |
1837 ms |
23640 KB |
Output is correct |
3 |
Correct |
1930 ms |
23204 KB |
Output is correct |
4 |
Correct |
1918 ms |
24092 KB |
Output is correct |
5 |
Correct |
1627 ms |
23872 KB |
Output is correct |
6 |
Correct |
1261 ms |
23552 KB |
Output is correct |
7 |
Correct |
1949 ms |
23120 KB |
Output is correct |
8 |
Correct |
1908 ms |
24164 KB |
Output is correct |
9 |
Correct |
1687 ms |
23956 KB |
Output is correct |
10 |
Correct |
1241 ms |
23496 KB |
Output is correct |
11 |
Correct |
1932 ms |
23244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12372 KB |
Output is correct |
2 |
Correct |
1713 ms |
399000 KB |
Output is correct |
3 |
Correct |
1777 ms |
403696 KB |
Output is correct |
4 |
Correct |
1341 ms |
396632 KB |
Output is correct |
5 |
Correct |
1407 ms |
440004 KB |
Output is correct |
6 |
Correct |
1895 ms |
405092 KB |
Output is correct |
7 |
Correct |
2390 ms |
92312 KB |
Output is correct |
8 |
Correct |
1191 ms |
91744 KB |
Output is correct |
9 |
Correct |
1135 ms |
97696 KB |
Output is correct |
10 |
Correct |
2190 ms |
93580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
12608 KB |
Output is correct |
2 |
Correct |
1837 ms |
23640 KB |
Output is correct |
3 |
Correct |
1930 ms |
23204 KB |
Output is correct |
4 |
Correct |
1918 ms |
24092 KB |
Output is correct |
5 |
Correct |
1627 ms |
23872 KB |
Output is correct |
6 |
Correct |
1261 ms |
23552 KB |
Output is correct |
7 |
Correct |
1949 ms |
23120 KB |
Output is correct |
8 |
Correct |
1908 ms |
24164 KB |
Output is correct |
9 |
Correct |
1687 ms |
23956 KB |
Output is correct |
10 |
Correct |
1241 ms |
23496 KB |
Output is correct |
11 |
Correct |
1932 ms |
23244 KB |
Output is correct |
12 |
Correct |
10 ms |
12372 KB |
Output is correct |
13 |
Correct |
1713 ms |
399000 KB |
Output is correct |
14 |
Correct |
1777 ms |
403696 KB |
Output is correct |
15 |
Correct |
1341 ms |
396632 KB |
Output is correct |
16 |
Correct |
1407 ms |
440004 KB |
Output is correct |
17 |
Correct |
1895 ms |
405092 KB |
Output is correct |
18 |
Correct |
2390 ms |
92312 KB |
Output is correct |
19 |
Correct |
1191 ms |
91744 KB |
Output is correct |
20 |
Correct |
1135 ms |
97696 KB |
Output is correct |
21 |
Correct |
2190 ms |
93580 KB |
Output is correct |
22 |
Correct |
5103 ms |
420544 KB |
Output is correct |
23 |
Correct |
3915 ms |
425372 KB |
Output is correct |
24 |
Correct |
6007 ms |
425280 KB |
Output is correct |
25 |
Correct |
5453 ms |
429344 KB |
Output is correct |
26 |
Correct |
4143 ms |
406200 KB |
Output is correct |
27 |
Correct |
3766 ms |
450020 KB |
Output is correct |
28 |
Correct |
2919 ms |
417684 KB |
Output is correct |
29 |
Correct |
3853 ms |
405340 KB |
Output is correct |
30 |
Correct |
3970 ms |
404588 KB |
Output is correct |
31 |
Correct |
3818 ms |
405432 KB |
Output is correct |
32 |
Correct |
2747 ms |
112028 KB |
Output is correct |
33 |
Correct |
2165 ms |
109020 KB |
Output is correct |
34 |
Correct |
2641 ms |
90696 KB |
Output is correct |
35 |
Correct |
2556 ms |
90616 KB |
Output is correct |
36 |
Correct |
2969 ms |
91532 KB |
Output is correct |
37 |
Correct |
2898 ms |
91508 KB |
Output is correct |