QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#451395#5148. Tree DistanceWilliamxzhWA 1857ms215464kbC++234.0kb2024-06-23 10:38:342024-06-23 10:38:34

Judging History

你现在查看的是最新测评结果

  • [2024-06-23 10:38:34]
  • 评测
  • 测评结果:WA
  • 用时:1857ms
  • 内存:215464kb
  • [2024-06-23 10:38:34]
  • 提交

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=1e7+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 || vis[y]) 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;
void dfs1(int x,int fath){
    int y,z;a[++m]=x;
    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;
    dfs1(x,0);sort(a+1,a+1+m);
    TOP=0;
    for(int i=1;i<=m;++i){
        while(TOP && d[st[TOP]]>=d[a[i]]) f[++k]={st[TOP],a[i],d[st[TOP]]+d[a[i]]},--TOP;
        //if(TOP) 
        st[++TOP]=a[i];
    }TOP=0;
    for(int i=m;i;--i){
        while(TOP && d[st[TOP]]>=d[a[i]]) f[++k]={a[i],st[TOP],d[st[TOP]]+d[a[i]]},--TOP;
        st[++TOP]=a[i];
    }TOP=0;
}
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);solve(rt);
    sort(f+1,f+1+k);
    for(int i=1;i<=k;++i){
        if(f[i].u>f[i].v) swap(f[i].u,f[i].v);
        f[i].w=dis(f[i].u,f[i].v);
    }
    //for(int i=1;i<=k;++i) printf("*** %d %d %lld\n",f[i].u,f[i].v,f[i].w);
    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;
}

详细

Test #1:

score: 100
Accepted
time: 11ms
memory: 160528kb

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: 1857ms
memory: 215464kb

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:

29573323
1178569098929
4088
65959
4366
7019193245
760172089
4867978
328055210
55721881562
2062364707
339719
92126
92126
4366
138216269
8212187
9404444928
2681285
4366
710854
114886
65959
1818252547
92126
91087
8367049186
26776689
5718199
710854
92126
1162886184
365255209
92126
710854
92126
710854
43...

result:

wrong answer 50th numbers differ - expected: '300541284329', found: '28418972379'