QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#670353 | #8235. Top Cluster | frankly6 | WA | 0ms | 20000kb | C++17 | 2.9kb | 2024-10-23 21:25:08 | 2024-10-23 21:25:09 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
typedef long double ld;
typedef long long ll;
const int MX=500010;
int N, Q;
int w[MX];
int head[MX], cnt=0;
int son[MX], fa[MX], top[MX], dep[MX], siz[MX], pat[MX];
int lp[MX], rp[MX];
bool ext[MX];
int read()
{
int r=0, f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {r=r*10+ch-'0'; ch=getchar();}
return r*f;
}
struct edge{int nxt, to, c;}e[2*MX];
inline void ae(int u, int v, int c) {e[++cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt; e[cnt].c=c;}
struct node
{
int p, c;
bool operator < (const node &a)const {return c<a.c;}
}pt[MX];
void dfs1(int x, int f, int D)
{
fa[x]=f; dep[x]=D; siz[x]=1;
for(int i=head[x];i;i=e[i].nxt)
{
int y=e[i].to;
if(y==f) continue;
pat[y]=pat[x]+e[i].c;
dfs1(y,x,D+1);
siz[x]+=siz[y];
if(siz[y]>siz[son[x]]) son[x]=y;
}
}
void dfs2(int x, int topf)
{
top[x]=topf;
if(!son[x]) return;
dfs2(son[x],topf);
for(int i=head[x];i;i=e[i].nxt)
{
int y=e[i].to;
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
inline int lca(int x, int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=fa[top[x]];
}
if(dep[x]>dep[y]) swap(x,y); return x;
}
int dis(int x, int y)
{
int l=lca(x,y);
return pat[x]+pat[y]-2*pat[l];
}
signed main()
{
// freopen("testdata.in","r",stdin);
N=read(); Q=read();
for(int i=1;i<=N;i++)
{
w[i]=read();
pt[i]={i,w[i]};
if(w[i]<=5e5+5) ext[w[i]]=1;
}
sort(pt+1,pt+1+N);
for(int i=1;i<N;i++)
{
int u=read(), v=read(), c=read();
ae(u,v,c); ae(v,u,c);
}
dfs1(1,0,1);
dfs2(1,1);
lp[1]=rp[1]=pt[1].p;
for(int i=2;i<=N;i++)
{
lp[i]=lp[i-1]; rp[i]=rp[i-1];
int d=dis(lp[i],rp[i]);
int d1=dis(pt[i].p,rp[i]);
int d2=dis(pt[i].p,lp[i]);
if(d2>d1&&d2>d) rp[i]=pt[i].p;
else if(d1>d2&&d1>d) lp[i]=pt[i].p;
}
// cout << pat[4] << " " << pat[5] << '\n';
// cout << "dis (1,5)=" << dis(1,5) << ", dis(4,5)=" << dis(4,5) << '\n';
// for(int i=1;i<=N;i++) cout << "p=" << pt[i].p << ", lp=" << lp[i] << ", rp=" << rp[i] << '\n';
int lim=MX;
for(int i=0;i<=5e5+5;i++)
if(!ext[i]) {lim=i; break;}
while(Q--)
{
int x=read(), k=read();
int l=0, r=lim-1, ans=0;
while(l<=r)
{
int mid=(l+r)>>1; //w[i]<=mid full covered
// cout << "mid=" << mid << ", lp=" << lp[mid+1] << ", rp=" << rp[mid+1] << '\n';
if(dis(lp[mid+1],x)<=k&&dis(rp[mid+1],x)<=k) ans=mid+1, l=mid+1;
else r=mid-1;
}
cout << ans << '\n';
}
return (0-0);
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 20000kb
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:
4 0 4 4
result:
wrong answer 1st numbers differ - expected: '1', found: '4'