软件包管理器(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用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ...
随机推荐
- 如何在ABAP里用函数式编程思想打印出非波拉契Fibonacci(数列)
在JavaScript里可以用ES6提供的FunctionGenerator这种黑科技来打印非波拉契数列,具体细节参考我这篇文章. 在ABAP里也有很多种方式实现这个需求. 下面这个report分别用 ...
- 文字自动自左向右滚动的js代码
重要的一点,就是scrollLeft一直在变化.对象一直在移动,参照物没有动. 代码: css: #div1{display:black;width:110px;height:50px;line-he ...
- winhex 中磁盘大小与偏移
下图为c盘(活动分区).上方base offset为相对于整个硬盘的字节偏移量.partition 1中信息包括c盘开始扇区,总扇区数.partition 2 信息为扩展分区开始扇区和扇区数.由 P1 ...
- CPP-基础:关于多态
类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持面向对象的,其实不然,Visua ...
- How to debug add-ins for arcgis
Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...
- 安装pycharm 2018.3 Professional Edition
1.下载pycharm 2018.3 Professional 2.下载破解补丁,Gitee仓库 或 直接下载(Direct download link) ,并放到pycharm目录下的\bin目录( ...
- OC各种遍历方法的效率比较
看了一篇博客,挺有意思,OC各种遍历方法的效率,打算自己也测试一番.看看,究竟哪一个的效率更好一些! 准备工作:懒加载一个数组,创建一千万个对象添加到数组. #pragma mark - Lazy M ...
- iptables(1)工具详解
一. iptables 查看链表,创建链表,类命令 1. iptables [-t table] -N chain : 创建一条自定义规则的链 1 2 # iptables -t filter ...
- HDU-2544-最短路(floyd)
板子题,实验一下floyd. #include <cstdio> #include <algorithm> #include <cstring> using nam ...
- ZOJ Monthly, January 2019-Little Sub and Pascal's Triangle
这个题的话,它每行奇数的个数等于该行行号,如果是0开始的,就该数的二进制中的1的个数,设为k,以它作为次数,2k就是了. #include <stdio.h> int main() { i ...