QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#748992#8235. Top ClusterccpyWA 561ms159160kbC++142.3kb2024-11-14 22:16:192024-11-14 22:16:19

Judging History

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

  • [2024-11-14 22:16:19]
  • 评测
  • 测评结果:WA
  • 用时:561ms
  • 内存:159160kb
  • [2024-11-14 22:16:19]
  • 提交

answer

#include<bits/stdc++.h>
#define MAX 500005
using namespace std;
#define ll long long
int n,Que;

#define pil pair<int,ll>
#define fi first
#define se second
vector<pil> e[MAX],E[MAX];

//lca预处理
int dep[MAX];ll d[MAX];
int bg[MAX],ceul;
int ST[MAX<<1][22];
void dfs(int x,int f){
    ST[++ceul][0]=x;
    bg[x]=ceul;
    for(auto o:e[x]){
        int y=o.fi;ll w=o.se;
        if(y==f) continue;
        dep[y]=dep[x]+1;
        d[y]=d[x]+w;
        dfs(y,x);
        ST[++ceul][0]=x;
    }
}
int MIN(int a,int b){
    if(dep[a]<dep[b]) return a;
    return b;
}
int lg2[MAX<<1];
int lca(int a,int b){
    a=bg[a],b=bg[b];
    if(a>b) swap(a,b);
    int k=lg2[b-a+1];
    return MIN(ST[a][k],ST[b-(1<<k)+1][k]);
}
ll dis(int x,int y){
    int lc=lca(x,y);
    return d[x]+d[y]-2*d[lc];
}
void init(){
    dfs(1,0);
    for(int i=2;i<=ceul;i++)
        lg2[i]=lg2[i>>1]+1;
    for(int j=1;(1<<j)<=ceul;j++)
        for(int i=1;i+(1<<j)-1<=ceul;i++)
            ST[i][j]=min(ST[i][j-1],ST[i+(1<<(j-1))][j-1]);
}

int W[MAX],pos[MAX],mx;
#define pii pair<int,int>
pii dia[MAX];
pii Max(pii a,pii b){
    ll l1=dis(a.fi,a.se),l2=dis(b.fi,b.se);
    if(l1>=l2) return a;
    return b;
}
void sol(){
    cin>>n>>Que;
    for(int i=1;i<=n;i++){
        W[i]=i;
        cin>>W[i];
        if(W[i]<=n) pos[W[i]]=i;
    }
    while(pos[mx]) mx++;
    for(int i=1,x,y;i<n;i++){
        ll w;
        cin>>x>>y>>w;
        e[x].push_back({y,w});
        e[y].push_back({x,w});
    }
    init();
    dia[0]={pos[0],pos[0]};
    for(int i=1;i<mx;i++){
        int x=dia[i-1].fi,y=dia[i-1].se;
        dia[i]=Max(pii{x,y},Max(pii{x,pos[i]},pii{y,pos[i]}));
    }
    for(int i=1;i<=Que;i++){
        int x;ll k;cin>>x>>k;
        int l=0,r=mx-1,rt=-1;
        if(i==6721){
            cout<<x<<" "<<k<<" "<<dis(x,dia[0].fi)<<" "<<dis(x,dia[0].se)<<endl;
        }
        while(l<=r){
            int mid=(l+r)>>1;
            int a=dia[mid].fi,b=dia[mid].se;
            if(dis(a,x)<=k&&dis(b,x)<=k){
                rt=mid;l=mid+1;
            }else r=mid-1;
        }
        if(n<=100) cout<<rt+1<<"\n";
    }
}
int main(){
    srand(time(0));
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    while(t--) sol();
}
// g++ b.cpp -Wl,--stack=10000000 -o b && b < in.txt > out.txt

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 33388kb

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: 561ms
memory: 159160kb

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:

40221 97 -64 -64

result:

wrong answer 1st numbers differ - expected: '0', found: '40221'