QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#649351 | #8235. Top Cluster | Yurily | WA | 2388ms | 76048kb | C++20 | 2.4kb | 2024-10-17 22:57:56 | 2024-10-17 22:57:56 |
Judging History
answer
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int MAX=5e5+5;
long long g[MAX][3];
int n,tot,lst;
struct edge{
int nxt,to,w;
};
edge e[MAX*2];
int h[MAX],a[MAX],b[MAX],dep[MAX],f[MAX][21],q;
long long dis[MAX];
void addedge(int u,int v,int w){
e[++tot].to=v;
e[tot].nxt=h[u];
e[tot].w=w;
h[u]=tot;
}
void pre(int u,int fa){
dep[u]=dep[fa]+1;
for(int i=0;i<=19;++i)
f[u][i+1]=f[f[u][i]][i];
for(int i=h[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)
continue;
dis[v]=dis[u]+e[i].w;
f[v][0]=u;
pre(v,u);
}
}
long long dist(int x,int y){
if(x==y)
return 0;
int u=x,v=y;
if(dep[x]<dep[y])
swap(x,y);
for(int i=20;i>=0;--i){
if(dep[f[x][i]]>=dep[y])
x=f[x][i];
if(x==y)
return dis[u]+dis[v]-2*dis[x];
}
for(int i=20;i>=0;--i){
if(f[x][i]!=f[y][i]){
x=f[x][i];
y=f[y][i];
}
}
int lca=f[x][0];
return dis[u]+dis[v]-2*dis[lca];
}
// 快读
int read(){
int x = 0;
char c = getchar();
while(c < '0' || c > '9'){
c = getchar();
}
while(c >= '0' && c <= '9'){
x = x*10+c-'0';
c = getchar();
}
return x;
}
long long readL(){
long long x = 0;
char c = getchar();
while(c < '0' || c > '9'){
c = getchar();
}
while(c >= '0' && c <= '9'){
x = x*10+c-'0';
c = getchar();
}
return x;
}
int main(){
n=read(),q=read();
for(int i=1;i<=n;++i){
a[i]=read();
if(a[i]<=n)
b[a[i]]=i;
}
for(int i=1;i<=n-1;++i){
int u=read(),v=read(),w=read();
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=0;i<=n;++i){
if(!b[i]){
lst=i-1;
break;
}
}
pre(1,0);
if(lst>=0)
g[0][0]=g[0][1]=b[0];
for(int i=1;i<=lst;++i){
int d0=dist(b[i],g[i-1][0]);
int d1=dist(b[i],g[i-1][1]);
// cout<<b[i]<<" "<<g[i][0]<<" "<<g[i][1]<<"^"<<i<<" "<<d0<<" "<<d1<<endl;
g[i][0]=g[i-1][0],g[i][1]=g[i-1][1],g[i][2]=g[i-1][2];
if(d1>=d0&&d1>g[i-1][2]){
g[i][0]=b[i];
g[i][2]=d1;
}
else
if(d0>=d1&&d0>g[i-1][2]){
g[i][1]=b[i];
g[i][2]=d0;
}
// cout<<"&"<<i<<" "<<g[i][0]<<" "<<g[i][1]<<endl;
}
// cout<<lst<<" "<<endl;
while(q--){
int u=read();
long long d=readL();
int l=0,r=lst,res=-1;
while(l<=r){
int mid=l+r>>1;
// cout<<mid<<endl;
if(dist(u,g[mid][0])<=d&&dist(u,g[mid][1])<=d){
res=mid;
l=mid+1;
}
else
r=mid-1;
}
res++;
printf("%d\n",res);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14020kb
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: 2345ms
memory: 75356kb
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: 2388ms
memory: 76048kb
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: '250002'