QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#766919#8235. Top ClusterCoffinsWA 717ms144492kbC++142.0kb2024-11-20 19:17:402024-11-20 19:17:41

Judging History

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

  • [2024-11-20 19:17:41]
  • 评测
  • 测评结果:WA
  • 用时:717ms
  • 内存:144492kb
  • [2024-11-20 19:17:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pil=pair<int,ll>;
const int N=5e5+5;
int n,m,q,d[N];ll ds[N];
vector<pil> edge[N];
int ls[N<<1],lg[N<<1];
int f[22][N<<1],ct;
int L[N],R[N],a[N],tg[N];
void dfs(int u,int f)
{
    L[u]=++ct,ls[ct]=u;
    for(auto e:edge[u])
    {
        int v=e.first;
        ll w=e.second;
        if(v==f)continue;
        d[v]=d[u]+1,
        ds[v]=ds[u]+w,
        dfs(v,u);
    }R[u]=++ct,ls[ct]=u;
}int lca(int x,int y)
{
    if(L[x]>L[y])swap(x,y);
    int g=lg[L[y]-L[x]+1];
    int a=f[g][L[x]];
    int b=f[g][R[x]-(1<<g)+1];
    return (d[a]<d[b])?a:b;
}ll dis(int x,int y)
{
    int l=lca(x,y);
    return ds[x]+ds[y]-ds[l]*2;
}int lp[N],rp[N],t=-1;
ll ck(int u,ll t)
{return max(dis(u,lp[t]),dis(u,rp[t]));}
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>q;for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)if(a[i]<n)tg[a[i]]=i;
    while(tg[t+1])t++;
    for(int i=2;i<=n;i++)
    {
        int x,y;ll w;cin>>x>>y>>w;
        edge[x].push_back({y,w});
        edge[y].push_back({x,w});
    }dfs(1,0);m=ct;
    for(int i=2;i<=m;i++)lg[i]=lg[i>>1]+1;
    for(int i=1;i<=m;i++)f[0][i]=ls[i];
    for(int i=1;i<=lg[m];i++)
    for(int j=1;j+(1<<i)-1<=m;j++)
    {
        int x=f[i-1][j];
        int y=f[i-1][j+(1<<i-1)];
        f[i][j]=(d[x]<d[y])?x:y;
    }lp[0]=rp[0]=tg[0];
    for(int c=1;c<=t;c++)
    {
        ll x=dis(lp[c-1],rp[c-1]);
        ll y=dis(lp[c-1],tg[c]);
        ll z=dis(rp[c-1],tg[c]);
        ll mx=max({x,y,z});if(mx==x)
        lp[c]=lp[c-1],rp[c]=rp[c-1];
        else if(mx==y)lp[c]=lp[c-1],
        rp[c]=tg[c];else
        lp[c]=rp[c-1],rp[c]=tg[c];
    }for(int i=1;i<=q;i++)
    {
        int u;ll k;cin>>u>>k;
        int l=0,r=t,mid;
        while(l<r)
        {
            mid=(l+r)>>1;
            if(ck(u,mid+1)<=k)
            l=mid+1;else r=mid;
        }if(ck(u,l)>k)l=-1;
        cout<<l+1<<'\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 7ms
memory: 34256kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 717ms
memory: 144492kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 36936th numbers differ - expected: '14', found: '4'