QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#648330#8235. Top ClusterwyxqwqWA 693ms205316kbC++143.3kb2024-10-17 18:29:142024-10-17 18:29:15

Judging History

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

  • [2024-10-17 18:29:15]
  • 评测
  • 测评结果:WA
  • 用时:693ms
  • 内存:205316kb
  • [2024-10-17 18:29:14]
  • 提交

answer

#include<bits/stdc++.h>
#define vectorwyx maze
namespace vectorwyx{
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define sml(x,y) (x=min(x,y))
#define big(x,y) (x=max(x,y))
#define ll long long
#define uint unsigned
#define ull unsigned long long
#define umap unordered_map
#define db double
#define fo(i,x,y) for(int i=(x);i<=(y);++i)
#define go(i,x,y) for(int i=(x);i>=(y);--i)
#define ptc putchar
#define gtc getchar
#define emp emplace
#define re return
#define co continue
#define brk break
#define HH (ptc('\n'))
#define bctz __builtin_ctz
#define bclz __builtin_clz
#define bppc __builtin_popcount
using namespace std;
ll seed=chrono::system_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline int rm(int x,int y){return (int)(rnd()%(y-x+1ll)+x);}
inline int read(){signed ch=getchar();int x=0,f=1;while(!isdigit(ch)){if(ch==(int)('-'))f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline ll llread(){signed ch=getchar();ll x=0,f=1;while(!isdigit(ch)){if(ch==(int)('-'))f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
template<typename T> void out(T *a,int l,int r){fo(i,l,r) cout<<*(a+i)<<' ';puts("");}

const int N=1e6+5;
int w[N],mp[N],n,q;
vector<pii> e[N];

void add(int x,int y,int v){
    e[x].eb(y,v);
    e[y].eb(x,v);
}

namespace Tree{
ll dis[N];
int dfn[N],ti,lg[N],dep[N],fa[N];
pii st[21][N];
void init(){
    fo(i,2,n) lg[i]=lg[i>>1]+1;
    fo(i,1,lg[n])
        fo(j,1,n) st[i][j]=min(st[i-1][j],st[i-1][min(n,j+(1<<(i-1)))]);
}
int rmq(int l,int r){
    int w=lg[r-l+1];
    re min(st[w][l],st[w][r-(1<<w)+1]).se;
}
int lca(int x,int y){
    if(x==y) re x;
    if(dfn[x]>dfn[y]) swap(x,y);
    re fa[rmq(dfn[x]+1,dfn[y])];
}
ll get_dis(int x,int y){re dis[x]+dis[y]-2*dis[lca(x,y)];}
void dfs(int x){
    dfn[x]=++ti;dep[x]=dep[fa[x]]+1;
    st[0][ti]=mk(dep[x],x);
    for(auto i:e[x]) if(i.fi!=fa[x]){
        fa[i.fi]=x,dis[i.fi]=dis[x]+i.se;
        dfs(i.fi);
    }
    if(x==1) init();
}
}
using Tree::get_dis;

pii dia[N];ll len[N];

signed main(){
    cin>>n>>q;
    fo(i,1,n) w[i]=read();
    fo(i,2,n){
        int x=read(),y=read();
        add(x,y,read());
    }
    Tree::dfs(1);
    fo(i,1,n) if(w[i]<n) mp[w[i]]=i;
    if(!mp[0]){
        while(q--) puts("0");
        re 0;
    }
    dia[0]=mk(mp[0],mp[0]);
    int lim=n;
    fo(i,1,n-1){
        int x=mp[i];
        if(!x){lim=i;break;}
        ll v=get_dis(x,dia[i-1].fi);
        if(v>len[i-1]){len[i]=v,dia[i]=mk(dia[i-1].fi,x);}
        v=get_dis(x,dia[i-1].se);
        if(v>len[i-1]){len[i]=v,dia[i]=mk(dia[i-1].se,x);}
        if(len[i]) co;
        len[i]=len[i-1],dia[i]=dia[i-1];
    }
    while(q--){
        int x=read();ll k=llread();
        int l=0,r=lim-1,ans=lim;
        while(l<=r){
            int mid=(l+r)>>1;
            ll v=max(get_dis(x,dia[mid].fi),get_dis(x,dia[mid].se));
            if(v>k) ans=mid,r=mid-1;
            else l=mid+1;
        }
        cout<<ans<<'\n';
    }
    return 0;
}
}
/*
-------------------------------------------------
*/










signed main(){re vectorwyx::main();}

詳細信息

Test #1:

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

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: 550ms
memory: 186820kb

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: 0
Accepted
time: 558ms
memory: 184200kb

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:

ok 500000 numbers

Test #4:

score: 0
Accepted
time: 693ms
memory: 205316kb

input:

500000 500000
161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...

output:

0
250000
250000
69
10
250000
250000
0
5
250000
250000
250000
1
33
69
10
250000
250000
250000
10
0
250000
250000
250000
1
250000
250000
0
250000
10
5
0
33
10
250000
1
66
250000
1
250000
1
250000
250000
250000
16
1
1
0
5
10
250000
250000
0
250000
250000
250000
250000
250000
250000
250000
5
1
250000
1
...

result:

ok 500000 numbers

Test #5:

score: -100
Wrong Answer
time: 604ms
memory: 202828kb

input:

500000 500000
205699 141313 291987 999929528 111195 248994 999960789 391023 323134 92686 432395 316257 188889 219187 315056 39556 999959131 279778 30709 419259 242325 169515 137848 999905250 130641 245510 26 52987 999900052 144308 180276 162429 405263 130502 101937 108999 259339 137566 414011 304915...

output:

0
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
3
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
250000
0
250000
250000
250000
250000
250000
2
250...

result:

wrong answer 9574th numbers differ - expected: '0', found: '3'