QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#403625 | #8318. 白兰地厅的西瓜 | wxqwq# | 100 ✓ | 92ms | 34700kb | C++14 | 2.2kb | 2024-05-02 15:54:30 | 2024-05-02 15:54:30 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0;bool f=0;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?-x:x;
}
#define x first
#define y second
#define pb push_back
#define mid ((l+r)>>1)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long ull;
const int N=1e5+10,M=N*100;
int n;
int w[N],nums[N],cnt;
int h[N],e[N<<1],ne[N<<1],idx;
int root[N],tot;
int ls[M],rs[M],mx[M][2];
int ans;
void add_e(int a,int b) {e[idx]=b,ne[idx]=h[a],h[a]=idx++;}
void modify(int &p,int l,int r,int pos,int v,int op) {
if(!p) p=++tot;
mx[p][op]=max(mx[p][op],v);
if(l==r) return ;
if(mid>=pos) modify(ls[p],l,mid,pos,v,op);
else modify(rs[p],mid+1,r,pos,v,op);
}
int query(int p,int l,int r,int L,int R,int op) {
if(!p) return 0;
if(l>=L && r<=R) return mx[p][op];
if(mid<L) return query(rs[p],mid+1,r,L,R,op);
else if(mid>=R) return query(ls[p],l,mid,L,R,op);
return max(query(ls[p],l,mid,L,R,op),query(rs[p],mid+1,r,L,R,op));
}
int merge(int p,int q) {
if(!p || !q) return p|q;
ans=max(ans,max(mx[ls[p]][0]+mx[rs[q]][1],mx[ls[q]][0]+mx[rs[p]][1]));
mx[p][0]=max(mx[p][0],mx[q][0]),mx[p][1]=max(mx[p][1],mx[q][1]);
ls[p]=merge(ls[p],ls[q]),rs[p]=merge(rs[p],rs[q]);
return p;
}
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,cnt,1,w[u]-1,0),t1=query(root[j],1,cnt,w[u]+1,cnt,1);
ans=max(ans,max(t0+mx1,t1+mx0)+1);
mx0=max(mx0,t0),mx1=max(mx1,t1);
root[u]=merge(root[u],root[j]);
}
// cout<<u<<' '<<mx0<<' '<<mx1<<endl;
modify(root[u],1,cnt,w[u],mx0+1,0);
modify(root[u],1,cnt,w[u],mx1+1,1);
}
int main()
{
n=read(),memset(h,-1,sizeof(h));
rep(i,1,n) w[i]=read(),nums[++cnt]=w[i];
sort(nums+1,nums+1+cnt),cnt=unique(nums+1,nums+1+cnt)-nums-1;
rep(i,1,n) w[i]=lower_bound(nums+1,nums+1+cnt,w[i])-nums;
for(int i=1,x,y;i<n;i++) x=read(),y=read(),add_e(x,y),add_e(y,x);
dfs(1,0);
printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 4236kb
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: 1ms
memory: 6196kb
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: 10
Accepted
Test #3:
score: 10
Accepted
time: 2ms
memory: 4536kb
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:
4
result:
ok 1 number(s): "4"
Subtask #4:
score: 10
Accepted
Test #4:
score: 10
Accepted
time: 16ms
memory: 8808kb
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:
2
result:
ok 1 number(s): "2"
Subtask #5:
score: 10
Accepted
Test #5:
score: 10
Accepted
time: 81ms
memory: 34700kb
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:
3
result:
ok 1 number(s): "3"
Subtask #6:
score: 10
Accepted
Test #6:
score: 10
Accepted
time: 70ms
memory: 16240kb
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: 10
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Dependency #6:
100%
Accepted
Test #7:
score: 10
Accepted
time: 85ms
memory: 31976kb
input:
100000 989006465 273186777 686466908 828906673 784813825 129903897 437927186 855860409 151880008 205622303 52488329 121430183 146482373 989757946 751644599 972173501 118366377 209800585 7798826 850705724 903480939 118462706 38931862 99926861 380670474 857487147 194716288 458379589 947641379 40437503...
output:
16
result:
ok 1 number(s): "16"
Test #8:
score: 0
Accepted
time: 85ms
memory: 27228kb
input:
100000 376325316 423464118 514600155 128241303 617247807 496865863 247253324 397730766 83040054 441316052 791170426 116788240 778460279 333494614 308555803 191070073 611806436 717194899 949015790 76686346 545962957 406128775 26857589 105055762 836616415 737656112 232958186 476290877 309747842 886868...
output:
357
result:
ok 1 number(s): "357"
Test #9:
score: 0
Accepted
time: 92ms
memory: 27940kb
input:
100000 4315 39289 11073 46880 94635 37467 28769 75705 19206 6365 79945 90008 68068 82757 57256 76403 45594 51112 1981 77777 30585 64873 74623 33156 28681 25101 68167 10196 37307 58406 71560 50528 34580 82460 94137 38977 72485 91425 14850 11652 27519 19820 35046 49204 89985 74502 57486 71339 18923 45...
output:
354
result:
ok 1 number(s): "354"
Test #10:
score: 0
Accepted
time: 66ms
memory: 23696kb
input:
100000 35056 14076 32630 34042 2618 6242 12444 4075 43362 28868 22032 34321 3314 45828 36713 39900 1895 19568 27500 27395 12759 28131 33661 31442 11863 43204 22045 18597 40188 30493 20985 6246 1668 35561 10482 48006 33268 42621 27905 4546 48666 13232 29540 23218 25581 47996 14421 33207 16872 39178 1...
output:
50000
result:
ok 1 number(s): "50000"