软件包管理器(bzoj 4196)
Description
Linux用户和OSX用户一定对软件包管理器不会陌生。通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖(即下载安装这个软件包的安装所依赖的其它软件包),完成所有的配置。Debian/Ubuntu使用的apt-get,Fedora/CentOS使用的yum,以及OSX下可用的homebrew都是优秀的软件包管理器。
Input
输入文件的第1行包含1个正整数n,表示软件包的总数。软件包从0开始编号。
Output
输出文件包括q行。
Sample Input
0 0 0 1 1 5
5
install 5
install 6
uninstall 1
install 4
uninstall 0
Sample Output
1
3
2
3
HINT
一开始所有的软件包都处于未安装状态。
/*
安装就是安装一条链,可用树链剖分,删除就是删除一棵子树,根据dfs序的性质,他们是在一段连续的区间上,所以也可以用树链剖分解决。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
int n,cnt,tot,root;
struct node{
int to,pre;
};node e[N];
int head[N],fa[N],size[N],dep[N],son[N],top[N],pos[N],end[N],sum[N*],tag[N*],col[N*];
void init(){
memset(head,-,sizeof(head));
cnt=;
}
void add(int from,int to){
e[cnt].to=to;
e[cnt].pre=head[from];
head[from]=cnt++;
}
void dfs1(int now){
size[now]=,son[now]=-;
for(int i=head[now];i!=-;i=e[i].pre){
int to=e[i].to;
if(to==fa[now])continue;
dep[to]=dep[now]+;
fa[to]=now;
dfs1(to);
size[now]+=size[to];
if(son[now]==-||size[son[now]]<size[to])son[now]=to;
}
}
void dfs2(int now,int tp){
top[now]=tp,pos[now]=++tot;
if(son[now]!=-)dfs2(son[now],tp);
for(int i=head[now];i!=-;i=e[i].pre){
int to=e[i].to;
if(to==fa[now]||to==son[now])continue;
dfs2(to,to);
}
end[now]=tot;
}
void pushup(int rt){
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt,int m){
if(tag[rt]){
sum[rt<<]=(m-(m>>))*col[rt];
sum[rt<<|]=(m>>)*col[rt];
tag[rt<<]=,tag[rt<<|]=;
col[rt<<]=col[rt];
col[rt<<|]=col[rt];
tag[rt]=;
}
}
void update(int L,int R,int l,int r,int rt,int jd){
if(L<=l&&r<=R){
tag[rt]=,col[rt]=jd,sum[rt]=(r-l+)*jd;
return;
}
int mid=(l+r)>>;
pushdown(rt,r-l+);
if(L<=mid)update(L,R,lson,jd);
if(R>mid)update(L,R,rson,jd);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt,int jd){
if(L<=l&&r<=R)return sum[rt];
pushdown(rt,r-l+);
int ret=;
int mid=(l+r)>>;
if(L<=mid)ret+=query(L,R,lson,jd);
if(R>mid)ret+=query(L,R,rson,jd);
//pushup(rt);
return ret;
}
int install(int x,int y){
int ans=;
ans=query(,n,,n,,);
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
int l=pos[top[x]],r=pos[x];
update(l,r,,n,,);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
int l=pos[x],r=pos[y];
update(l,r,,n,,);
ans=query(,n,,n,,)-ans;
return ans;
}
char s[];
int main(){
init();
scanf("%d",&n);
for(int i=;i<=n;i++){
int x;scanf("%d",&x);x++;
add(x,i);
}
dfs1();dfs2(,);
int q;
scanf("%d",&q);
while(q--){
scanf("%s",s);
int x;
if(s[]=='i'){
scanf("%d",&x);x++;
printf("%d\n",install(,x));
}
else{
scanf("%d",&x);x++;
int l=pos[x],r=end[x];
int ans=query(,n,,n,,);
update(l,r,,n,,);
ans-=query(,n,,n,,);
printf("%d\n",ans);
}
}
}
软件包管理器(bzoj 4196)的更多相关文章
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
		4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ... 
- Bzoj 4196: [Noi2015]软件包管理器  树链剖分
		4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 721 Solved: 419[Submit][Statu ... 
- bzoj 4196  [Noi2015]软件包管理器 (树链剖分+线段树)
		4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2852 Solved: 1668[Submit][Sta ... 
- bzoj 4196: [Noi2015]软件包管理器
		Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ... 
- 【刷题】BZOJ 4196 [Noi2015]软件包管理器
		Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ... 
- 4196. [NOI2015]软件包管理器【树链剖分】
		Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ... 
- 4196: [Noi2015]软件包管理器
		Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 412 Solved: 251[Submit][Status][Discuss] Descriptio ... 
- [BZOJ4196][NOI2015]软件包管理器
		4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1040 Solved: 603[Submit][Stat ... 
- BZOJ4196 软件包管理器
		Description Linux用户和OSX用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ... 
随机推荐
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
			spring boot web项目运行时提示如下错误 org.springframework.beans.factory.BeanCreationException: Error creating b ... 
- CNNs 在图像分割中应用简史: 从R-CNN到Mask R-CNN
			作者:嫩芽33出处:http://www.cnblogs.com/nenya33/p/6756024.html 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须 ... 
- C#反射调用小DEMO
			程序集的源代码: namespace DesignMode { class IOCTest { public void TestO() { Console.WriteLine("O方法&qu ... 
- IOS7 Text View 截断的问题解决
			- (void)textViewDidChange:(UITextView *)textView { CGRect line = [textView caretRectForPosition: tex ... 
- archlinux alsa安装,音量设置和音量信息保存
			1,使用前确认安装了alsa-utils sudo pacman -S alsa-utils2,运行alsamixer调试音量 alsamixer左右键选择调哪个,将Master和PCM按“m”解除静 ... 
- a标签点击后更改颜色
			function choose(id){ document.getElementById("typeid").value = id; //var infoa=document.ge ... 
- java面试宝典第二弹
			arraylist和linklist的区别,hashmap和hashset的区别,常用的集合有哪些 一.基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, M ... 
- mcu读写调式
			拿仿真SPIS为例: 对于其他外设(UART.SPIM.I2S.I2C...)都是一个道理. 当MCU写时:主要对一个寄存器进行写,此寄存器是外设的入口(基本都会做并转串逻辑). spis_tx_da ... 
- InnoDB体系架构总结(二)
			事务 确保事务内的SQL都可以同步执行 要么一起成功 要么一起失败.事务有四个特性原子性 一致性,隔离性,持久性 实现方式 开始事务的时候回家记录记录一个LSN日志序列 当事务执行的时候 会首先在In ... 
- 初识Pyhon之准备环境
			安装成功python的运行环境之后,你可能要迫不及待大展身手了 如果你有一定的语言基础,那么基础这一块儿就可以简单的看看就可以了,但是你是一个编程语言的初学者.不着急,慢慢往下看 打开pycharm创 ... 
