QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353719#8235. Top Cluster2745518585WA 4ms27380kbC++142.7kb2024-03-14 15:29:522024-03-14 15:29:53

Judging History

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

  • [2024-03-14 15:29:53]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:27380kb
  • [2024-03-14 15:29:52]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1000001,M=31;
int n,m,q,tot,b[N],c[N],d1[N],d2[N],e[N];
vector<pair<int,int>> a[N];
struct tree
{
    int f,s,d,t,z,b;
    ll h;
}T[N];
void dfs1(int x)
{
    T[x].s=1;
    T[x].d=T[T[x].f].d+1;
    for(auto i:a[x])
    {
        if(i.first==T[x].f) continue;
        T[i.first].f=x;
        T[i.first].h=T[x].h+i.second;
        dfs1(i.first);
        T[x].s+=T[i.first].s;
        if(T[i.first].s>T[T[x].z].s) T[x].z=i.first;
    }
}
void dfs2(int x,int t)
{
    T[x].t=t;
    T[x].b=++tot;
    if(T[x].z) dfs2(T[x].z,t);
    for(auto i:a[x])
    {
        if(i.first==T[x].f||i.first==T[x].z) continue;
        dfs2(i.first,i.first);
    }
}
int LCA(int x,int y)
{
    while(T[x].t!=T[y].t)
    {
        if(T[T[x].t].d>T[T[y].t].d) x=T[T[x].t].f;
        else y=T[T[y].t].f;
    }
    if(T[x].d<T[y].d) return x;
    else return y;
}
namespace ST
{
    int b[N][M],lg[N];
    int check(int x,int y)
    {
        return T[x].d<T[y].d?x:y;
    }
    void init()
    {
        for(int i=0;i<=20;++i)
        {
            for(int j=(1<<i);j<=min((1<<(i+1))-1,n);++j) lg[j]=i;
        }
        for(int i=2;i<=n;++i) b[i][0]=LCA(e[i-1],e[i]);
        for(int i=1;i<=20;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(j+(1<<i)-1<=n) b[j][i]=check(b[j][i-1],b[j+(1<<(i-1))][i-1]);
            }
        }
    }
    int sum(int x,int y)
    {
        x=T[x].b,y=T[y].b;
        if(x>y) swap(x,y);
        ++x;
        return check(b[x][lg[y-x+1]],b[y-(1<<lg[y-x+1])+1][lg[y-x+1]]);
    }
}
ll dis(int x,int y)
{
    if(x==y) return x;
    return T[x].h+T[y].h-T[ST::sum(x,y)].h*2;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&b[i]);
        if(b[i]<=n) c[b[i]]=i;
    }
    q=-1;
    while(c[q+1]) ++q;
    for(int i=1;i<=n-1;++i)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        a[x].push_back(make_pair(y,w));
        a[y].push_back(make_pair(x,w));
    }
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=n;++i) e[T[i].b]=i;
    ST::init();
    d1[0]=d2[0]=c[0];
    for(int i=1;i<=q;++i)
    {
        d1[i]=d1[i-1],d2[i]=d2[i-1];
        if(dis(d1[i],c[i])>dis(d1[i],d2[i])) d2[i]=c[i];
        else if(dis(c[i],d2[i])>dis(d1[i],d2[i])) d1[i]=c[i];
    }
    for(int i=1;i<=m;++i)
    {
        int x;
        ll k;
        scanf("%d%lld",&x,&k);
        int l=0,r=q+1;
        while(l<r)
        {
            int z=l+r>>1;
            if(dis(x,d1[z])<=k&&dis(x,d2[z])<=k) l=z+1;
            else r=z;
        }
        printf("%d\n",l);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 27380kb

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:

0
0
3
4

result:

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