QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#387060 | #5363. ZYB 的游览计划 | 0x2ee08 | 0 | 1224ms | 34720kb | C++14 | 4.1kb | 2024-04-12 00:48:34 | 2024-04-12 00:48:35 |
Judging History
answer
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define ll long long
#define vec vector<long long>
#define pb push_back
#define sz(a) a.size()
#define fi first
#define se second
#define ret(a) return cout<<(a)<<"\n",void()
#define endl "\n"
#define el cout<<"\n"
#define f(a) for(int i=0;i<(a);i++)
#define f1(a) for(int i=1;i<(a);i++)
#define all(v) v.begin(),v.end()
template<class T> void PRINTARR(T a[], int n){for(int i=0;i<n;i++){std::cout<<a[i]<<" ";}std::cout<<'\n';}
template<class T> void PRINTVEC(std::vector<T> arr){for(int i=0;i<arr.size();i++){std::cout<<arr[i]<<" ";}std::cout << '\n';}
int tc=1;
void input(){
}
const ll oo = 1e18;
const int maxn = 2e5+5;
const int mod = 1e9+7;
const int S = 360;
//REMEMBER:
//USE LONG LONG WHEN NECESSARY
//ALWAYS USE "\n" OR el;
//PRAGMA?
int n,k;
vector<int> p;
vector<int> g[maxn];
ll dp[2][maxn];
int tin[maxn]={},tout[maxn]={};
int vis[maxn]={};
int up[maxn][20]={};
int h[maxn]={};
//dp[i][j]=argmin{dp[i-1][k-1]+C(k,j)}
int L,R;
ll cur=0,pp=1;
void dfs(int u){
tin[u]=pp;pp++;
for(auto v:g[u]){
if(!vis[v]){
vis[v]=1;
h[v]=h[u]+1;
up[v][0]=u;
for(int i=1;i<20;i++){
up[v][i]=up[up[v][i-1]][i-1];
}
dfs(v);
}
}
tout[u]=pp;pp++;
}
//2 way to imple:
//easy: set.
//hard: trie. (time+space efficient)
int rtin[2*maxn],rtout[2*maxn];
set<int> s;
void init(){
for(int i=1;i<=n;i++){
s.insert(tin[i]);
rtin[tin[i]]=i;
rtout[tout[i]]=i;
}
//sus
rtin[tout[1]]=1;
//
s.insert(tout[1]);
cur=(n-1)*2;
}
int lca(int u, int v){
if(h[u]>h[v]) swap(u,v);
//h[u]<=h[v]
int diff=h[v]-h[u];
for(int i=0;i<20;i++){
if(diff&(1<<i)){
v=up[v][i];
}
}
if(u==v) return u;
for(int i=19;i>=0;i--){
int u1=up[u][i],v1=up[v][i];
if(u1!=v1){
u=u1;v=v1;
}
}
return up[u][0];
}
ll dis(int u, int v){
if(h[u]>h[v]) swap(u,v);
//h[u]<=h[v];
int x=lca(u,v);
if(x==u) return h[v]-h[u];
return (h[u]-h[x])+(h[v]-h[x]);
}
void update(int l, int r){
//test dnc dp xem tm tinh chat ko:
//cur=mn(l,r);
//Optimize:
while(L>l){
L--;
if(p[L]!=1){
s.insert(tin[p[L]]);
auto it1=s.lower_bound(tin[p[L]]);it1--;
auto it2=s.lower_bound(tin[p[L]]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
cur+=(dis(prv,p[L])+dis(p[L],aft)-dis(prv,aft));
}
}
while(L<l){
if(p[L]!=1){
auto it1=s.lower_bound(tin[p[L]]);it1--;
auto it2=s.lower_bound(tin[p[L]]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
s.erase(tin[p[L]]);
cur-=(dis(prv,p[L])+dis(p[L],aft)-dis(prv,aft));
}
L++;
}
while(R>r){
if(p[R]!=1){
auto it1=s.lower_bound(tin[p[R]]);it1--;
auto it2=s.lower_bound(tin[p[R]]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
s.erase(tin[p[R]]);
cur-=(dis(prv,p[R])+dis(p[R],aft)-dis(prv,aft));
}
R--;
}
while(R<r){
R++;
if(p[R]!=1){
s.insert(tin[p[R]]);
auto it1=s.lower_bound(tin[p[R]]);it1--;
auto it2=s.lower_bound(tin[p[R]]);it2++;
int prv=rtin[*it1],aft=rtin[*it2];
cur+=(dis(prv,p[R])+dis(p[R],aft)-dis(prv,aft));
}
}
//cout<<l<<" "<<r<<" "<<cur<<endl;
}
void cal(int l, int r, int otpl, int otpr){
if(l>r) return;
int mid=(l+r)/2;
int otp;
for(int k=otpl;k<=min(mid,otpr);k++){
update(k,mid);
if(dp[1][mid]<dp[0][k-1]+cur){
dp[1][mid]=dp[0][k-1]+cur;
otp=k;
}
}
cal(l,mid-1,otp,otpr);
cal(mid+1,r,otpl,otp);
}
void solve()
{
cin>>n>>k;
L=1,R=n;
p.resize(n+5);
f1(n+1) cin>>p[i];
f(n-1){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
}
vis[1]=1;
h[1]=1;
dfs(1);
init();
memset(dp,0,sizeof(dp));
for(int i=1;i<=k;i++){
cal(1,n,1,n);
for(int j=0;j<=n;j++){
dp[0][j]=dp[1][j];
dp[1][j]=0;
}
}
cout<<dp[0][n];
}
int32_t main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
input();
while (tc--)
solve();
cerr << "\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 0ms
memory: 18460kb
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
Wrong Answer
time: 1224ms
memory: 34720kb
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:
239918
result:
wrong answer 1st lines differ - expected: '251418', found: '239918'
Subtask #2:
score: 0
Wrong Answer
Test #10:
score: 0
Wrong Answer
time: 502ms
memory: 18932kb
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:
4342
result:
wrong answer 1st lines differ - expected: '35938', found: '4342'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Wrong Answer
Test #29:
score: 0
Wrong Answer
time: 510ms
memory: 22088kb
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:
66088
result:
wrong answer 1st lines differ - expected: '78002', found: '66088'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%