QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#423766 | #5148. Tree Distance | xlpg0713 | RE | 0ms | 17500kb | C++20 | 2.7kb | 2024-05-28 16:13:45 | 2024-05-28 16:13:47 |
Judging History
answer
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cmath>
#define pb push_back
using ll = long long;
const int N = 2e5 + 10, M = 1e6 + 10;
std::vector<std::pair<int, int>> g[N], q[N];
int n, m, nn, rt, t, md, ms[N], sz[N], vs[N]; ll rs[N];
std::pair<int, ll> a[N]; int tp, st[N]; std::vector<int> vc[N];
namespace dis{
ll D[N]; int d[N], c, f[20][N<<1], p[N];
void pr(int x, int fa){
d[x]=d[fa]+1,++c,p[x]=c,f[0][c]=x;
for(auto [v,w]:g[x]) if(v!=fa){
D[v] = D[x] + w;
pr(v, x), f[0][++c] = x;
}
}
void bd(){
pr(1, 0); for(int i = 1; 1 << i <= c; i++)
for(int j = 1; j <= c + 1 - (1 << i); j++)
f[i][j]=d[f[i-1][j]]<d[f[i-1][j+(1<<i-1)]]?f[i-1][j]:f[i-1][j+(1<<i-1)];
}
int lca(int x, int y){
if((x=p[x])>(y=p[y])) std::swap(x, y); int k=std::__lg(y-x+1);
return d[f[k][x]]<d[f[k][y-(1<<k)+1]]?f[k][x]:f[k][y+1-(1<<k)];
}
ll ds(int x, int y){return D[x]+D[y]-2ll*D[lca(x,y)];}
} using dis::ds;
struct bt{
ll t[N]; bt(){memset(t, 63, sizeof t);}
void upd(int p,ll x){for(;p;p-=p&-p)t[p]=std::min(t[p],x);}
ll qmi(int p){ll r=9e18;for(;p<=n;p+=p&-p)r=std::min(r,t[p]);return r;}
}T;
void fd(int x, int fa){
sz[x] = 1, ms[x] = 0;
for(auto [v,w]:g[x]) if(!vs[v]&&v!=fa){
fd(v, x), sz[x] += sz[v];
ms[x] = std::max(ms[x], sz[v]);
} if((ms[x]=std::max(ms[x],nn-sz[x]))<md) md=ms[x],rt=x;
}
void gd(int x, int fa, ll d){
a[++t] = {x, d};
for(auto [v, w] : g[x])
if(v != fa && !vs[v]) gd(v, x, d+w);
}
void sol(int x){
vs[x] = 1; t = 0; gd(x, 0, 0);
std::sort(a + 1, a + t + 1);
tp = 0; for(int i = 1; i <= t; i++){
while(tp&&a[st[tp]].second>a[i].second)tp--;
if(tp) vc[a[i].first].pb(a[st[tp]].first); st[++tp] = i;
} tp = 0; for(int i = t; i; i--){
while(tp&&a[st[tp]].second>a[i].second)tp--;
if(tp) vc[a[st[tp]].first].pb(a[i].first); st[++tp] = i;
} for(auto [v,w]:g[x]) if(!vs[v])
rt = 0, md = n, nn = sz[v], fd(v, x), sol(rt);
}
int main(){
// freopen("in", "r", stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(0);std::cout.tie(0);
std::cin >> n; for(int i = 1, u, v, w; i < n; i++)
std::cin >> u >> v >> w, g[u].pb({v, w}), g[v].pb({u, w});
std::cin >> m; for(int i = 1, l, r; i <= m; i++)
std::cin >> l >> r, l >= r ? rs[i] = -1, void() : q[r].pb({l, i});
dis::bd(); rt=0, md=n, nn=n; fd(1, 0), sol(rt);
for(int i = 1; i <= n; i++){
for(int j:vc[i]) T.upd(j, ds(i, j));
for(auto [l,p]:q[i]) rs[p] = T.qmi(l);
} for(int i = 1; i <= m; i++) std::cout << rs[i] << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17500kb
input:
5 1 2 5 1 3 3 1 4 4 3 5 2 5 1 1 1 4 2 4 3 4 2 5
output:
-1 3 7 7 2
result:
ok 5 number(s): "-1 3 7 7 2"
Test #2:
score: -100
Runtime Error
input:
199999 31581 23211 322548833 176307 196803 690953895 34430 82902 340232856 36716 77480 466375266 7512 88480 197594480 95680 61864 679567992 19572 14126 599247796 188006 110716 817477802 160165 184035 722372640 23173 188594 490365246 54801 56250 304741654 10103 45884 643490340 127469 154479 214399361...