P2146 [NOI2015] 软件包管理器

题目

题目

思路

把依赖关系抽象成树,发现插入操作为:

  1. 求根到x的和
  2. 把根到x置1
  3. 求根到x的和
  4. 输出1,3的差的绝对值

卸载操作为:

  1. 求x的子树和并输出
  2. 把x的子树置0

那就树剖了,上代码
code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
//When I wrote this code,God and I unterstood what was I doing 
inline int read()
{
    int ret,c,f=1;
    while (((c=getchar())> '9'||c< '0')&&c!='-');
    if (c=='-') f=-1,ret=0;
    else ret=c-'0';
    while ((c=getchar())>='0'&&c<='9') ret=ret*10+c-'0';
    return ret*f;
}
int xdtree[400020],lazy[400020],a[100005],b[100005],fa[100005],top[100005],zson[100005],dep[100005],id[100005],siz[100005];
int head[100005],nxt[200010],to[200010],n,m,r,tot,x,y,z;
void dfs1(int x,int f)
{
	dep[x]=dep[f]+1;
	fa[x]=f;
	siz[x]=1;
	for (int i=head[x];i!=-1;i=nxt[i])
	{
		if (to[i]==f||to[i]==x) continue;
		dfs1(to[i],x);
		siz[x]+=siz[to[i]];
		if (siz[zson[x]]<siz[to[i]]) zson[x]=to[i];
	}
	return;
}
void dfs2(int x,int f)
{
	id[x]=++tot;
	b[id[x]]=a[x];
	top[x]=f;
	if (zson[x]==0) return;
	dfs2(zson[x],f);
	for (int i=head[x];i!=-1;i=nxt[i])
	{
		if (id[to[i]]) continue;
		dfs2(to[i],to[i]);
	}
	return;
}
void down(int l,int r,int id)
{
	if (lazy[id]!=-1)
	{
		int mid=(l+r)>>1;
		lazy[id*2]=lazy[id],lazy[id*2+1]=lazy[id];
		xdtree[id*2]=(mid-l+1)*lazy[id*2];
		xdtree[id*2+1]=(r-mid)*lazy[id*2]; 
		lazy[id]=-1;
	}
	return;
}
void qjgx(int l,int r,int id,int L,int R,int x)
{
	if (l>=L&&R>=r)
	{
		xdtree[id]=(r-l+1)*x;
		lazy[id]=x;
		return;
	}
	down(l,r,id);
	int mid=(l+r)>>1;
	if (L<=mid) qjgx(l,mid,id*2,L,R,x);
	if (R>mid) qjgx(mid+1,r,id*2+1,L,R,x);
	xdtree[id]=xdtree[id*2]+xdtree[id*2+1];
	return;
}
int qjcx(int l,int r,int id,int L,int R)
{
	if (l>=L&&R>=r)
	{
		return xdtree[id];
	}
	down(l,r,id);
	int mid=(l+r)>>1,ans=0;
	if (L<=mid) ans=ans+qjcx(l,mid,id*2,L,R);
	if (R>mid) ans=ans+qjcx(mid+1,r,id*2+1,L,R);
	return ans;
}
void treeadd(int x,int y,int z)
{
	while (top[x]!=top[y])
	{
		if (dep[top[x]]<dep[top[y]]) swap(x,y);
		qjgx(1,n,1,id[top[x]],id[x],z);
		x=fa[top[x]];
	}
	if (dep[x]<dep[y]) swap(x,y);
	qjgx(1,n,1,id[y],id[x],z);
	return;
}
int treesum(int x,int y)
{
	int ans=0;
	while (top[x]!=top[y])
	{
		if (dep[top[x]]<dep[top[y]]) swap(x,y);
		ans=ans+qjcx(1,n,1,id[top[x]],id[x]);
		x=fa[top[x]];
	}
	if (dep[x]<dep[y]) swap(x,y);
	ans=ans+qjcx(1,n,1,id[y],id[x]);
	return ans;
}
int main()
{
	n=read();
	memset(head,-1,sizeof(head));
	memset(lazy,-1,sizeof(lazy));
	r++;
	for (int i=2;i<=n;i++)
	{
		x=read();
		x++;
		to[++tot]=i;
		nxt[tot]=head[x];
		head[x]=tot;
		to[++tot]=x;
		nxt[tot]=head[i];
		head[i]=tot;
	}
	m=read();
	dfs1(r,0);
	tot=0;
	dfs2(r,r);
	while (m--)
	{
		string wj;
		cin>>wj>>x;
		x++;
		if (wj[0]=='i')
		{
			y=treesum(x,r);
			treeadd(x,r,1);
			y=abs(y-treesum(x,r));
		}
		else
		{
			y=qjcx(1,n,1,id[x],id[x]+siz[x]-1);
			qjgx(1,n,1,id[x],id[x]+siz[x]-1,0);
			y=abs(y-qjcx(1,n,1,id[x],id[x]+siz[x]-1));
		}
		cout<<y<<endl;
	}
	return 0;
}
//Now,only God know
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值