题解:P9168 [省选联考 2023] 人员调度
WinResearcher · · 个人记录
P9168
题意:给一棵树,树上有一些物品,物品有价值和初始位置。每个物品都可以移动到它初始位置的子树里的任意一个点,每个点的价值是在这个点的所有物品的价值的最大值。你需要支持插入物品和删除物品。每次操作后输出这棵树所有点的最大价值和。
非常好题,删掉删除操作后放noip很不错。读错题读错了几个月都没发现,今天终于读对了,非常难绷。
首先考虑静态问题,显然每个物品应该尽量不重合的散布到其子树内,如果重合就牺牲价值小的。直接维护非常简单,从下到上维护一个可并堆(或启发式合并,线段树合并等等)存子树内所有产生价值的物品。每次合并所有子树的堆,然后逐个考虑当前点的物品,判断子树内是否还有空位(即堆的大小是不是等于子树大小),有就放进空位,没有就从堆里弹出价值最小的物品,插入新的物品。直接这样做复杂度是
然后我们考虑快速维护这个过程。称“把物品尽量不重合的散布到子树内后,没有物品的点”为空位。我们在
其中
但可能没有空位,或按上面的过程继续向上合并,有其他物品挤占了这个空位。可以发现第一种情况和第二种情况本质相同(向上合并
所以我们现在要快速维护每个子树的空位数
最后考虑删除,显然这个过程是可撤销的,线段树分治即可。复杂度
需要防范线段树合并空信息之类的五花八门的坑点,写起来非常烦人,但总体上还算好写。大样例强度不错,过了大样例基本就能通过了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REPG(i,h,x) for(int i=h[x];~i;i=edge[i].next)
#define CLR(a,x) memset(a,x,sizeof(a))
#define pii pair<int,int>
const int INF=0x3f3f3f3f;
const int G=1e5+5;
int head[G],gcnt=1;
struct Edge{int v,next,w;}edge[G];
inline void addedge(int u,int v,int w){edge[++gcnt]={v,head[u],w},head[u]=gcnt;}
const int N=2e5+5;
int n,q,fa[N],dfn[N],rk[N],dep[N],hson[N],siz[N],top[N],cnt,tot,lst[N];
ll cur,ans[N];
vector<int> t[N<<2];
stack<pii> stk;
struct node{
int v,pos;
node(){}
node(int x,int y):v(x),pos(y){}
}a[N];
struct Cmp{
inline bool operator()(const int &x,const int &y){
if(a[x].v==a[y].v) return x<y;
return a[x].v<a[y].v;
}
};
multiset<int,Cmp> ms[N];
void dfs1(int p){
dep[p]=dep[fa[p]]+1,siz[p]=1;
REPG(i,head,p){
int to=edge[i].v;
dfs1(to);
siz[p]+=siz[to];
if(siz[hson[p]]<siz[to]) hson[p]=to;
}
}
void dfs2(int p,int topf){
dfn[p]=++cnt,top[p]=topf,rk[cnt]=p;
if(!hson[p]) return;
dfs2(hson[p],topf);
REPG(i,head,p){
int to=edge[i].v;
if(!dfn[to]) dfs2(to,to);
}
}
inline int lc(int p){return p<<1;}
inline int rc(int p){return p<<1|1;}
namespace SGT1{
int t[N<<2];
inline int merge(int x,int y){
if(!x) return y;
else if(!y) return x;
else if(a[x].v<a[y].v) return x;
return y;
}
inline void pushup(int p){t[p]=merge(t[lc(p)],t[rc(p)]);}
void insert(int tt,int l,int r,int p,int id){
if(l==r) return (void)(ms[tt].insert(id),t[p]=*ms[tt].begin());
int mid=l+r>>1;
if(tt<=mid) insert(tt,l,mid,lc(p),id);
else insert(tt,mid+1,r,rc(p),id);
pushup(p);
}
void remove(int tt,int l,int r,int p,int id){
if(l==r) return (void)(ms[tt].erase(ms[tt].find(id)),t[p]=(ms[tt].empty()?0:*ms[tt].begin()));
int mid=l+r>>1;
if(tt<=mid) remove(tt,l,mid,lc(p),id);
else remove(tt,mid+1,r,rc(p),id);
pushup(p);
}
int query(int tl,int tr,int l,int r,int p){
if(tl<=l&&r<=tr) return t[p];
int mid=l+r>>1;
if(tl>mid) return query(tl,tr,mid+1,r,rc(p));
else if(tr<=mid) return query(tl,tr,l,mid,lc(p));
return merge(query(tl,tr,l,mid,lc(p)),query(tl,tr,mid+1,r,rc(p)));
}
}
namespace SGT2{
struct node{
int v,pos;
node(){v=INF,pos=0;}
node(int x,int y):v(x),pos(y){}
inline bool operator<(const node &x)const{
if(v==x.v) return dep[pos]>dep[x.pos];
return v<x.v;
}
}t[N<<2];
int lazy[N<<2];
inline void pushup(int p){t[p]=min(t[lc(p)],t[rc(p)]);}
inline void push(int p,int v){t[p].v+=v,lazy[p]+=v;}
inline void pushdown(int p){if(lazy[p]) push(lc(p),lazy[p]),push(rc(p),lazy[p]),lazy[p]=0;}
void update(int tl,int tr,int l,int r,int p,int c){
if(tl<=l&&r<=tr) return push(p,c);
int mid=l+r>>1;
pushdown(p);
if(tl<=mid) update(tl,tr,l,mid,lc(p),c);
if(tr>mid) update(tl,tr,mid+1,r,rc(p),c);
pushup(p);
}
node query(int tl,int tr,int l,int r,int p){
if(tl<=l&&r<=tr) return t[p];
int mid=l+r>>1;node ret;
pushdown(p);
if(tl<=mid) ret=query(tl,tr,l,mid,lc(p));
if(tr>mid) ret=min(ret,query(tl,tr,mid+1,r,rc(p)));
return ret;
}
void build(int p,int l,int r){
if(l==r) return (void)(t[p]={siz[rk[l]],rk[l]});
int mid=l+r>>1;
build(lc(p),l,mid),build(rc(p),mid+1,r);
pushup(p);
}
inline void updatep(int p,int v){while(p) update(dfn[top[p]],dfn[p],1,n,1,v),p=fa[top[p]];}
inline node queryp(int p){
node ret;
while(p) ret=min(ret,query(dfn[top[p]],dfn[p],1,n,1)),p=fa[top[p]];
return ret;
}
}
inline int insert(int id){
SGT2::node anc=SGT2::queryp(a[id].pos);
if((!anc.pos)||anc.v>0){
cur+=a[id].v;
SGT1::insert(dfn[a[id].pos],1,n,1,id);
SGT2::updatep(a[id].pos,-1);
stk.push({id,1});
return 1;
}
int tgt=SGT1::query(dfn[anc.pos],dfn[anc.pos]+siz[anc.pos]-1,1,n,1);
if(a[tgt].v>=a[id].v) return 0;
cur+=a[id].v-a[tgt].v;
SGT1::remove(dfn[a[tgt].pos],1,n,1,tgt);
SGT2::updatep(a[tgt].pos,1);
SGT1::insert(dfn[a[id].pos],1,n,1,id);
SGT2::updatep(a[id].pos,-1);
stk.push({tgt,-1}),stk.push({id,1});
return 2;
}
void update(int tl,int tr,int l,int r,int p,int id){
if(tl<=l&&r<=tr) return (void)(t[p].push_back(id));
int mid=l+r>>1;
if(tl<=mid) update(tl,tr,l,mid,lc(p),id);
if(tr>mid) update(tl,tr,mid+1,r,rc(p),id);
}
void solve(int p,int l,int r){
int tot=0;
for(auto i:t[p]) tot+=insert(i);
int mid=l+r>>1;
if(l==r) ans[l]=cur;
else solve(lc(p),l,mid),solve(rc(p),mid+1,r);
while(tot--){
pii p=stk.top();stk.pop();
if(p.second==1){
cur-=a[p.first].v;
SGT1::remove(dfn[a[p.first].pos],1,n,1,p.first);
SGT2::updatep(a[p.first].pos,1);
}else{
cur+=a[p.first].v;
SGT1::insert(dfn[a[p.first].pos],1,n,1,p.first);
SGT2::updatep(a[p.first].pos,-1);
}
}
}
int main(){
// ios::sync_with_stdio(false);
// cin.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
CLR(head,-1);
scanf("%*d%d%d%d",&n,&tot,&q);
for(int i=2;i<=n;i++) scanf("%d",&fa[i]),addedge(fa[i],i,0);
dfs1(1),dfs2(1,1),SGT2::build(1,1,n);
for(int i=1;i<=tot;i++) scanf("%d%d",&a[i].pos,&a[i].v);
for(int i=1,op,x,v;i<=q;i++){
scanf("%d%d",&op,&x);
if(op==1) scanf("%d",&v),a[++tot]={v,x},lst[tot]=i;
if(op==2) update(lst[x],i-1,0,q,1,x),lst[x]=-1;
}
for(int i=1;i<=tot;i++) if(lst[i]!=-1) update(lst[i],q,0,q,1,i);
solve(1,0,q);
for(int i=0;i<=q;i++) printf("%lld ",ans[i]);
return 0;
}