rt,思路就是把员工尽量往子树中最小的且深度最深的地方放,但是第三个样例过不去了,也找不出问题来,求解
#include<bits/stdc++.h>
#define int long long
#define ll long long
#define next nxt
#define re register
#define il inline
const int N = 2e5 + 5;
const int M = 2e5 + 5;
using namespace std;
int max(int x,int y){return x > y ? x : y;}
int min(int x,int y){return x < y ? x : y;}
int sid,n,k,m,ans[N],tot,id;
int pos,Min,val[N];
int fa[N],dep[N],vis[N];
int op,x,y;
struct Node{
int pos,val;
}a[N],b[N];
struct node{
int u,v,next;
}edge[M<<1]; int head[N],num_edge;
il int read()
{
int f=0,s=0;
char ch=getchar();
for(;!isdigit(ch);ch=getchar()) f |= (ch=='-');
for(; isdigit(ch);ch=getchar()) s = (s<<1) + (s<<3) + (ch^48);
return f ? -s : s;
}
il void add(int from,int to)
{
edge[++num_edge] = (node){from,to,head[from]};
head[from] = num_edge;
}
il bool cmp(Node a,Node b)
{
return (dep[a.pos] ^ dep[b.pos]) ? a.pos > b.pos : a.val < b.val;
}
il void get_dep(int x)
{
dep[x] = dep[fa[x]] + 1;
for(re int i=head[x];i;i=edge[i].next)
{
int y = edge[i].v;
get_dep(y);
}
}
il void dfs(int x)
{
if(Min > val[x] || (Min == val[x] && dep[pos] < dep[x])) { Min = val[x]; pos = x; }//贪心策略暴力找最小值
for(re int i=head[x];i;i=edge[i].next)
{
int y = edge[i].v;
dfs(y);
}
}
il void calc(int ID)
{
tot = 0 , id = 0;
for(re int i=1;i<=k;i++) if(vis[i]) a[++id] = b[i];//看看还有哪些员工没走
//cout << endl << ID << ":" << "\n";
sort(a+1,a+id+1,cmp);
//for(re int i=1;i<=id;i++) cout << dep[a[i].pos] << " ";
//cout << endl;
for(re int i=1;i<=n;i++) val[i] = 0;
for(re int i=1;i<=id;i++)
{
Min = 1e7 , pos = a[i].pos;
dfs(a[i].pos);
if(val[pos] >= a[i].val) continue;//不优的情况直接弃掉
if(pos == a[i].pos) tot = tot + a[i].val - val[pos] , val[pos] = a[i].val;
else { tot = tot + a[i].val - val[pos]; val[pos] = a[i].val; val[a[i].pos] = 0; }
}
ans[ID] = tot;
}
signed main()
{
//freopen("trans.txt","r",stdin);
//freopen("ans.txt","w",stdout);
sid = read();
n = read() , k = read() , m = read();
for(re int i=2;i<=n;i++) fa[i] = read() , add(fa[i],i);
for(re int i=1;i<=k;i++) a[i] = {read(),read()} , b[i] = a[i] , vis[i] = 1;
get_dep(1);
calc(1);
for(re int i=1;i<=m;i++)
{
op = read();
if(op == 1) b[++k]={read(),read()},vis[k]=1;
if(op == 2) vis[read()]=0;
calc(i+1);
}
for(re int i=1;i<=m+1;i++) cout << ans[i] << " ";
return 0;
}