QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431603#8235. Top Clusterfzj2007WA 2001ms178524kbC++143.0kb2024-06-05 19:45:402024-06-05 19:45:41

Judging History

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

  • [2024-06-05 19:45:41]
  • 评测
  • 测评结果:WA
  • 用时:2001ms
  • 内存:178524kb
  • [2024-06-05 19:45:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline void read(T &x){
	x=0;
	bool flag=0;
	char ch=getchar();
	while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	x=flag?-x:x;
}
template<typename T,typename ...Args>inline void read(T &x,Args &...args){
	read(x),read(args...);
}
template<typename T>inline void prt(T x){
	if(x>9) prt(x/10);
	putchar(x%10+'0');
}
template<typename T>inline void put(T x){
	if(x<0) putchar('-'),x=-x;
	prt(x);
}
template<typename T>inline void put(char ch,T x){
	put(x),putchar(ch);
}
template<typename T,typename ...Args>inline void put(char ch,T x,Args ...args){
	put(ch,x),put(ch,args...);
}
#define N 1000005
#define inf 0x3f3f3f3f
#define ll long long
#define info vector<pair<ll,int>>
int n,m,q,num[N],ans[N];
vector<pair<int,int>> g[N];
info qry[N];
struct edge{
	int v,nxt,w;
}e[N<<1];
int head[N],cnt=1;
inline void link(int u,int v,int w){
	e[++cnt]=(edge){v,head[u],w},head[u]=cnt;
	e[++cnt]=(edge){u,head[v],w},head[v]=cnt;
}
inline void predfs(int x,int fa){
	int pre=x;
	for(auto tmp:g[x]){
		int v=tmp.first,w=tmp.second;
		if(v==fa) continue;
		link(pre,++m,0),link(m,v,w),pre=m;
		predfs(v,x);
	}
}
bool vis[N];
int siz[N],dis,rt;
inline void get_wc(int x,int fa,int tot){
	siz[x]=1;
	for(int i=head[x];i;i=e[i].nxt){
		int v=e[i].v;
		if(v==fa||vis[i>>1]) continue;
		get_wc(v,x,tot),siz[x]+=siz[v];
		if(dis>max(siz[v],tot-siz[v]))
			dis=max(siz[v],tot-siz[v]),rt=i; 
	}
}
inline void get_dist(int x,int fa,info &val,info &dat,ll dis,int r){
	if(x<=n) val.emplace_back(dis,num[x]);
	for(auto tmp:qry[x]) 
		if(tmp.first>=dis+r) dat.emplace_back(tmp.first-dis-r,tmp.second);
	for(int i=head[x];i;i=e[i].nxt)
		if(e[i].v!=fa&&!vis[i>>1]) get_dist(e[i].v,x,val,dat,dis+e[i].w,r);
}
inline void update(info val,info dat){
	if(!dat.size()) return;
	sort(val.begin(),val.end(),[](const pair<ll,int> &a,const pair<ll,int> &b){return a.first>b.first;});
	sort(dat.begin(),dat.end(),[](const pair<ll,int> &a,const pair<ll,int> &b){return a.first>b.first;});
	int j=0,res=inf;
	for(auto tmp:dat){
		while(j<val.size()&&val[j].first>tmp.first)
			res=min(res,val[j].second),j++;
		ans[tmp.second]=min(ans[tmp.second],res);
	}
}
inline void devide(int x,int tot){
	if(tot==1) return;
	dis=1e9,get_wc(x,0,tot);
	int a=e[rt].v,b=e[rt^1].v;
	vis[rt>>1]=1;
	info A,B,qA,qB;
	get_dist(a,0,A,qA,0,e[rt].w),get_dist(b,0,B,qB,0,e[rt].w);
	update(A,qB),update(B,qA);
	int sa=siz[a],sb=tot-siz[a];
	devide(a,sa),devide(b,sb);
}
int ton[N],mn;
int main(){
	read(n,q),m=n;
	for(int i=1;i<=n;i++){
		read(num[i]);
		if(num[i]<=n) ton[num[i]]=1;
	}
	while(ton[mn]) mn++;
	for(int i=1,u,v,w;i<n;i++)
		read(u,v,w),g[u].emplace_back(v,w),g[v].emplace_back(u,w);
	predfs(1,0);
	for(int i=1;i<=q;i++){
		ll x,k;read(x,k),ans[i]=mn;
		qry[x].emplace_back(k,i);
	}
	devide(1,m);
	for(int i=1;i<=q;i++) put('\n',ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 58816kb

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: 2001ms
memory: 178524kb

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:

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

result:

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