码迷,mamicode.com
首页 > 其他好文 > 详细

【树链剖分】【NOI 2015】【bzoj 4196】软件包管理器

时间:2015-08-13 18:06:11      阅读:1024      评论:0      收藏:0      [点我收藏+]

标签:

4196: [Noi2015]软件包管理器

Time Limit: 10 Sec  Memory Limit: 512 MB
Submit: 122  Solved: 97

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

题解:

题目长而没用。
辨析题意,其实就是给一颗树,然后询问链信息和子树信息,用树链剖分就可以切掉。
ps:听说CA爷写的TopTree。。跪跪跪

Code:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 200100
#define root 1,1,tot
#define lch rt<<1,l,mid
#define rch rt<<1|1,mid+1,r
struct Edge{
    int v,next;
}edge[N<<1];
struct Tree{
    int sum,lazy;
}tree[N<<2];
int n,q,num=0,tot=0,head[N],de[N],fa[N],sz[N],son[N],top[N],w[N],en[N];
inline int in(){
    int x=0; char ch=getchar();
    while (ch<‘0‘ || ch>‘9‘) ch=getchar();
    while (ch>=‘0‘ && ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x;
}
inline void add(int u,int v){
    edge[++num].v=v; edge[num].next=head[u]; head[u]=num;
}
inline void dfs1(int x){
    sz[x]=1; son[x]=0;
    for (int i=head[x]; i; i=edge[i].next){
        int v=edge[i].v;
        if (v==fa[x]) continue;
        de[v]=de[x]+1; fa[v]=x;
        dfs1(v); sz[x]+=sz[v];
        if (!son[x] || sz[son[x]]<sz[v]) son[x]=v;
    }
}
inline void dfs2(int x,int st){
    w[x]=++tot; top[x]=st;
    if (son[x]) dfs2(son[x],st);
    for (int i=head[x]; i; i=edge[i].next){
        int v=edge[i].v;
        if (v==son[x] || v==fa[x]) continue;
        dfs2(v,v);
    }
    en[x]=tot;
}
inline void push_up(int rt,int l,int r){
    if (tree[rt].lazy!=-1)
        tree[rt].sum=tree[rt].lazy*(r-l+1);
    else
        tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
inline void push_down(int rt,int l,int r){
    if (tree[rt].lazy!=-1){
        tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
        tree[rt].lazy=-1;
    }
}
inline void build(int rt,int l,int r){
    tree[rt].sum=0; tree[rt].lazy=-1;
    if (l==r) return;
    int mid=(l+r)>>1;
    build(lch); build(rch);
}
inline void change(int rt,int l,int r,int ll,int rr,int p){
    if (ll<=l && r<=rr){
        tree[rt].lazy=p;
        push_up(rt,l,r);
        return;
    }
    int mid=(l+r)>>1;
    push_down(rt,l,r);
    if (ll<=mid) change(lch,ll,rr,p);
    else push_up(lch);
    if (rr>mid) change(rch,ll,rr,p);
    else push_up(rch);
    push_up(rt,l,r);
}
inline int query(int rt,int l,int r,int ll,int rr,int p){
    if (ll<=l && r<=rr){
        if (p) return r-l+1-tree[rt].sum;
        return tree[rt].sum;
    }
    int mid=(l+r)>>1,k=0;
    push_down(rt,l,r);
    push_up(lch),push_up(rch);
    if (ll<=mid) k+=query(lch,ll,rr,p);
    if (rr>mid) k+=query(rch,ll,rr,p);
    return k;
}
inline void install(int x,int y){
    int ans=0;
    while (top[x]!=top[y]){
        if (de[top[x]]<de[top[y]]) swap(x,y);
        ans+=query(root,w[top[x]],w[x],1);
        change(root,w[top[x]],w[x],1);
        x=fa[top[x]];
    }
    if (de[x]<de[y]) swap(x,y);
    ans+=query(root,w[y],w[x],1);
    change(root,w[y],w[x],1);
    printf("%d\n",ans);
}
inline void uninstall(int x){
    int ans=query(root,w[x],en[x],0);
    change(root,w[x],en[x],0);
    printf("%d\n",ans);
}
int main(){
    n=in();
    for (int i=1; i<n; i++){
        int v=in();
        add(v,i),add(i,v);
    }
    q=in();
    dfs1(0); dfs2(0,0); build(root);
    while (q--){
        char opt=getchar();
        while (opt!=‘i‘ && opt!=‘u‘) opt=getchar();
        int x=in();
        if (opt==‘i‘) install(0,x);
        else uninstall(x);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

【树链剖分】【NOI 2015】【bzoj 4196】软件包管理器

标签:

原文地址:http://blog.csdn.net/morestep/article/details/47615363

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!