【树链剖分】【NOI 2015】【bzoj 4196】软件包管理器
2015-08-13 17:01
561 查看
4196: [Noi2015]软件包管理器
[code]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个询问。询问分为两种:
[code]installx:表示安装软件包x uninstallx:表示卸载软件包x
你需要维护每个软件包的安装状态,一开始所有的软件包都处于未安装状态。对于每个操作,你需要输出这步操作会改变多少个软件包的安装状态,随后应用这个操作(即改变你维护的安装状态)。
Output
输出文件包括q行。
输出文件的第i行输出1个整数,为第i步操作中改变安装状态的软件包数。
Sample Input
[code]7 0 0 0 1 1 5 5 install 5 install 6 uninstall 1 install 4 uninstall 0
Sample Output
[code]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 号软件包会卸载所有的软件包。
[code]n<=100000 q<=100000
题解:
题目长而没用。
辨析题意,其实就是给一颗树,然后询问链信息和子树信息,用树链剖分就可以切掉。
ps:听说CA爷写的TopTree。。跪跪跪
Code:
[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 ,de ,fa ,sz ,son ,top ,w ,en ; 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; }
相关文章推荐
- 关于在Android Studio中使用requestWindowFeather()
- kamailio4.2.6 安装和配置教程
- JS模块化工具requirejs教程(一):初识requirejs
- For嵌套输出图形
- 三、城市企业经济体制改革
- 【JavaScript】document对象属性
- Ajax的一些笔记
- 关于无线PCB中 中50欧姆的特性阻抗的注意事项
- Reorder List
- JQ $("#form :input" )与$("#form input")的区别 以及后代元素和子元素
- SOAPUI 项目新建
- OpenWRT开发之——对C++的支持(解决库依赖问题)
- Linux makefile 教程
- [C#] zdbviewcs: 跨平台数据库查看器。支持SqlServer、Oracle、MySql等数据库
- 半成品博客皮肤(不知有没有继续下去的必要)
- iOS 开发中农历与公历的转换,星座计算。
- 二、农村土地家庭承包改革
- 计算机系统(一)信息就是(位+上下文)
- [置顶] WPF+WinForm系统主页框架Demo(附带一些小功能!)
- Linux终端常用指令-Linux terminal commonly used commands