QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#469235#6885. Simple Tree Problemgrass8cowAC ✓4130ms137460kbC++172.3kb2024-07-09 16:38:142024-07-09 16:38:15

Judging History

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

  • [2024-07-09 16:38:15]
  • 评测
  • 测评结果:AC
  • 用时:4130ms
  • 内存:137460kb
  • [2024-07-09 16:38:14]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pi pair<int,int>
#define fi first
#define se second
struct SGT{
	int mx[4010000],wh[1010000],n;
	inline void build(int p,int l,int r){
		if(p==1)n=r;
		mx[p]=0;if(l==r){wh[l]=p;return;}
		int mi=(l+r)>>1;
		build(p<<1,l,mi),build(p<<1|1,mi+1,r);
	}
	inline void up(int x,int z){
		int p=wh[x];mx[p]+=z;
		while(p>1)
			p>>=1,mx[p]=max(mx[p<<1],mx[p<<1|1]);
	}
	inline int ask(int z){
		if(mx[1]<z)return 0;
		int p=1,l=1,r=n;
		while(l<r){
			int mi=(l+r)>>1;
			if(mx[p<<1|1]>=z)p=(p<<1)|1,l=mi+1;
			else p=(p<<1),r=mi;
		}
		return l;
	}
}T1,T2;
const int N=1e6+10;
int sz[N],dfn[N],son[N],db[N],fa[N],bel[N],fw[N];
vector<pi>g[N];
void dfs(int x){
	sz[x]=1;int mm=-1;
	for(pi e:g[x]){
		int v=e.fi,w=e.se;if(fa[x]==v)continue;
		fa[v]=x,fw[v]=w,dfs(v),sz[x]+=sz[v];
		if(mm<sz[v])son[x]=v,mm=sz[v];
	}
}
void dfs2(int x){
	bel[dfn[x]=++dfn[0]]=x;if(!son[x]){db[x]=x;return;}
	dfs2(son[x]),db[x]=db[son[x]];
	for(pi e:g[x]){
		int v=e.fi;
		if(v!=fa[x]&&v!=son[x])dfs2(v);
	}
}
int a[N],qc[N],cn,ans[N],w_[N],n;
void sol(){
	scanf("%d",&n);
	dfn[0]=cn=0;
	for(int i=1;i<=n;i++)fa[i]=son[i]=0,scanf("%d",&a[i]),qc[++cn]=a[i],g[i].clear();
	if(n==1)return;
	sort(qc+1,qc+cn+1);cn=unique(qc+1,qc+cn+1)-qc-1;
	T1.build(1,1,cn),T2.build(1,1,cn);
	for(int i=1;i<=n;i++)a[i]=lower_bound(qc+1,qc+cn+1,a[i])-qc,
	T1.up(a[i],1);
	for(int i=1,u,v;i<n;i++)
	scanf("%d%d%d",&u,&v,&w_[i]),g[u].pb({v,i}),g[v].pb({u,i});
	dfs(1),dfs2(1);
	for(int top=1;top<=n;top++)if(top==1||son[fa[top]]!=top){
		for(int i=dfn[db[top]];i>=dfn[top];i--){
			if(i==1)break;
			int u=bel[i];
			T1.up(a[u],-1),T2.up(a[u],1);
			if(son[u]){
				int v=son[u];
				for(int j=dfn[v]+sz[v];j<dfn[u]+sz[u];j++){
					int z=bel[j];
					T1.up(a[z],-1),T2.up(a[z],1);
				}
			}
			ans[fw[u]]=qc[max(T1.ask(w_[fw[u]]),T2.ask(w_[fw[u]]))];
		}
		for(int i=dfn[db[top]];i>=dfn[top];i--){
			if(i==1)break;
			int u=bel[i];
			T1.up(a[u],1),T2.up(a[u],-1);
			if(son[u]){
				int v=son[u];
				for(int j=dfn[v]+sz[v];j<dfn[u]+sz[u];j++){
					int z=bel[j];
					T1.up(a[z],1),T2.up(a[z],-1);
				}
			}
		}
	}
	for(int i=1;i<n;i++)printf("%d\n",ans[i]);
}
int main(){
    int T;scanf("%d",&T);while(T--)sol();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4130ms
memory: 137460kb

input:

10000
96
378804736 378804736 101171470 683875564 378804736 997225055 448759149 683875564 683875564 997225055 152015654 83284224 229458933 101171470 229458933 448759149 448759149 152015654 101171470 600214219 378804736 997225055 448759149 152015654 229458933 229458933 83284224 997225055 229458933 600...

output:

997225055
997225055
0
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
997225055
99722505...

result:

ok 2494877 lines