P2146 [NOI2015]软件包管理器

题目描述

Linux用户和OSX用户一定对软件包管理器不会陌生。通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个软件包的安装所依赖的其它软件包),完成所有的配置。Debian/Ubuntu使用的apt-get,Fedora/CentOS使用的yum,以及OSX下可用的homebrew都是优秀的软件包管理器。

你决定设计你自己的软件包管理器。不可避免地,你要解决软件包之间的依赖问题。如果软件包A依赖软件包B,那么安装软件包A以前,必须先安装软件包B。同时,如果想要卸载软件包B,则必须卸载软件包A。现在你已经获得了所有的软件包之间的依赖关系。而且,由于你之前的工作,除0号软件包以外,在你的管理器当中的软件包都会依赖一个且仅一个软件包,而0号软件包不依赖任何一个软件包。依赖关系不存在环(若有m(m≥2)个软件包A1,A2,A3,⋯,Am,其中A1依赖A2,A2依赖A3,A3依赖A4,……,A[m-1]依赖Am,而Am依赖A1,则称这m个软件包的依赖关系构成环),当然也不会有一个软件包依赖自己。

现在你要为你的软件包管理器写一个依赖解决程序。根据反馈,用户希望在安装和卸载某个软件包时,快速地知道这个操作实际上会改变多少个软件包的安装状态(即安装操作会安装多少个未安装的软件包,或卸载操作会卸载多少个已安装的软件包),你的任务就是实现这个部分。注意,安装一个已安装的软件包,或卸载一个未安装的软件包,都不会改变任何软件包的安装状态,即在此情况下,改变安装状态的软件包数为0。

输入输出格式

输入格式:

 

从文件manager.in中读入数据。

输入文件的第1行包含1个整数n,表示软件包的总数。软件包从0开始编号。

随后一行包含n−1个整数,相邻整数之间用单个空格隔开,分别表示1,2,3,⋯,n−2,n−1号软件包依赖的软件包的编号。

接下来一行包含1个整数q,表示询问的总数。之后q行,每行1个询问。询问分为两种:

install x:表示安装软件包x

uninstall x:表示卸载软件包x

你需要维护每个软件包的安装状态,一开始所有的软件包都处于未安装状态。

对于每个操作,你需要输出这步操作会改变多少个软件包的安装状态,随后应用这个操作(即改变你维护的安装状态)。

 

输出格式:

 

输出到文件manager.out中。

输出文件包括q行。

输出文件的第i行输出1个整数,为第i步操作中改变安装状态的软件包数。

 

输入输出样例

输入样例#1: 复制
7
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
输出样例#1: 复制
3
1
3
2
3
输入样例#2: 复制
10
0 1 2 1 3 0 0 3 2
10
install 0
install 3
uninstall 2
install 7
install 5
install 9
uninstall 9
install 4
install 1
install 9
输出样例#2: 复制
1
3
2
1
3
1
1
1
0
1

说明

【样例说明 1】

一开始所有的软件包都处于未安装状态。

安装5号软件包,需要安装0,1,5三个软件包。

之后安装6号软件包,只需要安装6号软件包。此时安装了0,1,5,6四个软件包。

卸载1号软件包需要卸载1,5,6三个软件包。此时只有0号软件包还处于安装状态。

