QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#353729#8235. Top Cluster2745518585WA 651ms150132kbC++142.8kb2024-03-14 15:35:452024-03-14 15:35:45

Judging History

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

  • [2024-03-14 15:35:45]
  • 评测
  • 测评结果:WA
  • 用时:651ms
  • 内存:150132kb
  • [2024-03-14 15:35:45]
  • 提交

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];
vector<pair<int,ll>> 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 a[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) a[i][0]=LCA(b[i-1],b[i]);
        for(int i=1;i<=20;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(j+(1<<i)-1<=n) a[j][i]=check(a[j][i-1],a[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(a[x][lg[y-x+1]],a[y-(1<<lg[y-x+1])+1][lg[y-x+1]]);
    }
}
ll dis(int x,int y)
{
    if(x==y) return 0;
    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)
    {
        int x;
        scanf("%d",&x);
        if(x<=n) c[x]=i;
    }
    q=-1;
    while(c[q+1]) ++q;
    for(int i=1;i<=n-1;++i)
    {
        int x,y;
        ll w;
        scanf("%d%d%lld",&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) b[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];
        int z1=dis(c[i],d2[i]),z2=dis(d1[i],c[i]),z=dis(d1[i],d2[i]);
        if(z1>z&&z1>z2) d1[i]=c[i];
        else d2[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: 100
Accepted
time: 4ms
memory: 36528kb

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: 651ms
memory: 150132kb

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 20939th numbers differ - expected: '4', found: '52'