QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#751771#8235. Top ClusterhotdogsellerTL 0ms22264kbC++142.8kb2024-11-15 20:38:242024-11-15 20:38:25

Judging History

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

  • [2024-11-15 20:38:25]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:22264kb
  • [2024-11-15 20:38:24]
  • 提交

answer

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")

#include<bits/stdc++.h>

#define INF 1e18
#define int long long
#define mod 1000000007
#define maxn 500005

using namespace std;

inline int read(){
	int lre=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-'){
			f=-1;
		}
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		lre=(lre<<3)+(lre<<1)+(c-'0');
		c=getchar();
	}
	return lre*f;
} 

struct edge{
	int to;
	int val;
	int next;
}e[2*maxn];

int e_cnt=1,head[maxn];

void add(int u,int v,int w){
	e[e_cnt].to=v;
	e[e_cnt].val=w;
	e[e_cnt].next=head[u];
	head[u]=e_cnt++;
}

int n,q,up;
int arr[maxn],depth[maxn],cnt[maxn];
vector<int> v;

//len[i]:包含到权值(i-1)的树直径 
int ind[maxn],len[maxn];
pair<int,int> nd[maxn];

int f[maxn],root[maxn],son[maxn],sz[maxn];

bool cmp(int a,int b){
	return arr[a]<arr[b];
}

void dfs1(int x,int fa){
	cnt[x]=cnt[fa]+1;
	sz[x]=1;f[x]=fa;
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==f[x])continue;
		depth[v]=depth[x]+e[i].val;
		dfs1(v,x);
		sz[x]+=sz[v];
		if(sz[v]>sz[son[x]])son[x]=v;
	}
}

void dfs2(int x,int rt){
	root[x]=rt;
	if(son[x]){
		dfs2(son[x],rt);
	}
	for(int i=head[x];i;i=e[i].next){
		int v=e[i].to;
		if(v==f[x])continue;
		dfs2(v,v);
	}
}

int lca(int u,int v){
	while(root[u]!=root[v]){
		if(cnt[root[u]]<cnt[root[v]])swap(u,v);
		u=f[root[u]];
	}
	if(cnt[u]>cnt[v])return v;
	return u;
}

int dist(int u,int v){
//	cout<<"lca("<<u<<","<<v<<")="<<lca(u,v)<<endl;
	return depth[u]+depth[v]-2*depth[lca(u,v)];
}

void init(){
	n=read();q=read();
	for(int i=1;i<=n;i++){
		arr[i]=read();ind[i]=i;
	}
	sort(ind+1,ind+n+1,cmp);
	for(int i=1;i<n;i++){
		int u=read(),v=read(),w=read();
		add(u,v,w);add(v,u,w);
	} 
	up=-1;
	for(int i=1;i<=n;i++){
		if(arr[ind[i]]>up+1){
			up++;break;
		}else{
			up=arr[ind[i]];
		}
	}
	if(up==arr[ind[n]])up++;//up变成了答案上界 
}

bool okay(int x,int k,int mid){
	int d=max(dist(x,nd[mid].first),dist(x,nd[mid].second));
	return d<=k;
}

signed main(){
	
	init();dfs1(1,0);dfs2(1,1);
	nd[1].first=nd[1].second=ind[1];
	
	len[1]=0;
	for(int i=2;i<=n;i++){
		int x=ind[i];
		if(arr[x]>i-1)break;//没有考虑的必要 
		nd[i]=nd[i-1];len[i]=len[i-1];
		int d1=dist(x,nd[i-1].first),d2=dist(x,nd[i-1].second);
		if(d1>len[i-1]&&d1>d2){
			nd[i].second=x;
			len[i]=d1;
		}else if(d2>len[i-1]){
			nd[i].first=x;
			len[i]=d2;
		}
	}
	
	int x,k,l,r,mid,res;
	while(q--){
		x=read(),k=read();
		l=1,r=up,mid,res=0;
		while(l<=r){
			int mid=l+r>>1;
		//	cout<<"mid="<<mid<<endl;
			if(okay(x,k,mid)){
			//	cout<<"okay!"<<endl;
				l=mid+1;
				res=mid;
			}else{
			//	cout<<"no!"<<endl;
				r=mid-1; 
			}
		}
		//此时的res-1应该是mex-1 
		printf("%lld\n",res);
	}
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Time Limit Exceeded

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: