洛谷2146【NOI2015】 软件包管理器(树链剖分)

传送门

【题目分析】

树链剖分+一点点小变化。

用0表示未安装,1表示已安装。

首先根据给出的关系建树,每一次install(x)操作相当于询问1~x这条路径上有多少个为0的点,直接统计为1的点的个数再用深度去减一下即可,然后将这条路径上的所有点变为1;每一次uninstall(x)操作相当于询问x的子树中有多少为1的点,然后将子树中所有点变为0。

所以支持区间覆盖操作,路径修改路径查询,子树修改子树查询,树链剖分+线段树维护一下即可。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
const int MAXM=2e5+10;

int n,q,cnt;
int head[MAXN],depth[MAXN],siz[MAXN],fa[MAXN],son[MAXN],top[MAXN];
int nxt[MAXM],to[MAXM];
int dfn[MAXN],ys[MAXN],tot;
struct Tree{
	int l,r;
	int sum,tag,siz;
}tr[MAXN<<2];

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

void add(int x,int y){
	nxt[cnt]=head[x];
	head[x]=cnt;
	to[cnt]=y;
	cnt++;
}

void dfs1(int u,int f){
	siz[u]=1;
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==f)
		  continue;
		fa[v]=u,depth[v]=depth[u]+1;
		dfs1(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[son[u]])
		  son[u]=v;
	}
}

void dfs2(int u,int tp){
	top[u]=tp;
	dfn[u]=++tot;
	ys[tot]=u;
	if(!son[u])
	  return ;
	dfs2(son[u],tp);
	for(int i=head[u];i!=-1;i=nxt[i]){
		int v=to[i];
		if(v==fa[u]||v==son[u])
		  continue;
		dfs2(v,v);
	}
}

void push_up(int root){
	tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
	tr[root].siz=tr[root<<1].siz+tr[root<<1|1].siz;
}

void push_now(int root,int v){
	tr[root].tag=v;
	tr[root].sum=tr[root].siz*v;
}

void push_down(int root){
	if(tr[root].tag!=-1){
		push_now(root<<1,tr[root].tag);
		push_now(root<<1|1,tr[root].tag);
		tr[root].tag=-1;
	}
}

void build(int root,int l,int r){
	tr[root].l=l,tr[root].r=r;
	if(l==r){
		tr[root].tag=-1;
		tr[root].siz=1;
		return ;
	}
	int mid=l+r>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	push_up(root);
}

void update(int root,int l,int r,int L,int R,int key){
	if(l>R||r<L)
	  return ;
	if(L<=l&&r<=R){
		push_now(root,key);
		return ;
	}
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  update(root<<1,l,mid,L,R,key);
	else{
		if(L>mid)
		  update(root<<1|1,mid+1,r,L,R,key);
		else
		  update(root<<1,l,mid,L,mid,key),update(root<<1|1,mid+1,r,mid+1,R,key);
	}
	push_up(root);
}

int query(int root,int l,int r,int L,int R){
	if(l>R||r<L)
	  return 0;
	if(L<=l&&r<=R)
	  return tr[root].sum;
	push_down(root);
	int mid=l+r>>1;
	if(R<=mid)
	  return query(root<<1,l,mid,L,R);
	else{
		if(L>mid)
		  return query(root<<1|1,mid+1,r,L,R);
		else
		  return query(root<<1,l,mid,L,mid)+query(root<<1|1,mid+1,r,mid+1,R);
	}
}

void updatepath(int x,int y){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		update(1,1,n,dfn[top[x]],dfn[x],1);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	update(1,1,n,dfn[y],dfn[x],1);
}

int querypath(int x,int y){
	int ret=0;
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])
		  swap(x,y);
		ret+=query(1,1,n,dfn[top[x]],dfn[x]);
		x=fa[top[x]];
	}
	if(depth[x]<depth[y])
	  swap(x,y);
	ret+=query(1,1,n,dfn[y],dfn[x]);
	return ret;
}

void updateson(int x,int key){
	update(1,1,n,dfn[x],dfn[x]+siz[x]-1,key);
}

int queryson(int x){
	return query(1,1,n,dfn[x],dfn[x]+siz[x]-1);
}

int main(){
	memset(head,-1,sizeof(head));
	n=Read();
	for(int i=2;i<=n;++i){
		int x=Read()+1;
		add(x,i);
	}
	dfs1(1,-1);
	dfs2(1,1);
	build(1,1,n);
	q=Read();
	while(q--){
		char cz[15];
		scanf("%s",cz);
		if(cz[0]=='i'){
			int x=Read()+1;
			cout<<depth[x]-depth[1]+1-querypath(1,x)<<'\n';
			updatepath(1,x);
		}
		else{
			int x=Read()+1;
			cout<<queryson(x)<<'\n';
			updateson(x,0);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/g21glf/article/details/83549627