QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#670330 | #8235. Top Cluster | frankly6 | WA | 486ms | 74440kb | C++17 | 2.9kb | 2024-10-23 21:20:13 | 2024-10-23 21:20:14 |
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];
if(dis(lp[i],rp[i])<dis(pt[i].p,rp[i])) lp[i]=pt[i].p;
if(dis(lp[i],rp[i])<dis(pt[i].p,lp[i])) rp[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, 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);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 19964kb
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: -100
Wrong Answer
time: 486ms
memory: 74440kb
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 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 250000 2500...
result:
wrong answer 2nd numbers differ - expected: '249999', found: '250000'