QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#780818#8235. Top Clusterlouhao088WA 358ms89512kbC++982.2kb2024-11-25 13:26:482024-11-25 13:26:48

Judging History

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

  • [2024-11-25 13:26:48]
  • 评测
  • 测评结果:WA
  • 用时:358ms
  • 内存:89512kb
  • [2024-11-25 13:26:48]
  • 提交

answer

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

typedef long long ll;

const int MAXN=5e5,LOGN=18;

inline ll Read()
{
	ll res;char c;
	while(1) {c=getchar();if('0'<=c && c<='9') {res=c-'0';break;}}
	while(1) {c=getchar();if('0'<=c && c<='9') res=res*10+c-'0';else break;}
	return res;
}

int n,q,A[MAXN+5];
int Q[MAXN+5],ALL,pre[MAXN+5][2];
vector< pair<int,int> > Tree[MAXN+5];
int ans[MAXN+5];

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

int fa[MAXN+5],DFN[MAXN+5],tot;
ll dis[MAXN+5];
void CalMsg(int now)
{
	DFN[now]=++tot;
	for(int i=0,rear;i<Tree[now].size();i++)
	{
		rear=Tree[now][i].first;
		if(rear==fa[now]) continue;
		fa[rear]=now;
		dis[rear]=dis[now]+Tree[now][i].second;
		CalMsg(rear);
	}
}
int lg[MAXN+5],ST[LOGN+1][MAXN+5]; 
inline void Build()
{
	for(int i=2;i<=n;i++) lg[i]=lg[i-1]+((1<<(lg[i-1]+1))<=i);
	for(int i=1;i<=n;i++) ST[0][DFN[i]]=i;
	for(int i=1;(1<<i)<=n;i++)
		for(int j=1;j+(1<<i)-1<=n;j++)
		{
			int a=ST[i-1][j],b=ST[i-1][j+(1<<(i-1))];
			ST[i][j]=(dis[a]<dis[b] ? a : b);
		}
}
inline int LCA(int a,int b)
{
	if(a==b) return a;
	a=DFN[a],b=DFN[b];
	if(a>b) swap(a,b);
	int w=lg[b-a],x=ST[w][a+1],y=ST[w][b-(1<<lg[b-a])+1];
	if(dis[x]<dis[y]) return fa[x];
	return fa[y];
}
inline ll GetDis(int a,int b) {return dis[a]+dis[b]-2*dis[LCA(a,b)];}

int main()
{
	n=Read(),q=Read();
	for(int i=1;i<=n;i++) A[i]=Read(),Q[i]=i;
	sort(Q+1,Q+n+1,cmp);
	for(int i=1;i<=n;i++)
		if(A[Q[i]]==ALL) ++ALL;
		else break;
	for(int i=1,a,b,c;i<n;i++)
	{
		a=Read(),b=Read(),c=Read();
		Tree[a].push_back(make_pair(b,c));
		Tree[b].push_back(make_pair(a,c));
	}
	CalMsg(1),Build();
	pre[1][0]=pre[1][1]=Q[1];
	for(int i=2;i<=n;i++)
	{
		pre[i][0]=pre[i-1][0],pre[i][1]=pre[i-1][1];
		if(GetDis(Q[i],pre[i-1][1])>GetDis(pre[i][0],pre[i][1]))
			pre[i][0]=Q[i],pre[i][1]=pre[i-1][1];
		if(GetDis(pre[i-1][0],Q[i])>GetDis(pre[i][0],pre[i][1]))
			pre[i][0]=pre[i-1][0],pre[i][1]=Q[i];
	}
	ll K;
	for(int X;q--;)
	{
		X=Read(),K=Read();
		int L=1,R=n;
		for(int mid;L<=R;)
		{
			mid=(L+R)>>1;
			if(GetDis(X,pre[mid][0])>K || GetDis(X,pre[mid][1])>K) R=mid-1;
			else L=mid+1;
		}
		printf("%d\n",min(ALL,A[Q[L]]));
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 358ms
memory: 89512kb

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
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 2nd numbers differ - expected: '249999', found: '0'