样例全过,提交全wa
查看原帖
样例全过,提交全wa
292029
幽理家的男人楼主2021/3/6 10:08
#include<bits/stdc++.h>
#define tl 2*p
#define tr 2*p+1
using namespace std;
const int maxn=100005;
int n,q,cnt,ctt,head[maxn],vis[maxn],top[maxn],dep[maxn],fa[maxn],son[maxn],size[maxn],is[maxn];
typedef struct a{
	int to,next;
}per;
per si[maxn*4];
typedef struct b{
	int l,r,tag,w;//w线段树维护子树和,tag==1标记全安装,tag==2标记全删除
}node;
node shu[maxn*4];
void add(int u,int v){
	si[++ctt].next=head[u];
	si[ctt].to=v;
	head[u]=ctt;
} 
void dfs1(int now,int f){
	if(f!=-1) dep[now]=dep[f]+1;
	else dep[now]=1;
	fa[now]=f;
	size[now]=1;
	int temp=0;
	for(int i=head[now];i!=-1;i=si[i].next){
		int to=si[i].to;
		dfs1(to,now);
		size[now]+=size[to];
		if(size[to]>temp){
			temp=size[to];
			son[now]=to;
		}	
	}
}
void dfs2(int now,int t){
	top[now]=t;
	vis[now]=++cnt;
	if(!son[now]) return;
    dfs2(son[now],t);
	for(int i=head[now];i!=-1;i=si[i].next){
		int to=si[i].to;
		if(to!=son[now]){
			dfs2(to,to);
		}
	}
}
void build(int p,int l,int r){
	shu[p].l=l;
	shu[p].r=r;
	if(l==r) return;
	int mid=(l+r)/2;
	build(tl,l,mid);
	build(tr,mid+1,r);
}
void spread(int p){
	if(shu[p].tag&&shu[p].l){
        if(shu[p].tag==1){
        	shu[tl].w=shu[tl].r-shu[tl].l+1;
        	shu[tr].w=shu[tr].r-shu[tr].l+1;
		}
		else{
			shu[tl].w=0;
			shu[tr].w=0;
		}
		shu[tl].tag=shu[p].tag;
		shu[tr].tag=shu[p].tag;
		shu[p].tag=0;
	}
}
void change(int p,int l,int r,int sign){
	if(shu[p].l>=l&&shu[p].r<=r){
		shu[p].tag=sign;
		if(sign==1){
			shu[p].w=shu[p].r-shu[p].l+1;
		}
		else{
			shu[p].w=0;
		}
		return;
	}
	spread(p);
	int mid=(shu[p].l+shu[p].r)/2;
	if(l<=mid) change(tl,l,r,sign);
	if(r>mid) change(tr,l,r,sign);
	shu[p].w=shu[tl].w+shu[tr].w;
}
int find(int p,int l,int r){
	//cout<<l<<' '<<r<<"\n";
	if(shu[p].l>=l&&shu[p].r<=r){
		return shu[p].w;
	}
	spread(p);
	int mid=(shu[p].l+shu[p].r)/2;
	int ans=0;
	if(l<=mid)  ans+=find(tl,l,r);
	if(r>mid)  ans+=find(tr,l,r);
	return ans;
}
int main(){
	cin>>n;
	memset(head,-1,sizeof(head));
	for(int i=1;i<n;++i){
		cin>>q;
		add(q,i);
	}
	dfs1(0,-1);
	dfs2(0,0);
	build(1,1,cnt);
	cin>>q;
	string ts;
	int tem;
	while(q--){
		cin>>ts>>tem; 
		if(ts=="install"){//tem向上的链全置1
			int now=tem,temp=0;
			while(now!=-1){//根节点-1

				temp=find(1,vis[top[now]],vis[now]);
				change(1,vis[top[now]],vis[now],1);
				now=fa[top[now]];
			}
			printf("%d",dep[tem]-temp);
		}
		else{//子树全置0
			int now=tem;
			printf("%d",find(1,vis[tem],vis[tem]+size[tem]-1));
			change(1,vis[now],vis[now]+size[now]-1,2);
		}
		if(q) printf("\n");
	}
	return 0;
}

样例都过了,自己构造数据乱试也没什么毛病(QAQ)

2021/3/6 10:08
加载中...