QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#343452#8235. Top Cluster111445#WA 685ms90296kbC++231.9kb2024-03-02 16:28:282024-03-02 16:28:29

Judging History

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

  • [2024-03-02 16:28:29]
  • 评测
  • 测评结果:WA
  • 用时:685ms
  • 内存:90296kb
  • [2024-03-02 16:28:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define N 500000

int i,j,k,n,m,t,dfn[N+50],it,pos[N+50];
vector<pair<int,int> > v[N+50];

ll dep[N+50];

struct SB{
	int i,j,k,n;
	int f[N+50][22],l2[N+50];
	int _get(int l,int r){
		return (dfn[l]<dfn[r]?l:r);
	}
	void init(int nn){
		n=nn;
		for(i=1;i<=20;i++)for(j=1;j+(1<<i)-1<=n;j++)f[j][i]=_get(f[j][i-1],f[j+(1<<(i-1))][i-1]);
	}
	int lca(int l,int r){
		if(l==r)return l;
		l=dfn[l]; r=dfn[r]; if(l>r)swap(l,r);
		l++; int k=l2[r-l+1];
		return _get(f[l][k],f[r-(1<<k)+1][k]);
	}
	ll dis(int x,int y){
		int z=lca(x,y);
		return dep[x]+dep[y]-2*dep[z];
	}
}sb;

void dfs0(int x,int fa){
	dfn[x]=++it; sb.f[it][0]=fa;
	for(auto [i,j]:v[x])if(i!=fa){
		dep[i]=dep[x]+j;
		dfs0(i,x);
	}
}

struct JB{
	int x,y; ll w;
}f[N+50];

JB hb(JB x,JB y){
	auto [x1,y1,w1]=x;
	auto [x2,y2,w2]=y;
	vector<int> v={x1,y1,x2,y2};
	int rx=0,ry=0,rw=-1,k;
	for(int i=0;i<4;i++)for(int j=i+1;j<4;j++){
		k=sb.dis(v[i],v[j]);
		if(k>rw){rw=k; rx=v[i]; ry=v[j];}
	}
	return {rx,ry,rw};
}

ll fuck(ll x,ll y){
	int res=0,l=1,r=m+1,md;
	ll t1;
	while(l<=r){
		md=(l+r)/2;
		t1=max(sb.dis(x,f[md-1].x),sb.dis(x,f[md-1].y));
		if(t1<=y){
			res=max(res,md); l=md+1;
		}
		else r=md-1;
	}
	return res;
}

int main(){
	for(i=2;i<=N;i++)sb.l2[i]=sb.l2[i/2]+1;
	ios::sync_with_stdio(0); cin.tie(0);
	cin>>n>>t;
	for(i=1;i<=n;i++){
		cin>>k;
		if(k<=n)pos[k]=i;
	}
	if(!pos[0]){
		while(t--)cout<<0<<'\n';
		return 0;
	}
	for(i=1;i<n;i++){
		int x,y,z;
		cin>>x>>y>>z;
		v[x].push_back({y,z});
		v[y].push_back({x,z});
	}
	dfs0(1,0);
	sb.init(n);
	//for(i=1;i<=n;i++)for(j=i+1;j<=n;j++){cout<<"NMSL "<<i<<' '<<j<<' '<<sb.dis(i,j)<<endl;}
	
	f[0]={pos[0],pos[0],0};
	for(i=1;i<=n;i++){
		if(!pos[i])break;
		m++;
		f[i]={pos[i],pos[i],0};
		f[i]=hb(f[i],f[i-1]);
	}
	while(t--){
		ll x,y;
		cin>>x>>y;
		cout<<fuck(x,y)<<'\n';
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 7048kb

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: 671ms
memory: 90164kb

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: 685ms
memory: 90296kb

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: '57926'