QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#716054 | #5363. ZYB 的游览计划 | xujindong | 0 | 2508ms | 27252kb | C++14 | 2.2kb | 2024-11-06 14:12:20 | 2024-11-06 14:12:21 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,k,a[200005],dep[200005],dfn[200005],d,minn[200005][20],lg[200005],f[200005],g[200005],l=1,r,now;
vector<int>e[200005];
set<pair<int,int> >s;
void dfs(int pos,int fa,vector<int>e[]){
minn[dfn[pos]=++d][0]=fa,dep[pos]=dep[fa]+1;
for(int i=0;i<e[pos].size();i++)if(e[pos][i]!=fa)dfs(e[pos][i],pos,e);
}
int cmp(int a,int b){
return dfn[a]<dfn[b]?a:b;
}
void init(int s,vector<int>e[]){
dfs(s,0,e);
for(int i=2;i<=d;i++)lg[i]=lg[i>>1]+1;
for(int j=1;1<<j<=d;j++)for(int i=1;i+(1<<j)-1<=d;i++)minn[i][j]=cmp(minn[i][j-1],minn[i+(1<<(j-1))][j-1]);
}
int lca(int u,int v){
if(u==v)return u;
u=dfn[u],v=dfn[v];
if(u>v)swap(u,v);
return u++,cmp(minn[u][lg[v-u+1]],minn[v-(1<<lg[v-u+1])+1][lg[v-u+1]]);
}
int dis(int u,int v){
return dep[u]+dep[v]-2*dep[lca(u,v)];
}
void add(int x){
if(x==1)return;
s.insert(make_pair(dfn[x],x));
set<pair<int,int> >::iterator it=s.find(make_pair(dfn[x],x)),pre=it,nxt=it;
if(it==s.begin())pre=--s.end();
else pre--;
if(it==--s.end())nxt=s.begin();
else nxt++;
int t1=(*pre).second,t2=(*nxt).second;
now+=dis(t1,x)+dis(x,t2)-dis(t1,t2);
}
void del(int x){
if(x==1)return;
set<pair<int,int> >::iterator it=s.find(make_pair(dfn[x],x)),pre=it,nxt=it;
if(it==s.begin())pre=--s.end();
else pre--;
if(it==--s.end())nxt=s.begin();
else nxt++;
int t1=(*pre).second,t2=(*nxt).second;
now-=dis(t1,x)+dis(x,t2)-dis(t1,t2),s.erase(make_pair(dfn[x],x));
}
int calc(int gl,int gr){
while(l>gl)add(a[--l]);
while(r<gr)add(a[++r]);
while(l<gl)del(a[l++]);
while(r>gr)del(a[r--]);
return now;
}
void solve(int l1,int r1,int l2,int r2){
if(l2>r2)return;
int mid=(l2+r2)>>1,pos=mid;
for(int i=l1;i<=min(mid,r1);i++)if(f[i-1]+calc(i,mid)>g[mid])g[mid]=f[i-1]+calc(i,mid),pos=i;
solve(l1,pos,l2,mid-1),solve(pos,r1,mid+1,r2);
}
int main(){
s.insert(make_pair(1,1)),cin>>n>>k;
for(int i=1,p;i<=n;i++)cin>>p,a[n-p+1]=i;
for(int i=1,u,v;i<n;i++)cin>>u>>v,e[u].push_back(v),e[v].push_back(u);
init(1,e);
for(int i=1;i<=k;i++){
solve(1,n,1,n);
for(int j=1;j<=n;j++)f[j]=g[j],g[j]=0;
}
return cout<<f[n]<<'\n',0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 3ms
memory: 13636kb
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: 0
Wrong Answer
time: 2508ms
memory: 27252kb
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:
251264
result:
wrong answer 1st lines differ - expected: '251418', found: '251264'
Subtask #2:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 397ms
memory: 10636kb
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:
35962
result:
wrong answer 1st lines differ - expected: '35938', found: '35962'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #29:
score: 0
Wrong Answer
time: 624ms
memory: 16780kb
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:
77906
result:
wrong answer 1st lines differ - expected: '78002', found: '77906'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%