QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#414769#8235. Top Clusterushg8877TL 2996ms92356kbC++142.5kb2024-05-19 17:10:362024-05-19 17:10:37

Judging History

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

  • [2024-05-19 17:10:37]
  • 评测
  • 测评结果:TL
  • 用时:2996ms
  • 内存:92356kb
  • [2024-05-19 17:10:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MP make_pair
mt19937 rnd(time(0));
const int MAXN=5e5+5;
int n,m,q,a[MAXN],tsiz,mson;
int eu[MAXN],ev[MAXN],ew[MAXN],siz[MAXN],maxx[MAXN];
int col[MAXN],ans[MAXN];
ll dis[MAXN];bool vis[MAXN],isw[MAXN];
struct query{ll id,d;};
struct info{int op,fr;ll d;};
vector<query> vec[MAXN];
vector<int> edg[MAXN];
info f[MAXN<<1];
void get_size(int u,int fa){
	siz[u]=maxx[u]=1;
	for(int id:edg[u]){
		int v=eu[id]^ev[id]^u;
		if(v==fa||vis[v]) continue;
		get_size(v,u);
		maxx[u]=max(maxx[u],siz[v]);
		siz[u]+=siz[v];
	}
	maxx[u]=max(maxx[u],tsiz-siz[u]);
	if(maxx[u]<maxx[mson]) mson=u;
}
void get_col(int u,int fa){
	for(int id:edg[u]){
		int v=eu[id]^ev[id]^u;
		if(v==fa||vis[v]) continue;
		col[v]=col[u];dis[v]=dis[u]+ew[id];
		get_col(v,u);
	}
	return;
}
void get_info(int u,int fa){
	f[++m]=(info){0,u,dis[u]};
	for(auto q:vec[u]) f[++m]=(info){col[u],q.id,q.d-dis[u]};
	for(int id:edg[u]){
		int v=eu[id]^ev[id]^u;
		if(v==fa||vis[v]) continue;
		get_info(v,u);
	}
}
void dfz(int u){
	// cout<<"Dfz at "<<u<<'\n';
	vis[u]=true;col[u]=u;dis[u]=0;
	for(int id:edg[u]){
		int v=eu[id]^ev[id]^u;
		if(vis[v]) continue;
		col[v]=v;dis[v]=ew[id];
		get_col(v,u);
	}
	m=0;
	get_info(u,0);
	sort(f+1,f+m+1,[&](info x,info y){
		if(x.d!=y.d) return x.d>y.d;
		return x.op>y.op;
	});
	int mn1=1e9,fr1=0,mn2=1e9,fr2=0;
	for(int i=1;i<=m;i++){
		if(f[i].op==0){
			if(mn1>a[f[i].fr]){
				if(col[f[i].fr]!=col[fr1]) mn2=mn1,fr2=fr1;
				mn1=a[f[i].fr];fr1=col[f[i].fr];
			}else if(col[f[i].fr]!=col[fr1]&&mn2>a[f[i].fr]){
				mn2=a[f[i].fr];fr2=col[f[i].fr];
			}
		}else{
			if(fr1!=f[i].op) ans[f[i].fr]=min(ans[f[i].fr],mn1);
			else ans[f[i].fr]=min(ans[f[i].fr],mn2);
		}
	}
	for(int id:edg[u]){
		int v=eu[id]^ev[id]^u;
		if(vis[v]) continue;
		mson=0;tsiz=siz[v];
		get_size(v,0);get_size(mson,0);dfz(mson);
	}
	return;
}
int main(){
	ios::sync_with_stdio(false);
	// freopen("Otomachi_Una.in","r",stdin);
	// freopen("Otomachi_Una.out","w",stdout);
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		if(a[i]<=n) isw[a[i]]=true;
	}
	int mn=0;
	while(isw[mn]) mn++;
	for(int i=1;i<n;i++){
		cin>>eu[i]>>ev[i]>>ew[i];
		edg[eu[i]].push_back(i);
		edg[ev[i]].push_back(i);
	}
	for(int i=1;i<=q;i++){
		ll x,k;cin>>x>>k;
		vec[x].push_back((query){i,k});
		ans[i]=mn;
	}
	maxx[0]=1e9;
	tsiz=n;
	get_size(1,0);get_size(mson,0);
	dfz(mson);
	for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
	cerr<<clock();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 28444kb

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: 2916ms
memory: 92356kb

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: 0
Accepted
time: 2996ms
memory: 92292kb

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:

ok 500000 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

500000 500000
161260 258036 999901390 280191 21975 193647 397811 362276 206096 421117 336278 416815 31870 999934957 999977591 15 999991550 235871 999916756 19 241647 466194 218880 352702 84559 479034 24903 124981 339271 153019 157132 333035 124326 464181 219609 94164 999933701 440055 118732 2285 175...

output:


result: