QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#403594#8318. 白兰地厅的西瓜Sround30 73ms22112kbC++142.2kb2024-05-02 15:22:142024-05-02 15:22:16

Judging History

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

  • [2024-05-02 15:22:16]
  • 评测
  • 测评结果:30
  • 用时:73ms
  • 内存:22112kb
  • [2024-05-02 15:22:14]
  • 提交

answer


#include <bits/stdc++.h>

using namespace std;

const int N=1e5+10,M=N<<4,V=1e5;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}
	while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return f==-1?-x:x;
}

int n,ans;
int h[N],w[N],e[N<<1],ne[N<<1],idx;
int root[N],tot;
int nums[N];
struct Node{
	int l,r;
	int s[2];
}tr[M];

inline void add(int a,int b){e[idx]=b,ne[idx]=h[a],h[a]=idx++;}

void insert(int &p,int l,int r,int pos,int v,int op)
{
	if(!p) p=++tot;
	ans=max(ans,tr[p].s[op]=max(tr[p].s[op],v));
	if(l==r) return ;
	int mid=l+r>>1;
	if(mid>=pos) insert(tr[p].l,l,mid,pos,v,op);
	else insert(tr[p].r,mid+1,r,pos,v,op);
}

int merge(int p,int q)
{
	if(!p || !q) return p|q;
	ans=max(ans,max(tr[tr[p].l].s[0]+tr[tr[q].r].s[1],tr[tr[p].r].s[1]+tr[tr[q].l].s[0]));
	tr[p].s[0]=max(tr[p].s[0],tr[q].s[0]),tr[p].s[1]=max(tr[p].s[1],tr[q].s[1]);
	tr[p].l=merge(tr[p].l,tr[q].l),tr[p].r=merge(tr[p].r,tr[q].r);
	return p;
}

int query(int p,int l,int r,int L,int R,int op)
{
	if(!p) return 0;
	if(l>=L && r<=R) return tr[p].s[op];
	int mid=l+r>>1;
	if(mid<L) return query(tr[p].r,mid+1,r,L,R,op);
	if(mid>=R) return query(tr[p].l,l,mid,L,R,op);
	return max(query(tr[p].l,l,mid,L,R,op),query(tr[p].r,mid+1,r,L,R,op));
}

void dfs(int u,int pa)
{
	int mx0=0,mx1=0;
	for(int i=h[u];i!=-1;i=ne[i]){
		int j=e[i];
		if(j==pa) continue;
		dfs(j,u);
		int t0=query(root[j],1,V,1,w[u]-1,0);
		int t1=query(root[j],1,V,w[u]+1,V,1);
		root[u]=merge(root[u],root[j]);
		ans=max(ans,max(t0+mx1,t1+mx0)+1);
		mx0=max(mx0,t0),mx1=max(mx1,t1);
		// cout<<j<<' '<<endl;
	}
	// cout<<u<<' '<<mx0<<' '<<mx1<<endl;
	insert(root[u],1,V,w[u],mx0+1,0);
	insert(root[u],1,V,w[u],mx1+1,1);
}

int main()
{
	scanf("%d",&n); int cnt=0;
	memset(h,-1,sizeof(h));
	for(int i=1;i<=n;i++) w[i]=read(),nums[++cnt]=w[i];
	sort(nums+1,nums+1+cnt),cnt=unique(nums+1,nums+1+cnt)-nums-1;
	for(int i=1;i<=n;i++) w[i]=lower_bound(nums+1,nums+1+cnt,w[i])-nums-1;
	for(int i=1,a,b;i<n;i++) a=read(),b=read(),add(a,b),add(b,a);
	dfs(1,0);
	
	printf("%d\n",ans);
	
	return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 2ms
memory: 8396kb

input:

100
153314338 599231681 858435749 723728532 852113215 378535625 215162690 923065539 951983654 189872202 340854577 762420560 247102481 58348011 381182670 225684974 727160244 17794042 176551091 74792743 815847597 25690481 708165027 806743934 878575557 892366013 619168311 110922692 910879195 115796937 ...

output:

8

result:

ok 1 number(s): "8"

Subtask #2:

score: 10
Accepted

Dependency #1:

100%
Accepted

Test #2:

score: 10
Accepted
time: 2ms
memory: 8400kb

input:

1000
78489921 969971487 825933116 372097611 4721729 316569942 977315119 632546018 270107640 166931760 987070325 74592266 79702886 208771995 223716883 428991179 886445793 820327751 528784039 362871691 328323831 491420678 946333794 428967373 177442537 569874407 792352850 453993155 880434421 654997821 ...

output:

11

result:

ok 1 number(s): "11"

Subtask #3:

score: 0
Wrong Answer

Test #3:

score: 0
Wrong Answer
time: 3ms
memory: 8800kb

input:

5000
3 4 1 4 4 1 4 3 2 2 1 2 4 1 4 2 1 2 4 2 1 4 1 3 1 3 4 4 3 2 4 1 1 1 1 1 1 4 3 2 1 3 3 4 3 2 1 4 3 1 1 4 4 1 2 4 4 1 4 2 3 3 3 3 3 3 3 3 2 1 4 2 4 2 1 2 4 2 1 2 2 1 1 1 2 3 1 1 3 4 2 1 2 4 4 1 2 2 3 3 3 3 4 2 4 1 3 3 2 4 1 3 4 1 3 1 3 3 1 2 2 3 2 4 2 1 4 4 3 2 2 1 4 2 2 4 2 4 2 3 3 2 1 3 3 3 3 1...

output:

9

result:

wrong answer 1st numbers differ - expected: '4', found: '9'

Subtask #4:

score: 0
Wrong Answer

Test #4:

score: 0
Wrong Answer
time: 34ms
memory: 22112kb

input:

100000
1 1 1 2 2 1 2 1 1 1 1 1 2 1 1 2 1 2 2 2 2 1 1 1 1 2 1 2 1 1 2 1 2 2 2 1 2 1 1 2 1 1 2 1 2 2 2 2 2 2 2 1 2 2 1 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 2 2 1 1 2 2 1 1 2 2 2 1 2 1 2 2 1 1 2 1 1 1 1 2 1 1 2 1 1 2 1 2 1 2 2 2 1 2 1 2 1 2 2 2 1 2 1 1 2 1 1 2 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 1 1 2 1 2...

output:

21

result:

wrong answer 1st numbers differ - expected: '2', found: '21'

Subtask #5:

score: 0
Runtime Error

Test #5:

score: 0
Runtime Error

input:

100000
513402173 905110107 1515964 765035952 73901932 836471400 638273851 141529265 534872932 51406447 890683116 47931908 38105393 980251419 528541651 947351982 893221503 90272203 405153875 343727573 238912737 33910798 528133613 280738156 612988056 313310885 65798230 856093442 398702229 181287213 88...

output:


result:


Subtask #6:

score: 10
Accepted

Test #6:

score: 10
Accepted
time: 73ms
memory: 16888kb

input:

100000
830714990 589894538 943716562 157018548 696139650 392186560 878398600 121609777 598909611 482284158 271893438 701809635 751800960 313432974 666336108 18407929 776178328 4559353 746871036 133896190 239663762 1679167 391373768 219219820 540730189 5756262 599943931 186156412 806901497 778383641 ...

output:

625

result:

ok 1 number(s): "625"

Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

0%