QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#608952 | #5363. ZYB 的游览计划 | Kevin5307 | 0 | 4758ms | 31864kb | C++23 | 3.1kb | 2024-10-04 09:38:23 | 2024-10-04 09:38:23 |
Judging History
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
int n,m;
int a[200200];
vector<int> G[200200];
int in[200200],out[200200],depth[200200],dfn,ind[200200];
int siz[200200],son[200200],tp[200200],f[200200];
void dfs(int u,int fa)
{
f[u]=fa;
depth[u]=depth[fa]+1;
in[u]=++dfn;
ind[dfn]=u;
siz[u]=1;
for(auto v:G[u])
if(v!=fa)
{
dfs(v,u);
if(siz[v]>siz[son[u]])
son[u]=v;
siz[u]+=siz[v];
}
out[u]=dfn;
}
void dfs2(int u,int fa)
{
if(!tp[u]) tp[u]=u;
if(son[u])
{
tp[son[u]]=tp[u];
dfs2(son[u],u);
}
for(auto v:G[u])
if(v!=fa&&v!=son[u])
dfs2(v,u);
}
inline int lca(int x,int y)
{
while(tp[x]!=tp[y])
{
if(depth[tp[x]]<depth[tp[y]]) swap(x,y);
x=f[tp[x]];
}
if(depth[x]<=depth[y]) return x;
return y;
}
vector<ll> dp[200200];
vector<int> pos[200200];
ll Value;
set<int> st;
inline void ins(int x)
{
Value+=depth[x];
auto it=st.lower_bound(in[x]);
if(it!=st.end()&&it!=st.begin())
Value+=depth[lca(ind[*it],ind[*prev(it)])];
if(it!=st.end())
Value-=depth[lca(ind[*it],x)];
if(it!=st.begin())
Value-=depth[lca(ind[*prev(it)],x)];
st.insert(in[x]);
}
inline void del(int x)
{
Value-=depth[x];
st.erase(in[x]);
auto it=st.lower_bound(in[x]);
if(it!=st.end()&&it!=st.begin())
Value-=depth[lca(ind[*it],ind[*prev(it)])];
if(it!=st.end())
Value+=depth[lca(ind[*it],x)];
if(it!=st.begin())
Value+=depth[lca(ind[*prev(it)],x)];
}
void solve(int layer,int l,int r,int L,int R)
{
if(l>r) return ;
int mid=(l+r)/2;
for(int i=max(l,R+1);i<=mid;i++)
ins(a[i]);
ll mx=0;
int mxp=min(R,mid);
for(int i=min(R,mid);i>=L&&i>=pos[mid][layer-1];i--)
{
if(Value+dp[i][layer-1]>mx)
{
mx=Value+dp[i][layer-1];
mxp=i;
}
if(i>L)
ins(a[i]);
}
dp[mid][layer]=mx;
pos[mid][layer]=mxp;
for(int i=min(R,mid);i>L;i--)
del(a[i]);
solve(layer,mid+1,r,mxp,R);
for(int i=max(l,R+1);i<=mid;i++)
del(a[i]);
for(int i=mxp+1;i<=min(R,l-1);i++)
ins(a[i]);
solve(layer,l,mid-1,L,mxp);
for(int i=mxp+1;i<=min(R,l-1);i++)
del(a[i]);
}
int main()
{
ios_base::sync_with_stdio(false);
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].pb(v);
G[v].pb(u);
}
dfs(1,0);
dfs2(1,0);
for(int i=0;i<=n;i++)
dp[i].resize(m+1);
for(int i=0;i<=n;i++)
pos[i].resize(m+1);
for(int i=1;i<=m;i++)
solve(i,1,n,0,n);
cout<<(dp[n][m]-m)*2<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 2ms
memory: 5704kb
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: 4758ms
memory: 28812kb
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: 4196ms
memory: 31864kb
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: 2988ms
memory: 25000kb
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: 2159ms
memory: 23104kb
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: 3335ms
memory: 24024kb
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: 3539ms
memory: 24276kb
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: 0
Wrong Answer
time: 4392ms
memory: 27736kb
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:
252550
result:
wrong answer 1st lines differ - expected: '252628', found: '252550'
Subtask #2:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 22ms
memory: 9900kb
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:
-434
result:
wrong answer 1st lines differ - expected: '35938', found: '-434'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #29:
score: 0
Wrong Answer
time: 476ms
memory: 13860kb
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:
-12
result:
wrong answer 1st lines differ - expected: '78002', found: '-12'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%