zoukankan      html  css  js  c++  java
  • BZOJ4196 软件包管理器

    Description

     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,……,Am−1依赖Am,而Am依赖A1,则称这m个软件包的依赖关系构成 环),当然也不会有一个软件包依赖自己。
    现在你要为你的软件包管理器写一个依赖解决程序。根据反馈,用户希望在安装和卸载某个软件包时,快速地知道这个操作实际上会改变多少个软件包的 安装状态(即安装操作会安装多少个未安装的软件包,或卸载操作会卸载多少个已安装的软件包),你的任务就是实现这个部分。注意,安装一个已安装的软件包, 或卸载一个未安装的软件包,都不会改变任何软件包的安装状态,即在此情况下,改变安装状态的软件包数为0。

    Input

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

    随后一行包含n−1个整数,相邻整数之间用单个空格隔开,分别表示1,2,3,…,n−2,n−1号软件包依赖的软件包的编号。
    接下来一行包含1个正整数q,表示询问的总数。
    之后q行,每行1个询问。询问分为两种:
    installx:表示安装软件包x
    uninstallx:表示卸载软件包x
    你需要维护每个软件包的安装状态,一开始所有的软件包都处于未安装状态。对于每个操作,你需要输出这步操作会改变多少个软件包的安装状态,随后应用这个操作(即改变你维护的安装状态)。

    Output

    输出文件包括q行。

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

    Sample Input

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

    Sample Output

    3
    1
    3
    2
    3

    HINT

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

     

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

     

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

     

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

     

    之后安装 4 号软件包,需要安装 1,4 两个软件包。此时 0,1,4 处在安装状态。

     

    最后,卸载 0 号软件包会卸载所有的软件包。



    n=100000

     

    q=100000
     
     
    正解:树链剖分+线段树
    解题报告:
      刷NOI2015的时候居然顺利地切到了T2,NOI果然比省选水多了。
      然而上次做的时候一直wa,今天做了BZOJ那道LCA之后才发现原来是我线段树打萎了,这道题也一样,改了线段树之后就AC了。
      其实很简单,就是每次将一棵子树置为0,或者给一条链置为1,果断链剖+线段树。
     
      
      1 //It is made by jump~
      2 #include <iostream>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <algorithm>
      8 #include <ctime>
      9 #include <vector>
     10 #include <queue>
     11 #include <map>
     12 #include <set>
     13 #ifdef WIN32   
     14 #define OT "%I64d"
     15 #else
     16 #define OT "%lld"
     17 #endif
     18 using namespace std;
     19 typedef long long LL;
     20 const int MAXN = 100011;
     21 int n,father[MAXN],q;
     22 int deep[MAXN],first[MAXN],next[MAXN*2],to[MAXN*2];
     23 int top[MAXN],size[MAXN],son[MAXN],id[MAXN],pre[MAXN];
     24 int last[MAXN];//last[i]表示的是原号码为i(而不是id)的最后一个id
     25 int ecnt;
     26 char ch[12];
     27 int ans;
     28 int ql,qr;
     29 
     30 struct node{
     31     int lazy,sum;
     32     int l,r,size;
     33 }a[MAXN*4];
     34 
     35 inline int getint()
     36 {
     37        int w=0,q=0;
     38        char c=getchar();
     39        while((c<'0' || c>'9') && c!='-') c=getchar();
     40        if (c=='-')  q=1, c=getchar();
     41        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
     42        return q ? -w : w;
     43 }
     44 
     45 inline void dfs(int x,int fa){
     46     size[x]=1;
     47     for(int i=first[x];i;i=next[i])  {
     48     int v=to[i];
     49     deep[v]=deep[x]+1;
     50     dfs(v,x);
     51     size[x]+=size[v];
     52     if(size[v]>size[son[x]]) son[x]=v;
     53     }
     54 }
     55 
     56 inline void dfs2(int x,int fa){
     57     id[x]=++ecnt; pre[ecnt]=x;
     58     if(son[x]) {  top[son[x]]=top[x]; dfs2(son[x],x);  }
     59     for(int i=first[x];i;i=next[i]) {
     60     int v=to[i];
     61     if(v!=son[x]) {
     62         top[v]=v;
     63         dfs2(v,x);
     64     }
     65     }
     66     last[x]=ecnt;
     67 }
     68 
     69 inline void pushdown(int root){
     70     if(a[root].size==1) return ;
     71     if(a[root].lazy) {
     72     int lc=root*2,rc=lc+1;
     73     a[lc].lazy=a[rc].lazy=a[root].lazy;
     74     if(a[lc].lazy==1) a[lc].sum=a[rc].sum=0;
     75     else if(a[lc].lazy==2) a[lc].sum=a[lc].size,a[rc].sum=a[rc].size;    
     76     a[root].lazy=0;
     77     }
     78 }
     79 
     80 inline void update(int root,int l,int r){
     81     pushdown(root);
     82     if(ql<=l && qr>=r) {
     83     if(a[root].lazy==1) ans+=r-l+1,a[root].sum=r-l+1,a[root].lazy=2;
     84     else if(!a[root].lazy) {  
     85         ans+=r-l+1-a[root].sum;
     86         a[root].sum=r-l+1;
     87         a[root].lazy=2;
     88     }    
     89     return ;
     90     }
     91     int mid=(l+r)/2;
     92     int lc=root*2,rc=lc+1;
     93     if(ql<=mid) update(lc,l,mid);
     94     if(qr>mid) update(rc,mid+1,r); 
     95     a[root].sum=a[lc].sum+a[rc].sum;
     96 }
     97 
     98 inline void lca(int x){
     99     ans=0;
    100     int f1=top[x];
    101     while(x){
    102     ql=id[f1],qr=id[x];
    103     update(1,1,n);
    104     x=father[f1]; f1=top[x];
    105     }
    106     printf("%d
    ",ans);
    107 }
    108 
    109 inline void jian(int root,int l,int r){
    110     pushdown(root);
    111     if(ql<=l && r<=qr) {
    112     if(a[root].lazy==2) a[root].lazy=1,a[root].sum=0,ans+=r-l+1;
    113     else if(!a[root].lazy) {
    114         a[root].lazy=1;    
    115         ans+=a[root].sum;    
    116         a[root].sum=0;
    117     }
    118     return ;
    119     }
    120     int mid=(l+r)/2; int lc=root*2,rc=lc+1;
    121     if(ql<=mid) jian(lc,l,mid);
    122     if(qr>mid) jian(rc,mid+1,r); 
    123     a[root].sum=a[lc].sum+a[rc].sum;
    124 }
    125 
    126 inline void suf(int x){
    127     ans=0;
    128     ql=id[x]; qr=last[x];
    129     jian(1,1,n);
    130     printf("%d
    ",ans);
    131 }
    132 
    133 inline void build(int root,int l,int r){
    134     a[root].size=r-l+1; a[root].l=l; a[root].r=r;
    135     if(l==r) return ;    
    136     int mid=(l+r)/2; int lc=root*2,rc=lc+1;
    137     build(lc,l,mid); build(rc,mid+1,r);
    138 }
    139 
    140 inline void work(){
    141     n=getint();
    142     for(int i=2;i<=n;i++) father[i]=getint()+1,next[++ecnt]=first[father[i]],first[father[i]]=ecnt,to[ecnt]=i;
    143     deep[1]=1;  dfs(1,0);
    144     top[1]=1; ecnt=0; dfs2(1,0);
    145     build(1,1,n);
    146     q=getint();
    147     int x;
    148     for(int i=1;i<=q;i++) {
    149     scanf("%s",ch);
    150     if(ch[0]=='i') x=getint()+1,lca(x);
    151     else x=getint()+1,suf(x);
    152     }
    153 }
    154 
    155 int main()
    156 {
    157   work();  
    158   return 0;
    159 }
  • 相关阅读:
    LeetCode Arithmetic Slices
    LeetCode Number of Longest Increasing Subsequence
    LeetCode Longest Increasing Subsequence
    shrio注解的方式进行权限控制
    30分钟学会如何使用Shiro(转自:http://www.cnblogs.com/learnhow/p/5694876.html)
    eclipse逆向生成实体类
    redis2.3.7安装时出现undefined reference to `clock_gettime'
    使用Nginx+Lua(OpenResty)开发高性能Web应用
    shrio初体验(2)Realm
    shrio初体验(1)
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5679334.html
Copyright © 2011-2022 走看看