之后安装4号软件包,需要安装1,4两个软件包。此时0,1,4处在安装状态。最后,卸载0号软件包会卸载所有的软件包。`

【数据范围】

【时限1s,内存512M】

 

//卸载操作:把以当前点为跟的子树删除,先query一下得出ans再midify 
//安装操作:把当前点到根所处的链的top上的点置为1,要先query一遍求出改变的个数。 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=1e5+5;

int n,m;
int head[N],num_edge;
struct Edge        //这道题就应该是单向边不是双向边了 
{
    int v,nxt;
}edge[N];
struct Node
{
    int fa,son;
    int size;
    int dep;
    int s,t;
    int top;
}node[N];
struct TREE
{
    TREE *lson,*rson;
    int l,r,mid;
    int sum;
    int flag;
}tree[N<<2];

typedef TREE* Tree;
Tree now_node=tree,Root;

inline int read()
{
    char c=getchar();int num=0,f=1;
    for(;!isdigit(c);c=getchar())
        f=c=='-'?-1:f;
    for(;isdigit(c);c=getchar())
        num=num*10+c-'0';
    return num*f;
}

inline void add_edge(int u,int v)
{
    edge[++num_edge].v=v;
    edge[num_edge].nxt=head[u];
    head[u]=num_edge;
}

void dfs1(int u)
{
    node[u].size=1;
    for(int i=head[u],v;i;i=edge[i].nxt)
    {
        v=edge[i].v;
        node[v].fa=u;
        node[v].dep=node[u].dep+1;
        dfs1(v);
        node[u].size+=node[v].size;
        if(!node[u].son||node[v].size>node[node[u].son].size)
            node[u].son=v;
    }
}

int bound;
void dfs2(int u,int top)
{
    node[u].top=top;
    node[u].s=++bound;
    if(node[u].son)
    {
        dfs2(node[u].son,top);
        for(int i=head[u],v;i;i=edge[i].nxt)
        {
            v=edge[i].v;
            if(v==node[u].son)
                continue;
            dfs2(v,v);
        }
    }
    node[u].t=bound;
}

void build(Tree &root,int l,int r)
{
    root=++now_node;
    root->l=l,root->r=r,root->mid=l+r>>1;
    if(l==r)
        return;
    build(root->lson,l,root->mid);
    build(root->rson,root->mid+1,r);
}

inline void pushdown(Tree root)
{
    if(root->flag!=-1)
    {
        root->lson->flag=root->flag;
        root->rson->flag=root->flag;
        root->lson->sum=(root->lson->r-root->lson->l+1)*root->flag;
        root->rson->sum=(root->rson->r-root->rson->l+1)*root->flag;
        root->flag=-1;
    }
}

void update(const Tree &root,int l,int r,int flag)
{
    if(l<=root->l&&root->r<=r)
    {
        root->flag=flag;
        root->sum=(r-l+1)*flag;
        return;
    }
    pushdown(root);
    if(r<=root->mid)
        update(root->lson,l,r,flag);
    else if(l>root->mid)
        update(root->rson,l,r,flag);
    else
    {
        update(root->lson,l,root->mid,flag);
        update(root->rson,root->mid+1,r,flag);
    }
    root->sum=root->lson->sum+root->rson->sum;
}

int query1(const Tree &root,int pos)
{
    if(root->l==root->r)
        return root->sum;
    pushdown(root);
    if(pos<=root->mid)
        return query1(root->lson,pos);
    else
        return query1(root->rson,pos);
}

int query2(const Tree &root,int l,int r)
{
    if(l<=root->l&&root->r<=r)
        return r-l+1-root->sum;
    pushdown(root);
    if(r<=root->mid)
        return query2(root->lson,l,r);
    else if(l>root->mid)
        return query2(root->rson,l,r);
    else
        return query2(root->lson,l,root->mid)+query2(root->rson,root->mid+1,r);
}

int query3(const Tree &root,int l,int r)
{
    if(l<=root->l&&root->r<=r)
        return root->sum;
    pushdown(root);
    if(r<=root->mid)
        return query3(root->lson,l,r);
    else if(l>root->mid)
        return query3(root->rson,l,r);
    else
        return query3(root->lson,l,root->mid)+query3(root->rson,root->mid+1,r);
}

inline void Modify(int x)
{
    int fx=node[x].top;
    while(fx)
    {
        update(Root,node[fx].s,node[x].s,1);
        x=node[fx].fa;
        fx=node[x].top;
    }
    update(Root,1,node[x].s,1);
}

inline int Query(int x)
{
    int fx=node[x].top;
    int ans=0;
    while(fx)
    {
        ans+=query2(Root,node[fx].s,node[x].s);
        x=node[fx].fa;
        fx=node[x].top;
    }
    return ans+query2(Root,1,node[x].s);
}

char s[15];

int main()
{
    n=read();
    for(int i=1,u;i<n;++i)
    {
        u=read();
        add_edge(u,i);
    }
    dfs1(0);
    dfs2(0,0);
    build(Root,1,n);
    m=read();
    int x;
    while(m--)
    {
        scanf("%s",s);
        if(s[0]=='i')
        {
            x=read();
            if(query1(Root,node[x].s))
                puts("0");
            else
            {
                printf("%d\n",Query(x));
                Modify(x);
            }
        }
        else
        {
            x=read();
            if(query1(Root,node[x].s)==0)
                puts("0");
            else
            {
                printf("%d\n",query3(Root,node[x].s,node[x].t));
                update(Root,node[x].s,node[x].t,0);
            }
        }
    }
    return 0;
}

 

posted @ 2018-03-11 16:30  whymhe  阅读(130)  评论(0编辑  收藏  举报