QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#791321#8235. Top ClusterKonjac16WA 425ms84416kbC++231.9kb2024-11-28 18:04:572024-11-28 18:04:59

Judging History

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

  • [2024-11-28 18:04:59]
  • 评测
  • 测评结果:WA
  • 用时:425ms
  • 内存:84416kb
  • [2024-11-28 18:04:57]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize (3,"Ofast","inline")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN=5e5+10;
const int mod=1e9+7;
const int K=20;
int F[K+1][MAXN];
vector<pii>e[MAXN];
pii a[MAXN];
int dfn[MAXN];
int tt=0;
inline int Min(int u,int v){return dfn[u]<dfn[v]?u:v;}
ll d[MAXN];
void dfs(int u,int fa)
{
    dfn[u]=++tt;
    F[0][dfn[u]]=fa;
    for(auto&[v,w]:e[u])if(v!=fa)d[v]=d[u]+w,dfs(v,u);
}
inline int LCA(int u,int v)
{
    if(u==v)return u;
    u=dfn[u],v=dfn[v];
    if(u>v)swap(u,v);
    int k=__lg(v-u);
    return Min(F[k][u+1],F[k][v-(1<<k)+1]);
}
inline ll dis(int u,int v){return d[u]+d[v]-2ll*d[LCA(u,v)];}
pii b[MAXN];
signed main() {
	
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n,T;cin>>n>>T;
    for(int i=1;i<=n;i++)cin>>a[i].first,a[i].second=i;
    sort(a+1,a+n+1);
    for(int i=1,u,v,w;i<n;i++)cin>>u>>v>>w,e[u].emplace_back(v,w),e[v].emplace_back(u,w);
    dfs(1,0);
    for(int i=1;i<=K;i++)
        for(int j=1;j+(1<<i)-1<=n;j++)
            F[i][j]=Min(F[i-1][j],F[i-1][j+(1<<(i-1))]);
    int endd=n+1;
    for(int i=1;i<=n;i++)if(a[i].first!=i-1){endd=i;break;}
    b[0]={a[1].second,a[1].second};
    for(int i=1;i<endd;i++)
    {
        int u=a[i+1].second;
        b[i]=b[i-1];
        if(dis(u,b[i-1].first)>dis(b[i].first,b[i].second))b[i]={u,b[i-1].first};
        if(dis(u,b[i-1].second)>dis(b[i].first,b[i].second))b[i]={u,b[i-1].second};
    }
    //cerr<<b[1].first<<'\n';
    while(T--)
    {
        int u;ll k;cin>>u>>k;
        int l=0,r=endd-1,ans=-1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
         //   cerr<<mid<<' '<<max(dis(b[mid].first,u),dis(b[mid].second,u))<<'\n';
            if(max(dis(b[mid].first,u),dis(b[mid].second,u))>k)r=mid-1;
            else l=mid+1,ans=mid;
        }
        cout<<ans+1<<'\n';
    }
    return 0;
}
/*
6 1
a?b??c
2 2 2
 */

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11856kb

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: 425ms
memory: 84416kb

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
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
2500...

result:

wrong answer 2nd numbers differ - expected: '249999', found: '250000'