QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#609251 | #5363. ZYB 的游览计划 | rainboy | 5 | 129ms | 44812kb | C++14 | 2.4kb | 2024-10-04 11:35:41 | 2024-10-04 11:35:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
int n,m,a[200009],fa[200009][20],dep[200009],sz[200009],dfn[200009],dfnc,anti[200009];
int c[200009],d[200009];
set<pair<int,int> > s;
vector<int> g[200009];
void dfs(int u,int f){
dep[u]=dep[f]+1;fa[u][0]=f;anti[dfn[u]=++dfnc]=u;sz[u]=1;
for(int i=1;i<20;i++)fa[u][i]=fa[fa[u][i-1]][i-1];
for(int v:g[u])if(v!=f)dfs(v,u),sz[u]+=sz[v];
}
struct fenwick{
vector<int> ch;
int sum[200009];
void clear(){for(int x:ch)sum[x]=0;ch.clear();}
void upd(int x,int f){while(x<=n)ch.push_back(x),sum[x]+=f,x+=x&-x;}
int query(int x){int f=0;while(x)f+=sum[x],x-=x&-x;return f;}
int query(int l,int r){return query(r)-query(l-1);}
}tr;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++){
int u,v;cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,0);
s.insert({1,n});
while(--m){
pair<int,pair<int,pair<int,int> > > ans={0,{0,{0,0}}};
for(auto p:s){
tr.clear();
for(int i=p.first,cur=0;i<=p.second;i++){
int u=a[i];
if(tr.query(dfn[u],dfn[u]+sz[u]-1)){c[i]=(cur-1)*2;continue;}
for(int p=15;~p;p--)
if(fa[u][p]&&!tr.query(dfn[fa[u][p]],dfn[fa[u][p]]+sz[fa[u][p]]-1))
u=fa[u][p];
cur+=dep[a[i]]-dep[u]+1;
tr.upd(dfn[a[i]],1);
c[i]=(cur-1)*2;
}
tr.clear();
for(int i=p.second,cur=0;i>=p.first;i--){
int u=a[i];
if(tr.query(dfn[u],dfn[u]+sz[u]-1)){d[i]=(cur-1)*2;continue;}
for(int p=15;~p;p--)
if(fa[u][p]&&!tr.query(dfn[fa[u][p]],dfn[fa[u][p]]+sz[fa[u][p]]-1))
u=fa[u][p];
cur+=dep[a[i]]-dep[u]+1;
tr.upd(dfn[a[i]],1);
d[i]=(cur-1)*2;
}
for(int i=p.first;i<p.second;i++)
ans=max(ans,{c[i]+d[i+1]-c[p.second],{i,p}});
}
s.erase(ans.second.second);
s.insert({ans.second.second.first,ans.second.first});
s.insert({ans.second.first+1,ans.second.second.second});
}
int ans=0;
for(auto p:s){
tr.clear();
for(int i=p.first,cur=0;i<=p.second;i++){
int u=a[i];
if(tr.query(dfn[u],dfn[u]+sz[u]-1)){c[i]=(cur-1)*2;continue;}
for(int p=15;~p;p--)
if(fa[u][p]&&!tr.query(dfn[fa[u][p]],dfn[fa[u][p]]+sz[fa[u][p]]-1))
u=fa[u][p];
cur+=dep[a[i]]-dep[u]+1;
tr.upd(dfn[a[i]],1);
c[i]=(cur-1)*2;
}
ans+=c[p.second];
}
cout<<ans<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 16156kb
input:
5 2 2 4 3 5 1 1 2 2 3 3 4 4 5
output:
14
result:
ok single line: '14'
Test #2:
score: 5
Accepted
time: 95ms
memory: 44812kb
input:
90752 2 88555 48815 61754 47133 45462 73740 84783 58077 73713 78537 14562 78533 71607 24890 16284 41187 78450 31531 25296 45169 55240 83197 82146 86338 83180 75924 9923 40553 84394 73069 7278 77214 24896 14446 87566 70680 48218 58608 86578 47698 86173 89371 77350 85782 36318 22735 80925 5435 76359 2...
output:
251418
result:
ok single line: '251418'
Test #3:
score: 5
Accepted
time: 129ms
memory: 41468kb
input:
93770 2 89903 29857 80965 80811 71223 80535 36801 25153 57709 29654 38160 35249 58550 68675 29353 62251 50118 79990 51189 61691 79132 19597 57750 1034 90042 93286 75086 57631 92276 26985 52590 90754 16542 92552 80430 73904 35365 89129 1663 11819 55751 24513 88548 90162 83633 19731 68199 6748 27706 7...
output:
290726
result:
ok single line: '290726'
Test #4:
score: 5
Accepted
time: 68ms
memory: 38212kb
input:
65317 2 51766 57961 28299 31999 40127 16772 33540 19124 26900 33659 7103 63514 2505 39507 56023 31079 63274 63343 14843 61644 28909 58494 61041 56428 65315 53077 63700 38453 61569 39339 5844 38272 51519 40290 26380 63569 37168 58037 59325 54243 64499 52417 37227 47397 54105 53297 16022 3375 22482 52...
output:
180930
result:
ok single line: '180930'
Test #5:
score: 5
Accepted
time: 66ms
memory: 31848kb
input:
54968 2 32076 44969 33748 31214 39664 34931 30817 25648 47689 46763 47967 53937 53728 18220 27763 46030 53523 44219 20200 14610 40293 40336 29098 33745 16007 46631 45935 24773 34522 25436 54008 35558 34959 54454 51456 25414 16526 38487 28216 38555 35027 26147 24162 11326 46548 50826 48554 32604 4735...
output:
171654
result:
ok single line: '171654'
Test #6:
score: 5
Accepted
time: 58ms
memory: 39576kb
input:
68335 2 28375 24392 60247 61323 52947 34744 57888 61373 50395 65896 19146 39921 65851 48750 1542 11291 32925 47723 58218 48669 10903 27842 45110 41451 62005 61905 38873 12161 53816 65654 43312 28094 51487 66072 63629 55510 47316 51979 64731 52920 67110 45018 28259 46018 60733 49970 51000 19511 24760...
output:
200690
result:
ok single line: '200690'
Test #7:
score: 5
Accepted
time: 64ms
memory: 39048kb
input:
72649 2 71869 56974 5397 57803 33008 70619 3561 36980 64022 44606 20962 65004 62918 65560 59254 70785 22210 71921 58629 61718 14809 45500 45395 9868 54723 30438 24983 58229 42978 67848 32425 34444 69676 30915 68879 63038 63829 33271 30029 57654 58744 64114 18811 69970 60443 36161 69714 56496 9668 59...
output:
201180
result:
ok single line: '201180'
Test #8:
score: 5
Accepted
time: 82ms
memory: 41928kb
input:
86016 2 66729 82992 85681 46404 58569 77836 82916 83518 79848 37068 43830 62675 56215 72709 15612 35712 65773 76805 85528 81357 56301 74881 13692 46936 50574 85728 82638 41595 70796 17045 61500 46114 79294 60756 56282 81104 70770 82699 72010 8531 66950 81157 70631 80579 6614 65467 42497 80999 63948 ...
output:
252628
result:
ok single line: '252628'
Test #9:
score: 5
Accepted
time: 114ms
memory: 40808kb
input:
75667 2 15091 63031 65720 55413 44774 71773 51901 69149 48649 73881 61574 68157 65817 73499 30726 46050 71914 33687 31751 46428 71704 74847 44532 73723 53020 67563 55007 69177 62435 32197 63068 66348 36407 66223 11393 72153 39127 75129 54135 50349 64047 25960 70200 40639 57590 59123 58615 9518 73652...
output:
231734
result:
ok single line: '231734'
Subtask #2:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 38ms
memory: 18064kb
input:
760 217 632 417 493 400 316 482 542 614 36 134 604 291 745 484 451 746 518 378 487 650 633 223 601 259 33 257 309 683 705 627 513 670 130 395 512 115 466 376 575 356 180 716 539 403 431 563 568 468 596 239 296 379 537 224 526 215 725 234 663 603 401 21 75 660 506 393 105 88 462 620 449 338 276 200 4...
output:
35796
result:
wrong answer 1st lines differ - expected: '35938', found: '35796'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #29:
score: 0
Wrong Answer
time: 28ms
memory: 21212kb
input:
14878 6 1663 4532 2022 11114 1768 7744 12403 7698 14863 1406 13199 9405 3528 9898 1205 3076 11342 7459 9401 10025 14498 7178 11457 1802 9923 1648 13136 10720 3002 7332 13780 2094 1716 13215 8118 318 11186 14833 7941 8174 8999 6189 7504 13738 4933 3367 12973 1889 9835 4080 3546 1993 1861 11613 2504 1...
output:
76580
result:
wrong answer 1st lines differ - expected: '78002', found: '76580'
Subtask #5:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%