QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#451377 | #5148. Tree Distance | Williamxzh | WA | 479ms | 136728kb | C++23 | 4.1kb | 2024-06-23 10:22:55 | 2024-06-23 10:22:56 |
Judging History
answer
#include <bits/stdc++.h>
#define il inline
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
il int read(){
int x=0,c=getchar(),f=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=x*10+c-48,c=getchar();
return f?-x:x;
}
const int N=2e5+5,M=1e6+5,K=5e6+5;const ll inf=1e18;
int n,q,fa[N],de[N],size[N],dfn[N],top[N],son[N];ll s[N];
int vis[N],siz[N],mx[N],rt,tot;ll d[N],ans[M];
vector<pii> e[N],g[N];
il void add(int x,int y,int z){e[x].push_back({y,z});}
struct node{
int u,v;ll w;
il node(){u=v=0,w=0ll;}
il node(int u,int v,ll w) : u(u),v(v),w(w) {}
bool operator<(const node &x) const {return v<x.v;}
}f[K];int k;
void DFS(int x,int fath){
int y,z;fa[x]=fath,de[x]=de[fath]+1,siz[x]=1;
for(auto it:e[x]){
y=it.fi,z=it.se;if(y==fath) continue;
s[y]=s[x]+1ll*z,DFS(y,x),siz[x]+=siz[y];
if(siz[y]>siz[son[x]]) son[x]=y;
}
}
void DFS1(int x,int t){
top[x]=t;
if(son[x]) DFS1(son[x],t);
for(auto it:e[x]) if(it.fi!=fa[x] && it.fi!=son[x]) DFS1(it.fi,it.fi);
}
il int lca(int x,int y){
while(top[x]!=top[y]){
if(de[top[x]]<de[top[y]]) swap(x,y);
x=fa[top[x]];
}return de[x]<de[y]?x:y;
}
il ll dis(int x,int y){return s[x]+s[y]-2ll*s[lca(x,y)];}
void dfs0(int x,int fath){
++tot;
for(auto it:e[x])if(it.fi!=fath && !vis[it.fi]) dfs0(it.fi,x);
}
void dfs(int x,int fath){
int y;siz[x]=1,mx[x]=0;
for(auto it:e[x]){
y=it.fi;if(y==fath) continue;
dfs(y,x),siz[x]+=siz[y],mx[x]=max(mx[x],siz[y]);
}mx[x]=max(mx[x],tot-siz[x]);
if(!rt || mx[x]<mx[rt]) rt=x;
}
int m,a[N],st[N],TOP,bel[N],cur;
void dfs1(int x,int fath){
int y,z;a[++m]=x,bel[x]=cur;
for(auto it:e[x]){
y=it.fi,z=it.se;if(y==fath || vis[y]) continue;
d[y]=d[x]+1ll*z,dfs1(y,x);
}
}
il void calc(int x){
int y,z,u,v,w,p;m=0,d[x]=0ll;
for(auto it:e[x]){
y=it.fi,z=it.se;if(vis[y]) continue;
d[y]=1ll*z,cur=y,dfs1(y,x);
}
sort(a+1,a+1+m);
TOP=0;
for(int i=1;i<=m;++i){
while(TOP && d[st[TOP]]<d[a[i]]) --TOP;
if(TOP && bel[st[TOP]]!=bel[a[i]]) f[++k]={st[TOP],a[i],d[st[TOP]]+d[a[i]]};
st[++TOP]=a[i];
}TOP=0;
for(int i=1;i<=m;++i){
while(TOP && d[st[TOP]]>d[a[i]]) --TOP;
if(TOP && d[st[TOP]]!=d[a[i]] && bel[st[TOP]]!=bel[a[i]]) f[++k]={st[TOP],a[i],d[st[TOP]]+d[a[i]]};
st[++TOP]=a[i];
}TOP=0;
for(int i=1;i<=m;++i){
u=x,v=a[i];if(u>v) swap(u,v);
f[++k]={u,v,d[a[i]]};
}
}
void solve(int x){
int y;vis[x]=1,calc(x);
for(auto it:e[x]){
y=it.fi;if(vis[y]) continue;
rt=tot=0,dfs0(y,x),dfs(y,x),solve(rt);
}
}
ll t[N<<2];
il void pushup(ll x){t[x]=min(t[x<<1],t[x<<1|1]);}
void build(int x,int l,int r){
t[x]=inf;if(l==r) return ;
int mid=(l+r)>>1;
build(x<<1,l,mid),build(x<<1|1,mid+1,r);
pushup(x);
}
void update(int x,int l,int r,int p,ll v){
if(l==r){t[x]=min(t[x],v);return ;}
int mid=(l+r)>>1;
if(p<=mid) update(x<<1,l,mid,p,v);
else update(x<<1|1,mid+1,r,p,v);
pushup(x);
}
ll query(int x,int l,int r,int L,int R){
if(l>=L && r<=R) return t[x];
int mid=(l+r)>>1;
if(L>mid) return query(x<<1|1,mid+1,r,L,R);
else if(R<=mid) return query(x<<1,l,mid,L,R);
else return min(query(x<<1,l,mid,L,R),query(x<<1|1,mid+1,r,L,R));
}
int x,y,z,p;
int main(){
scanf("%d",&n);
for(int i=1;i<n;++i) x=read(),y=read(),z=read(),add(x,y,z),add(y,x,z);
scanf("%d",&q);
for(int i=1;i<=q;++i) x=read(),y=read(),g[y].push_back({x,i});
DFS(1,0),DFS1(1,1),build(1,1,n);
rt=0,tot=n,dfs(1,0);if(n<=10) solve(rt);
sort(f+1,f+1+k);
for(int i=1;i<=n;++i){
//for(auto it:f[i]) update(1,1,n,it.fi,it.se);
while(p<k && f[p+1].v<=i) ++p,update(1,1,n,f[p+1].u,f[p+1].w);
for(auto it:g[i]) {
if(it.fi==i) ans[it.se]=-1;
else ans[it.se]=query(1,1,n,it.fi,i-1);
}
}
for(int i=1;i<=q;++i) printf("%lld\n",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 82792kb
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
Wrong Answer
time: 479ms
memory: 136728kb
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...
output:
1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 ...
result:
wrong answer 1st numbers differ - expected: '29573323', found: '1000000000000000000'