[UOJ UR #4追击圣诞老人]

时间:2021-10-15 19:40:13

来自FallDream的博客,未经允许,请勿转载, 谢谢。


传送门

考虑直接维护一个堆,然后往里面丢链,并且取出k个堆顶就行了。

然后就需要分类讨论啥的,给你的三个点变成两条链,每次取出一条链之后选择权值最小的再劈成两条链丢进去。

卡空间  所以树剖,不选择倍增

复杂度O((n+k)logn)

#include<iostream>
#include<cstdio>
#include<queue>
#define MN 500000
#define N 524288
#define ll long long
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct data{int x,y;ll X;
friend bool operator <(const data&x,const data&y){return x.X>y.X;}
data operator + (ll y)
{
data c=*this;c.X+=y;
return c;
}
};
priority_queue<data> q;
int n,k,w[MN+],dep[MN+],head[MN+],cnt=,top[MN+],mx[MN+];
int s[MN+],p[MN+],fa[MN+],T[N*+],dfn[MN+],dn=;
struct edge{int to,next;}e[MN+];
inline void ins(int f,int t){e[++cnt]=(edge){t,head[f]};head[f]=cnt;}
vector<data>v[MN+];
int Merge(int x,int y){return w[x]>w[y]?y:x;}
int query(int l,int r)
{
int sum=;
for(l+=N-,r+=N+;l^r^;l>>=,r>>=)
{
if(~l&) sum=Merge(sum,T[l+]);
if( r&) sum=Merge(sum,T[r-]);
}
return sum;
}
inline int Up(int x,int k)
{
int z=dep[x]-k;
for(;dep[top[x]]>z;x=fa[top[x]]);
return p[dfn[top[x]]+z-dep[top[x]]];
}
int lca(int x,int y)
{
for(;top[x]!=top[y];x=fa[top[x]])
if(dep[top[x]]<dep[top[y]]) swap(x,y);
return dep[x]<dep[y]?x:y;
} pair<int,int> Query(int x,int y)
{
int res=;
for(;top[x]!=top[y];x=fa[top[x]])
{
if(dep[top[x]]<dep[top[y]]) swap(x,y);
res=Merge(res,s[x]);
}
if(dfn[x]>dfn[y]) swap(x,y);
res=Merge(res,query(dfn[x],dfn[y]));
return make_pair(x,res);
} data Insert(int x,int y,ll v)
{
// cout<<"Insert"<<x<<" "<<y<<" "<<v<<endl;
pair<int,int> z=Query(x,y);
// cout<<"CalcOK"<<z.first<<" "<<z.second<<endl;
return (data){x,y,w[z.second]}+v;
} void Solve(int x,int y,int z,int l,ll Add)
{
// cout<<"Solve"<<x<<" "<<y<<" "<<z<<" "<<l<<" "<<Add<<endl;
if(z==l)
{
if(x!=z) q.push(Insert(x,Up(x,dep[x]-dep[z]-),Add));
if(y!=z) q.push(Insert(y,Up(y,dep[y]-dep[z]-),Add));
return;
}
if(!(dep[x]>=dep[z]&&Up(x,dep[x]-dep[z])==z)) swap(x,y);
if(x!=z) q.push(Insert(x,Up(x,dep[x]-dep[z]-),Add));
q.push(Insert(fa[z],y,Add));
} void Dfs(int x,int tp)
{
top[x]=tp;p[dfn[x]=++dn]=x;
if(tp==x) s[x]=x; else s[x]=Merge(s[fa[x]],x);
if(mx[x]) Dfs(mx[x],tp);
for(int i=head[x];i;i=e[i].next)
if(e[i].to!=mx[x]) Dfs(e[i].to,e[i].to);
} void Pre(int x)
{
top[x]=;mx[x]=;
for(int i=head[x];i;i=e[i].next)
{
Pre(e[i].to);
top[x]+=top[e[i].to];
if(top[e[i].to]>top[mx[x]]) mx[x]=e[i].to;
}
} int main()
{
n=read();k=read();w[]=1e9;
for(int i=;i<=n;++i) w[i]=read(),q.push((data){i,i,w[i]});
for(int i=;i<=n;++i) ins(fa[i]=read(),i),dep[i]=dep[fa[i]]+;
Pre();Dfs(,);
for(int i=;i<=n;++i) T[i+N]=p[i];
for(int i=N;i;--i) T[i]=Merge(T[i<<],T[i<<|]);
for(int i=;i<=n;++i)
{
int x=read(),y=read(),z=read();
if(x==y&&y==z){v[i].push_back((data){x,x,w[x]});continue;}
if(x==y) swap(x,z);v[i].push_back(Insert(x,y,));
if(z==y||z==x) continue;
int l1=lca(x,z),l2=lca(y,z),L=lca(x,y);
if(dep[z]<dep[L]) v[i].push_back(Insert(fa[L],z,));
else if(z!=l1&&z!=l2)
{
if(dep[l1]<dep[l2]) swap(l1,l2);
int Z=Up(z,dep[z]-dep[l1]-);
v[i].push_back(Insert(z,Z,));
}
}
for(int i=;i<=k;++i)
{
data x=q.top();q.pop();printf("%lld\n",x.X);int z=Query(x.x,x.y).second;
//printf("%d %d %d %d %lld\n",x.x,x.y,x.l,x.z,x.X);
Solve(x.x,x.y,z,lca(x.x,x.y),x.X-w[z]);
for(int j=;j<v[z].size();++j) q.push(v[z][j]+x.X);
}
return ;
}