BZOJ4196: [Noi2015]软件包管理器

【传送门:BZOJ4196


简要题意:

  有n个软件,每个软件有一个依赖的软件(除了第一个软件),要想安装一个软件就必须要安装它所依赖的软件

  有m种操作,有一种是安装某个软件,另一种是删除某个软件,如果删除了一个软件,那么所有直接或间接依赖它的软件都要删除

  求出每种操作改变了多少个软件的状态


题解:

  裸树链剖分

  把软件如果安装了则值为1,否则为0

  如果安装了一个软件,就要把这个软件到根节点的所有点都变成1,在这之前求一下这个软件到根节点的点权和,改变的状态数为总节点数-点权和

  如果删除了一个软件,就要把这个软件的子树内的所有点都变成0,在这之前求一下子树的点权和,改变的状态数就是点权和

  最后在操作的时候判断一下当前的软件有没有出现重复安装或者重复删除的情况就好了


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x,y,next;
}a[110000];int len,last[110000];
void ins(int x,int y)
{
    len++;
    a[len].x=x;a[len].y=y;
    a[len].next=last[x];last[x]=len;
}
int tot[110000],dep[110000],fa[110000],son[110000];
void pre_tree_node(int x)
{
    tot[x]=1;son[x]=0;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa[x])
        {
            fa[y]=x;
            dep[y]=dep[x]+1;
            pre_tree_node(y);
            tot[x]+=tot[y];
            if(tot[y]>tot[son[x]]) son[x]=y;
        }
    }
}
int ys[110000],z,top[110000],l[110000],r[110000];
void pre_tree_edge(int x,int tp)
{
    ys[x]=++z;top[x]=tp;
    l[x]=z;
    if(son[x]!=0) pre_tree_edge(son[x],tp);
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(y!=fa[x]&&y!=son[x]) pre_tree_edge(y,y);
    }
    r[x]=z;
}
struct trnode
{
    int l,r,lc,rc,c;
    int lazy;
}tr[210000];int trlen;
void bt(int l,int r)
{
    int now=++trlen;
    tr[now].l=l;tr[now].r=r;tr[now].c=0;tr[now].lazy=-1;
    tr[now].lc=tr[now].rc=-1;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=trlen+1;bt(l,mid);
        tr[now].rc=trlen+1;bt(mid+1,r);
    }
}
void update(int now)
{
    int lc=tr[now].lc,rc=tr[now].rc;
    if(lc!=-1)
    {
        tr[lc].c=(tr[lc].r-tr[lc].l+1)*tr[now].lazy;
        tr[lc].lazy=tr[now].lazy;
    }
    if(rc!=-1)
    {
        tr[rc].c=(tr[rc].r-tr[rc].l+1)*tr[now].lazy;
        tr[rc].lazy=tr[now].lazy;
    }
    tr[now].lazy=-1;
}
void change(int now,int l,int r,int c)
{
    if(tr[now].l==l&&tr[now].r==r)
    {
        tr[now].c=(r-l+1)*c;
        tr[now].lazy=c;
        return ;
    }
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].lazy!=-1) update(now);
    if(r<=mid) change(lc,l,r,c);
    else if(l>mid) change(rc,l,r,c);
    else change(lc,l,mid,c),change(rc,mid+1,r,c);
    tr[now].c=tr[lc].c+tr[rc].c;
}
int findsum(int now,int l,int r)
{
    if(tr[now].l==l&&tr[now].r==r) return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].lazy!=-1) update(now);
    if(r<=mid) return findsum(lc,l,r);
    else if(l>mid) return findsum(rc,l,r);
    else return findsum(lc,l,mid)+findsum(rc,mid+1,r);
}
int solve(int x)
{
    int tx=top[x],ans=0;
    while(x!=0)
    {
        ans+=(ys[x]-ys[tx]+1)-findsum(1,ys[tx],ys[x]);
        change(1,ys[tx],ys[x],1);
        x=fa[tx];tx=top[x];
    }
    return ans;
}
int findc(int now,int x)
{
    if(tr[now].l==tr[now].r) return tr[now].c;
    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;
    if(tr[now].lazy!=-1) update(now);
    if(x<=mid) return findc(lc,x);
    else return findc(rc,x);
}
int main()
{
    int n;
    scanf("%d",&n);
    len=0;memset(last,0,sizeof(last));
    for(int i=2;i<=n;i++)
    {
        int f;
        scanf("%d",&f);f++;
        ins(f,i);
    }
    fa[1]=0;dep[1]=0;pre_tree_node(1);
    z=0;pre_tree_edge(1,1);
    trlen=0;bt(1,z);
    int m;
    scanf("%d",&m);
    char st[11];
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%s%d",st+1,&x);x++;
        if(st[1]=='i')
        {
            if(findc(1,ys[x])==1) printf("0\n");
            else printf("%d\n",solve(x));
        }
        else
        {
            if(findc(1,ys[x])==0) printf("0\n");
            else
            {
                printf("%d\n",findsum(1,l[x],r[x]));
                change(1,l[x],r[x],0);
            }
        }
    }
    return 0;
}

 

posted @ 2018-04-07 21:26  Star_Feel  阅读(151)  评论(0编辑  收藏  举报