QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#616126#8235. Top Clusterlichenyu_acWA 700ms140672kbC++142.3kb2024-10-05 22:35:492024-10-05 22:35:50

Judging History

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

  • [2024-10-05 22:35:50]
  • 评测
  • 测评结果:WA
  • 用时:700ms
  • 内存:140672kb
  • [2024-10-05 22:35:49]
  • 提交

answer

#include<bits/stdc++.h>
#define fi first
#define se second
typedef long long ll;
typedef long long ll;
using namespace std;
const int N=5e5+10,M=N*2;

int head[N],ver[M],nxt[M],tot=1;
ll edge[M];
void add(int x,int y,ll z){
    ver[++tot]=y,edge[tot]=z,nxt[tot]=head[x],head[x]=tot;
}

pair<int,int>d[N];
int n,m,a[N],p[N];
int dfn[N<<1],cnt,fa[N<<1][25],dep[N],Log[N<<1];
ll dist[N];

void dfs(int x,int father){
    dfn[x]=++cnt,fa[cnt][0]=x;
    for(int i=head[x];i;i=nxt[i]){
        int y=ver[i];
        if(y==father)continue;
        dep[y]=dep[x]+1,dist[y]=dist[x]+edge[i];
        dfs(y,x);
        fa[++cnt][0]=x;
    }
}

int cmp(int x,int y){
    if(dep[x]<dep[y])return x;
    else return y;
}

void build(){
    for(int i=1;i<=cnt;i++)Log[i]=log2(i);

    for(int k=1;k<20;k++)
        for(int i=1;i+(1<<k)-1<=cnt;i++)
            fa[i][k]=cmp(fa[i][k-1],fa[i+(1<<(k-1))][k-1]);
}

int LCA(int x,int y){
    x=dfn[x],y=dfn[y];
    if(x>y)swap(x,y);
    int k=Log[y-x+1];
    return cmp(fa[x][k],fa[y-(1<<k)+1][k]);
}

ll get_dist(int x,int y){
    int lca=LCA(x,y);
    return dist[x]+dist[y]-2*dist[lca];
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]),p[i]=i;
    for(int i=1;i<n;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z),add(y,x,z);
    }
    dfs(1,0),build();
    sort(p+1,p+n+1,[](int x,int y){
        return a[x]<a[y];
    });
    int mex=n;
    for(int i=1;i<=n;i++)
        if(a[p[i]]!=i-1){
            mex=i-1;
            break;
        }
    for(int i=1;i<=n;i++){
        int x=p[i];
        if(i==1)d[i]={x,x};
        else{
            int Max=get_dist(d[i-1].fi,d[i-1].se);
            d[i]=d[i-1];
            if(get_dist(d[i-1].fi,x)>Max)Max=get_dist(d[i-1].fi,x),d[i]={d[i-1].fi,x};
            if(get_dist(d[i-1].se,x)>Max)Max=get_dist(d[i-1].se,x),d[i]={d[i-1].se,x};
        }
        // printf("%d: %d %d %d\n",i,x,d[i].fi,d[i].se);
    }
    while(m--){
        int x;ll k;scanf("%d%lld",&x,&k);
        int l=1,r=n;
        while(l<r){
            int mid=(l+r)>>1;
            ll now=max(get_dist(d[mid].fi,x),get_dist(d[mid].se,x));
            if(now>k)r=mid;
            else l=mid+1;
        }
        printf("%lld\n",min(a[p[r]],mex));
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 563ms
memory: 140672kb

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:

ok 500000 numbers

Test #3:

score: -100
Wrong Answer
time: 700ms
memory: 140492kb

input:

500000 500000
416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...

output:

0
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
2500...

result:

wrong answer 10955th numbers differ - expected: '0', found: '141712'