QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#483152 | #8318. 白兰地厅的西瓜 | cyc001 | 100 ✓ | 776ms | 59316kb | C++23 | 4.2kb | 2024-07-18 11:52:34 | 2024-07-18 11:52:35 |
Judging History
answer
#include<bits/stdc++.h>
#define cir(i,a,b) for(int i=a;i<b;++i)
using namespace std;
class segment{
private:
struct node{
node*ls,*rs;
int w;
node():ls(nullptr),rs(nullptr),w(0){}
~node(){
if(ls) delete ls;
if(rs) delete rs;
}
};
node*root;
auto update(node*&u,int l,int r,int p,int w){
if(!u) u=new node();
u->w=max(u->w,w);
if(l==r) return;
const auto mid=(l+r)/2;
p-1<mid?update(u->ls,l,mid,p,w):
update(u->rs,mid+1,r,p,w);
}
auto query(node*u,int l,int r,int ql,int qr){
auto res=0;
if(!u) return res;
if(ql-1<l&&r-1<qr) return u->w;
const auto mid=(l+r)/2;
if(ql-1<mid) res=max(res,query(u->ls,l,mid,ql,qr));
if(mid<qr) res=max(res,query(u->rs,mid+1,r,ql,qr));
return res;
}
int n;
auto dfs(node*u,int l,int r,vector<pair<int,int>>&a){
if(!u) return;
if(l==r) return a.push_back({l,u->w});
const auto mid=(l+r)/2;
dfs(u->ls,l,mid,a);
dfs(u->rs,mid+1,r,a);
}
public:
auto update(int p,int w){
update(root,0,n-1,p,w);
}
auto query(int l,int r){
return query(root,0,n-1,l,r);
}
auto swap(segment&sg){
auto lrt=root;
root=sg.root;sg.root=lrt;
}
auto getelement(){
vector<pair<int,int>> res;
dfs(root,0,n-1,res);
delete root;root=nullptr;
return res;
}
segment(int _n):n(_n-1),root(nullptr){}
~segment(){if(root) delete root;}
};
vector<vector<int>> gr,tr;
vector<int> dfn,low,a,siz,fl,fg;
vector<segment> sgml,sgmg;
auto dfs(int u,int&cnt,int f=0)->void{
dfn[u]=++cnt;siz[u]=1;
for(auto&i:gr[u]) if(i!=f){
dfs(i,cnt,u);siz[u]+=siz[i];
tr[u].push_back(i);
}
low[u]=cnt;
}
auto dfsfx(int u,int&ans,const int n)->void{
sort(tr[u].begin(),tr[u].end(),
[](int a,int b){return siz[a]>siz[b];});
bool hvy=true;
for(auto&i:tr[u]){
dfsfx(i,ans,n);
if(hvy){
sgml[u].swap(sgml[i]);
sgmg[u].swap(sgmg[i]);
}else{
auto elsl=sgml[i].getelement();
auto elsg=sgmg[i].getelement();
for(auto&[c,w]:elsl){
ans=max(ans,w+sgmg[u].query(c+1,n+7));
if(c<a[u]) ans=max(ans,w+sgmg[u].query(a[u]+1,n+7)+1);
}
for(auto&[c,w]:elsg){
ans=max(ans,w+sgml[u].query(0,c-1));
if(c>a[u]) ans=max(ans,w+sgml[u].query(0,a[u]-1)+1);
}
for(auto&[c,w]:elsl){
sgml[u].update(c,w);
}
for(auto&[c,w]:elsg){
sgmg[u].update(c,w);
}
}
hvy=false;
}
sgml[u].update(a[u],fl[u]);
sgmg[u].update(a[u],fg[u]);
}
auto init(int n){
gr.resize(n+1);dfn.resize(n+1);
low.resize(n+1);a.resize(n+1);
tr.resize(n+1);siz.resize(n+1);
fl.resize(n+1);fg.resize(n+1);
sgml.resize(n+1,segment(n+7));
sgmg.resize(n+1,segment(n+7));
}
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;cin>>n;init(n);
map<int,int> id;
cir(i,1,n+1) cin>>a[i],id.insert({a[i],0});
[&](){
auto c=0;
for(auto&[w,x]:id) x=++c;
}();
cir(i,1,n+1) a[i]=id[a[i]];
cir(i,0,n-1){
int u,v;cin>>u>>v;
gr[u].push_back(v);
gr[v].push_back(u);
}
[](){auto c=0;dfs(1,c);}();
[&](){
map<int,vector<int>> nrx;
cir(i,1,n+1) nrx[a[i]].push_back(i);
segment sgl(n+7);
for(auto&[ax,bx]:nrx){
for(auto&u:bx) fl[u]=sgl.query(dfn[u],low[u])+1;
for(auto&u:bx) sgl.update(dfn[u],fl[u]);
}
}();
[&](){
map<int,vector<int>,greater<int>> nrx;
cir(i,1,n+1) nrx[a[i]].push_back(i);
segment sgg(n+7);
for(auto&[ax,bx]:nrx){
for(auto&u:bx) fg[u]=sgg.query(dfn[u],low[u])+1;
for(auto&u:bx) sgg.update(dfn[u],fg[u]);
}
}();
auto ans=0;
cir(u,1,n+1) ans=max({ans,fl[u],fg[u]});
dfsfx(1,ans,n);
cout<<ans<<'\n';
return 0;
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 1ms
memory: 3628kb
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: 4ms
memory: 4052kb
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: 3ms
memory: 4928kb
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: 163ms
memory: 24964kb
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: 446ms
memory: 39160kb
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: 362ms
memory: 59316kb
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: 776ms
memory: 40000kb
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: 524ms
memory: 48816kb
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: 444ms
memory: 49880kb
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: 330ms
memory: 43176kb
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"