/* Things to notice: 1. do not calculate useless values 2. do not use similar names Things to check: 1. submit the correct file 2. time (it is log^2 or log) 3. memory 4. prove your naive thoughts 5. long long 6. corner case like n=0,1,inf or n=m 7. check if there is a mistake in the ds or other tools you use 8. fileio in some oi-contest 9. module on time 10. the number of a same divisor in a math problem 11. multi-information and queries for dp and ds problems */ #include using namespace std; #define int long long #define fi first #define se second #define pii pair #define mp make_pair #define pb push_back const int mod=998244353; const int inf=0x3f3f3f3f; const int INF=1e18; vector g[200005]; struct LCA { int dep[200005],dfn[200005],times; int dist[200005]; int st[20][400005],Lg[400005]; void dfs(int u,int fa) { dfn[u]=++times,st[0][times]=u; for(int i=0;iv) swap(u,v); int s=Lg[v-u+1]; return mindep(st[s][u],st[s][v-(1<>n; for(int i=1;i<=n;i++) cin>>a[i],b[i]=mp(a[i],i); sort(b+1,b+1+n),reverse(b+1,b+1+n); for(int i=1;i>u>>v; g[u].pb(mp(v,1)),g[v].pb(mp(u,1)); } Lca.build(); pii diam=mp(-1,-1); for(int i=1;i<=n;i++) { int j=i; while(j+1<=n&&b[j+1].fi==b[j].fi) j++; for(int l=i;l<=j;l++) { int u=b[l].se; if(diam==mp(-1LL,-1LL)) diam=mp(u,u); else { int x=diam.fi,y=diam.se; if(Lca.getdis(u,x)>=Lca.getdis(x,y)&&Lca.getdis(u,x)>=Lca.getdis(u,y)) diam=mp(u,x); else if(Lca.getdis(u,y)>=Lca.getdis(x,y)&&Lca.getdis(u,y)>=Lca.getdis(u,x)) diam=mp(u,y); else diam=mp(x,y); } } for(int l=i;l<=j;l++) { int u=b[l].se; ans[u]=max(Lca.getdis(u,diam.fi),Lca.getdis(u,diam.se)); } i=j; } for(int i=1;i<=n;i++) cout<>_; while(_--) solve(); return 0; }