WA 求调
查看原帖
WA 求调
793625
Pink_Cut_Tree楼主2024/4/5 19:38

过不了样例

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
#define ls(x) x<<1
#define rs(x) x<<1|1
int n,q;
vector<int>G[N];
int dfn[N],fa[N],sz[N],dep[N],top[N],son[N];
int siz[N<<2],w[N<<2],lzy[N<<2];
string s;
int x,cnt;
void dfs1(int u,int father){
	dep[u]=dep[father]+1;
	fa[u]=father;
	sz[u]=1;
	for(auto v:G[u]){
		if(v==father){
			continue;
		}
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[son[u]]){
			son[u]=v;
		}
	}
}
void dfs2(int u,int tp){
	dfn[u]=++cnt,top[u]=tp;
	if(!son[u]){
		return;
	}
	dfs2(son[u],tp);
	for(auto v:G[u]){
		if(v==son[u]||v==fa[u]){
			continue;
		}
		dfs2(v,v);
	}
}
void pushup(int u){
	w[u]=w[ls(u)]+w[rs(u)];
}
void build(int u,int l,int r){
	siz[u]=r-l+1;
	lzy[u]=2;
	if(l==r){
		return;
	}
	int mid=l+r>>1;
	build(ls(u),l,mid);
	build(rs(u),mid+1,r);
	pushup(u);
}
void pushdown(int u){
	if(lzy[u]<2){
		lzy[ls(u)]=lzy[rs(u)]=lzy[u];
		w[ls(u)]=siz[ls(u)]*lzy[u],w[rs(u)]=siz[rs(u)]*lzy[u];
	}
	lzy[u]=2;
}
void update(int u,int l,int r,int L,int R,int p){
	if(L<=l&&r<=R){
		w[u]=siz[u];
		lzy[u]=p;
	}
	else if(!(r<L||R<l)){
		pushdown(u);
		int mid=l+r>>1;
		update(ls(u),l,mid,L,R,p);
		update(rs(u),mid+1,r,L,R,p);
		pushup(u);
	}
}
void change(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
		}
		update(1,1,n,dfn[top[x]],dfn[x],1);
		x=fa[top[x]];
	}
	if(dfn[x]>dfn[y]){
		swap(x,y);
	}
	update(1,1,n,dfn[x],dfn[y],1);
}
int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin>>n;
	for(int i=2;i<=n;i++){
		cin>>x;
		G[x+1].push_back(i);
		G[i].push_back(x+1); 
	}
	dfs1(1,0);
	dfs2(1,1);
	build(1,1,n);
	cin>>q;
	while(q--){
		cin>>s>>x;
		x++;
		int fore=w[1];
		if(s=="install"){
			change(1,x);
			int after=w[1];
			cout<<abs(fore-after)<<"\n";
		}
		else{
			update(1,1,n,dfn[x],dfn[x]+sz[x]-1,0);
			int after=w[1];
			cout<<abs(fore-after)<<"\n";
		}
	}
return 0;
}
2024/4/5 19:38
加载中...