zoukankan      html  css  js  c++  java
  • 【bzoj4196】【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步操作中改变安装状态的软件包数。


    样例输入

    7
    0 0 0 1 1 5
    5
    install 5
    install 6
    uninstall 1
    install 4
    uninstall 0


    样例输出

    3
    1
    3
    2
    3


    题解

    其实就是一道模板题。

    #include<cmath>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define ll long long
    
    const int maxn=1e5+50;
    const int maxm=2e5+50;
    
    int n,fa[maxn],m,cnt;
    int fir[maxn],nex[maxm],to[maxm],ecnt;
    int top[maxn],son[maxn],dep[maxn],sz[maxn],id[maxn];
    
    struct SegmentTree{int v,l,r,set;}st[maxn<<2];
    
    void add(int u,int v){
        nex[++ecnt]=fir[u];fir[u]=ecnt;to[ecnt]=v;
    }
    
    void dfs1(int x,int deep){
        dep[x]=deep;
        sz[x]=1;
        int maxson=-1;
        for(int e=fir[x];e;e=nex[e]){
            int v=to[e];
            if(v==fa[x]) continue;
            dfs1(v,deep+1);
            sz[x]+=sz[v];
            if(sz[v]>maxson) maxson=sz[v],son[x]=v;
        }
    }
    
    void dfs2(int x,int topf){
        top[x]=topf;
        id[x]=++cnt;
        if(!son[x]) return ;
        dfs2(son[x],topf);
        for(int e=fir[x];e;e=nex[e]){
            int v=to[e];
            if(v==fa[x]||v==son[x]) continue;
            dfs2(v,v);
        }
    }
    
    void pushup(int root){
        st[root].v=st[root<<1].v+st[root<<1|1].v;
    }
    
    void build(int root,int l,int r){
        st[root].l=l;st[root].r=r;st[root].set=-1;
        if(l==r) return ;
        int m=l+r>>1;
        build(root<<1,l,m);build(root<<1|1,m+1,r);
        pushup(root);
    }
    
    void pushdown(int root){
        if(st[root].set!=-1){
            st[root<<1].v=st[root].set*(st[root<<1].r-st[root<<1].l+1);
            st[root<<1|1].v=st[root].set*(st[root<<1|1].r-st[root<<1|1].l+1);
            st[root<<1].set=st[root<<1|1].set=st[root].set;
            st[root].set=-1;
        }
    }
    
    void change(int root,int l,int r,int val){
        if(st[root].l>r||st[root].r<l) return ;
        if(st[root].l>=l&&st[root].r<=r){
            st[root].v=val*(st[root].r-st[root].l+1);
            st[root].set=val;
        }
        else{
            pushdown(root);
            change(root<<1,l,r,val);
            change(root<<1|1,l,r,val);
            pushup(root);
        }
    }
    
    int query(int root,int l,int r){
        if(st[root].l>r||st[root].r<l) return 0;
        if(st[root].l>=l&&st[root].r<=r) return st[root].v;
        pushdown(root);
        return query(root<<1,l,r)+query(root<<1|1,l,r);
    }
    
    void T_chg(int x,int y,int val){
        int f1=top[x],f2=top[y];
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            change(1,id[f1],id[x],val);
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        change(1,id[x],id[y],val);
    }
    
    int T_qry(int x,int y){
        int f1=top[x],f2=top[y],ans=0;
        while(f1!=f2){
            if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
            ans+=query(1,id[f1],id[x]);
            x=fa[f1];f1=top[x];
        }
        if(dep[x]>dep[y]) swap(x,y);
        ans+=query(1,id[x],id[y]);
        return ans;
    }
    
    void T_chg_son(int x,int val){
        change(1,id[x],id[x]+sz[x]-1,val);
    }
    
    int T_qry_son(int x){
        return query(1,id[x],id[x]+sz[x]-1);
    }
    
    template<typename T>void read(T& aa){
        char cc; ll ff;aa=0;cc=getchar();ff=1;
        while((cc<'0'||cc>'9')&&cc!='-') cc=getchar();
        if(cc=='-') ff=-1,cc=getchar();
        while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
        aa*=ff;
    }
    
    int main(){
        read(n);
        for(int i=1;i<n;i++){
            read(fa[i]);
            add(fa[i],i);add(i,fa[i]);
        }
        dfs1(0,1);dfs2(0,0);build(1,1,n);
        read(m);char c[20];
        while(m--){
            int x;
            scanf("%s",c);read(x);
            if(c[0]=='i'){
                int a1=T_qry(0,x);
                T_chg(0,x,1);
                int a2=T_qry(0,x);
                printf("%d
    ",abs(a1-a2));
            }
            else{
                int a1=T_qry_son(x);
                T_chg_son(x,0);
                printf("%d
    ",a1);
            }
        }
        return 0;
    }
  • 相关阅读:
    利用burpsuite实现重放攻击
    木马分析(隐藏分析)实验
    使用wireshark分析TLS
    ECharts折线图循环展示数据、自定义色值(渐变)
    Sumblime Text3格式化代码
    ECharts柱状图彩色柱状图(渐变),自定义鼠标移入小圈颜色、鼠标移入后提示框显示不全问题、渲染到页面中
    C#多线程学习(五) 多线程的自动管理(定时器)
    SQL取出 所有周六 周日的日期
    C#多线程学习(二) 如何操纵一个线程
    简单读写XML文件
  • 原文地址:https://www.cnblogs.com/rlddd/p/9805857.html
Copyright © 2011-2022 走看看