QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#353132#8235. Top ClusterGraphcityRE 0ms0kbC++202.0kb2024-03-13 21:35:002024-03-13 21:35:00

Judging History

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

  • [2024-03-13 21:35:00]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-03-13 21:35:00]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=5e5;

inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0' || ch>'9')
    {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}

int n,m,h[Maxn+5],mn; ll dis[Maxn+5];
int dfn[Maxn+5],st[Maxn+5][20],cur;
int X[Maxn+5],Y[Maxn+5];
vector<pair<int,int>> v[Maxn+5];
vector<int> w[Maxn+5];

inline int Get(int x,int y) {return dfn[x]<dfn[y]?x:y;}
inline int LCA(int x,int y)
{
    if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y);
    int len=__lg(y-x++); return Get(st[x][len],st[y-(1<<len)+1][len]);
}
inline ll Dis(int x,int y)
{
    if(!x || !y) return -1;
    return dis[x]+dis[y]-2*dis[LCA(x,y)];
}
inline void dfs(int x,int fa)
{
    dfn[x]=++cur,st[cur][0]=fa;
    for(auto [y,z]:v[x]) if(y!=fa)
        dis[y]=dis[x]+z,dfs(y,x);
}

int main()
{
    // freopen("1.in","r",stdin);

    n=read(),m=read();
    For(i,1,n) h[i]=min(read(),1ll*n),w[h[i]].push_back(i);
    For(i,1,n-1)
    {
        int a=read(),b=read(),c=read();
        v[a].emplace_back(b,c),v[b].emplace_back(a,c);
    }
    for(int i;;++i) if(w[i].empty()) {mn=i; break;}
    dfs(1,0);
    For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
        st[i][j]=Get(st[i][j-1],st[i+(1<<j-1)][j-1]);
    For(i,0,n)
    {
        int x=0,y=0; if(i) x=X[i-1],y=Y[i-1];
        for(auto k:w[i])
        {
            if(!x) {x=k; continue;} if(!y) {y=k; continue;}
            if(Dis(x,k)>Dis(x,y)) y=k; else if(Dis(y,k)>Dis(x,y)) x=k;
        } X[i]=x,Y[i]=y;
    }
    while(m--)
    {
        int x=read(),l=0,r=mn; ll k=read();
        while(l<r)
        {
            int mid=(l+r)/2;
            if(Dis(x,X[mid])>k || Dis(x,Y[mid])>k) r=mid;
            else l=mid+1;
        } printf("%d\n",l);
    }
    return 0;
}

詳細信息

Test #1:

score: 0
Runtime Error

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:


result: