QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#649328#8235. Top ClusterYurilyRE 0ms14104kbC++202.2kb2024-10-17 22:49:532024-10-17 22:49:54

Judging History

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

  • [2024-10-17 22:49:54]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:14104kb
  • [2024-10-17 22:49:53]
  • 提交

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=-2;
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, f = 1;
	char c = getchar();
	while(c < '0' || c > '9'){
		if(c == '-'){
			f = -1;
		}
		c = getchar();
	}
	while(c >= '0' && c <= '9'){
		x = x*10+c-'0';
		c = getchar();
	}
	return x*f;
}

int main(){
	n=read(),q=read();
	for(int i=1;i<=n;++i){
		a[i]=read();
		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;
		}
	}
	if(lst==-2)
		lst=n;
	
	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(),d=read();
		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);
	}	
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 14104kb

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
Runtime Error

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:


result: