QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#620860#8235. Top ClusterSoestxWA 2611ms118672kbC++232.8kb2024-10-07 21:52:082024-10-07 21:52:09

Judging History

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

  • [2024-10-07 21:52:09]
  • 评测
  • 测评结果:WA
  • 用时:2611ms
  • 内存:118672kb
  • [2024-10-07 21:52:08]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define fi first
#define se second
#define pll pair<int,int>
#define lowbit(x) (x&(-x))
typedef long long ll;
//typedef unsigned long long ull;
const int N = 1e6 + 10, M = 5e5, mod = 1e9+7;
const double eps=1e-11;
int n, m, k;
int res,ans;
int num[N],mp[N];


namespace Gra
{
	vector<pll> vt[N];
	int fa[N][30],dep[N],dp[N];
	
	void dfs(int id,int f)
	{
		for(int i=1;(1<<i)<dep[id];i++)
		{
			fa[id][i]=fa[fa[id][i-1]][i-1];
		}
		for(auto it:vt[id])
		{
			int j=it.fi;
			if(j==f) continue;
			fa[j][0]=id;
			dep[j]=dep[id]+1;
			dp[j]=dp[id]+it.se;
			dfs(j,id);
		}
	}
	
	int lca(int a,int b)
	{
		if(dep[a]<dep[b]) swap(a,b);
		if(dep[a]!=dep[b])
		{
			int k=log2(dep[a]-dep[b]);
			for(int i=k;i>=0;i--)
			{
				if(dep[fa[a][i]]>=dep[b]) a=fa[a][i];
			}
		}
		if(a==b) return a;
		int k=log2(dep[a]);
		for(int i=k;i>=0;i--)
		{
			if(fa[a][i]==fa[b][i]) continue;
			a=fa[a][i];
			b=fa[b][i];
		}
		return fa[a][0];
	}
	
	void run()
	{
		fa[1][0]=1;
		dep[1]=1;
		dfs(1,-1);
	}
	
	int get(int a,int b)
	{
		if(dep[a]<dep[b]) swap(a,b);
		int d=lca(a,b);
		if(b==d) return dp[a]-dp[d];
		return dp[a]+dp[b]-2*dp[d];
	}
}
int L[N],R[N],di[N];

bool check(int mid,int x,int k)
{
	if(mid==0) return 1;
	int dl=Gra::get(x,L[mid]),dr=Gra::get(x,R[mid]);
	//cout<<mid<<" "<<x<<" "<<k<<" "<<L[mid]<<" "<<R[mid]<<" "<<dl<<" "<<dr<<endl;
	return (dl<=k&&dr<=k);
}



void solve() {
	scanf("%d %d",&n,&m);
	vector<pll> vt;
	vt.push_back({-1,-1});
	int lim=m;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&num[i]);
		vt.push_back({num[i],i});
	}
	sort(vt.begin(),vt.end());
	for(int i=1;i<n;i++)
	{
		int u,v,w;
		scanf("%d %d %d",&u,&v,&w);
		Gra::vt[u].push_back({v,w});
		Gra::vt[v].push_back({u,w});
	}
	Gra::run();
	
	for(int i=1;i<=m;i++)
	{
		if(vt[i].first!=vt[i-1].first+1)
		{
			lim=i-1;
			break;
		}
		
		if(i==1)
		{
			L[i]=R[i]=vt[i].se;
			continue;
		}
		
		int u=L[i-1],v=R[i-1];
		int dis=di[i-1];
		int dl=Gra::get(u,vt[i].se),dr=Gra::get(v,vt[i].se);
		//cout<<u<<" "<<v<<" "<<dis<<" "<<dl<<" "<<dr<<" "<<vt[i].se<<endl;
		if(dl>=dr&&dl>dis) L[i]=L[i-1],R[i]=vt[i].se,di[i]=dl;
		else if(dr>=dl&&dr>dis) L[i]=vt[i].se,R[i]=R[i-1],di[i]=dr;
		else L[i]=L[i-1],R[i]=R[i-1],di[i]=di[i-1];
	}
	//cout<<lim<<"---"<<endl;
	//for(int i=1;i<=lim;i++) cout<<i<<" "<<vt[i].fi<<" "<<L[i]<<" "<<R[i]<<endl;
	
	while(m--)
	{
		int x,k;
		scanf("%d %d",&x,&k);
		int l=0,r=lim;
		while(l<r)
		{
			int mid=(l+r+1)>>1;
			if(check(mid,x,k)) l=mid;
			else r=mid-1;
		}
		printf("%d\n",vt[l].fi+1);
	}

}

signed main() {
//	ios::sync_with_stdio(0);
//	cin.tie(0);
//	cout.tie(0);
	int T;
	T = 1;
	//cin >> T;
	while (T--) solve();
	return 0;
}
/*
*/

详细

Test #1:

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

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: 2250ms
memory: 115204kb

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: 2611ms
memory: 118672kb

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:

59
0
250002
100311
94581
250002
250002
100311
59
0
0
94581
100311
250002
59
0
59
0
59
0
0
250002
250002
100311
100311
250002
250002
0
250002
0
0
94581
59
250002
0
250002
0
59
250002
0
0
250002
0
59
1018
250002
250002
4
59
250002
100311
94581
250002
250002
250002
250002
0
0
100311
0
0
4
611
250002
25...

result:

wrong answer 1st numbers differ - expected: '0', found: '59'