QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#277487#7513. Palindromic Beadsship2077WA 589ms94092kbC++143.2kb2023-12-06 19:23:172023-12-06 19:23:19

Judging History

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

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

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 (ed[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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 19080kb

input:

4
1 1 2 2
1 2
2 3
2 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 20128kb

input:

5
1 3 2 2 1
1 2
2 3
3 4
4 5

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 19764kb

input:

6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6

output:

2

result:

ok single line: '2'

Test #4:

score: 0
Accepted
time: 0ms
memory: 19244kb

input:

6
1 2 3 4 5 6
1 2
2 3
3 4
4 5
5 6

output:

1

result:

ok single line: '1'

Test #5:

score: 0
Accepted
time: 3ms
memory: 18996kb

input:

2000
845 1171 345 282 1181 625 754 289 681 493 423 840 1494 318 266 1267 967 379 135 14 39 191 60 972 116 1216 1205 19 194 185 1360 861 379 430 1262 1151 756 65 389 488 277 53 1283 1438 101 1465 195 714 737 980 80 298 961 1326 163 1163 1317 1152 992 35 334 802 1502 486 710 234 555 88 1278 146 46 696...

output:

5

result:

ok single line: '5'

Test #6:

score: 0
Accepted
time: 566ms
memory: 94092kb

input:

200000
48015 47923 20609 71806 43752 68214 95683 89449 25809 58110 19878 52931 7845 45206 86245 82945 62977 37876 12456 105915 10509 92943 66950 88545 26442 26545 42278 66977 3970 9631 21524 43638 7979 58240 25719 56260 276 89721 9553 16550 52161 30307 82748 108443 36676 48581 59069 57412 62453 7965...

output:

5

result:

ok single line: '5'

Test #7:

score: 0
Accepted
time: 542ms
memory: 93764kb

input:

200000
13011 51198 65374 107045 66506 14385 35784 94265 71449 41817 24646 60714 53382 68358 9354 840 3139 71282 72215 69550 2121 41498 13675 76444 67690 40513 56439 12832 51976 35333 47208 59602 98993 9383 77866 10464 41517 89125 58804 91741 66160 74208 70991 63865 84870 14282 2441 78046 73372 36311...

output:

7

result:

ok single line: '7'

Test #8:

score: -100
Wrong Answer
time: 589ms
memory: 93792kb

input:

200000
38715 33241 65919 39407 27500 36200 2259 42301 79147 57505 20 81399 69499 23658 14534 86934 14352 69558 59763 43318 35360 3281 38188 40058 40571 103709 75625 8434 53802 87159 98628 69421 53711 47986 18350 6079 37362 39377 71936 89573 25983 66882 48999 58918 66432 17453 82515 9588 95375 87287 ...

output:

39

result:

wrong answer 1st lines differ - expected: '45', found: '39'