QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277488#7513. Palindromic Beadsship2077WA 4ms19496kbC++143.2kb2023-12-06 19:24:202023-12-06 19:24:22

Judging History

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

  • [2024-03-27 16:34:54]
  • hack成功,自动添加数据
  • (/hack/584)
  • [2024-03-27 16:18:45]
  • hack成功,自动添加数据
  • (/hack/583)
  • [2023-12-06 19:24:22]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:19496kb
  • [2023-12-06 19:24:20]
  • 提交

answer

#include<bits/stdc++.h>
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define tup tuple<int,int,int>
using namespace std;
constexpr int M=2e5+5;
int n,x,y,ans,Index;
vector<int>g[M],pos[M];vector<tup>vec;
int bg[M],ed[M],lg[M],dep[M],st[M][20],rt[M<<2];
namespace Treap{
mt19937 mt(time(NULL));int num;
struct SegTree{int ls,rs,siz,rnd,val,mx,Mx;}tr[M<<6];
int new_node(int x,int y){
	tr[++num].siz=1;tr[num].rnd=mt();
	tr[num].val=x;tr[num].mx=tr[num].Mx=y;return num;
}
void pushup(int x){
	tr[x].siz=tr[tr[x].ls].siz+tr[tr[x].rs].siz+1;
	tr[x].mx=max({tr[tr[x].ls].mx,tr[tr[x].rs].mx,tr[x].Mx});
}
void split(int now,int k,int &x,int &y){
	if (!now) return x=y=0,void();
	if (tr[now].val<=k) x=now,split(tr[now].rs,k,tr[now].rs,y);
	else  y=now,split(tr[now].ls,k,x,tr[now].ls); pushup(now);
}
int merg(int x,int y){
	if (!x||!y) return x+y;
	if (tr[x].rnd<tr[y].rnd){
		tr[x].rs=merg(tr[x].rs,y);
		pushup(x); return x;	
	}
	tr[y].ls=merg(x,tr[y].ls);
	pushup(y); return y;
}
void insert(int &rt,int k,int c){
	int x,y; split(rt,k,x,y);
	rt=merg(merg(x,new_node(k,c)),y);
}
int query(int &rt,int l,int r){
	int x,y,z;
	split(rt,r,x,z);
	split(x,l-1,x,y);
	const int tmp=tr[y].mx;
	rt=merg(merg(x,y),z);
	return tmp;
}
};
int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)) x=x*10+ch-48,ch=getchar();
	return x*f;
}
bool check(int x,int y){return bg[x]<=bg[y]&&bg[y]<=ed[x];}
int cmp(int x,int y){return bg[x]<bg[y]?x:y;}
int lca(int x,int y){
	if (x==y) return x;if ((x=bg[x])>(y=bg[y])) swap(x,y);
	int k=lg[y-x++];return cmp(st[x][k],st[y+1-(1<<k)][k]);
}
int dist(int x,int y){return dep[x]+dep[y]-dep[lca(x,y)]*2;}
void dfs(int x,int f){
	st[bg[x]=++Index][0]=f;dep[x]=dep[f]+1;
	for (auto v:g[x]) if (v^f) dfs(v,x); ed[x]=Index;
}
void update(int x_,int y_,int c_,int l=1,int r=n,int x=1){
	Treap::insert(rt[x],y_,c_);
	if (l==r) return ;int mid=l+r>>1;
	if (x_<=mid) update(x_,y_,c_,l,mid,ls(x));
	else update(x_,y_,c_,mid+1,r,rs(x));	
}
int query(int L,int R,int ql,int qr,int l=1,int r=n,int x=1){
	if (L<=l&&r<=R) return Treap::query(rt[x],ql,qr);
	int mid=l+r>>1,ans=0;
	if (L<=mid) ans=max(ans,query(L,R,ql,qr,l,mid,ls(x)));
	if (R>mid) ans=max(ans,query(L,R,ql,qr,mid+1,r,rs(x)));
	return ans;
}
int main(){ n=read();
	for (int i=1;i<=n;i++) pos[read()].push_back(i);
	for (int i=1;i<n;i++){
		x=read();y=read();
		g[x].push_back(y);
		g[y].push_back(x);
	} dfs(1,0);ans=1;
	for (int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
	for (int j=1;j<=lg[n];j++)
		for (int i=1;i+(1<<j)-1<=n;i++)
			st[i][j]=cmp(st[i][j-1],st[i+(1<<j-1)][j-1]);
	for (int i=1;i<=n;i++)
		if (pos[i].size()==2)
			vec.push_back({pos[i][0],pos[i][1],dist(pos[i][0],pos[i][1])});
	sort(vec.begin(),vec.end(),[](tup a,tup b){return get<2>(a)>get<2>(b);});
	for (auto [x,y,dist]:vec){
		int res=0,p;
		if (check(x,y)){
			for (auto v:g[x])
				if (dep[v]>dep[x]&&check(v,y))
					{p=v;break;}
			res=query(1,bg[p]-1,bg[y],ed[y])+2;
			if (bg[p]<n) res=max(res,query(bg[y],ed[y],bg[p]+1,n)+2);
		}
		else res=query(bg[x],ed[x],bg[y],ed[y])+2;
		ans=max(ans,res+(dist>1)); update(bg[x],bg[y],res);
	}
	printf("%d\n",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 19496kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

4

result:

wrong answer 1st lines differ - expected: '3', found: '4'