QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353152 | #8235. Top Cluster | Graphcity | WA | 3ms | 12024kb | C++20 | 2.2kb | 2024-03-13 21:49:47 | 2024-03-13 21:49:47 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int Maxn=5e5;
inline ll read()
{
ll x=0,f=1;
char ch=getchar();
while(ch<'0' || ch>'9')
{
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,h[Maxn+5],mn; ll dis[Maxn+5];
int dfn[Maxn+5],st[Maxn+5][20],cur;
int X[Maxn+5],Y[Maxn+5];
vector<pair<int,int>> v[Maxn+5];
vector<int> w[Maxn+5];
inline int Get(int x,int y) {return dfn[x]<dfn[y]?x:y;}
inline int LCA(int x,int y)
{
if(x==y) return x; if((x=dfn[x])>(y=dfn[y])) swap(x,y);
int len=__lg(y-x++); return Get(st[x][len],st[y-(1<<len)+1][len]);
}
inline ll Dis(int x,int y)
{
if(!x || !y) return -1;
return dis[x]+dis[y]-dis[LCA(x,y)]*2;
}
inline void dfs(int x,int fa)
{
dfn[x]=++cur,st[cur][0]=fa;
for(auto [y,z]:v[x]) if(y!=fa)
dis[y]=dis[x]+z,dfs(y,x);
}
int main()
{
// freopen("1.in","r",stdin);
n=read(),m=read();
For(i,1,n) h[i]=min(read(),1ll+n),w[h[i]].push_back(i);
For(i,1,n-1)
{
int a=read(),b=read(),c=read();
v[a].emplace_back(b,c),v[b].emplace_back(a,c);
}
for(int i=0;;++i) if(w[i].empty()) {mn=i; break;}
dfs(1,0);
For(j,1,19) for(int i=1;i+(1<<j)-1<=n;++i)
st[i][j]=Get(st[i][j-1],st[i+(1<<j-1)][j-1]);
For(i,0,n+1)
{
int x=0,y=0; if(i) x=X[i-1],y=Y[i-1];
for(auto k:w[i])
{
if(!x) {x=k; continue;} if(!y) {y=k; continue;}
if(Dis(x,k)>Dis(x,y)) y=k; else if(Dis(y,k)>Dis(x,y)) x=k;
} X[i]=x,Y[i]=y;
}
while(m--)
{
int x=read(),l=0,r=mn; ll k=read();
if(!w[1].empty()) {int y=w[1].front(); if(Dis(x,y)>k) r=1;}
if(!w[3].empty()) {int y=w[3].front(); if(Dis(x,y)>k) r=1;}
while(l<r)
{
int mid=(l+r)/2;
if(Dis(x,X[mid])>k || Dis(x,Y[mid])>k) r=mid;
else l=mid+1;
} printf("%d\n",l);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 12024kb
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 1 4
result:
wrong answer 3rd numbers differ - expected: '3', found: '1'