软件包管理器(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用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ...
随机推荐
- python基础一 day10(2)
复习: # 三元运算符# 接收结果的变量 = 条件为真的结果 if 条件 else 条件为假的结果# 接收结果的变量 = “真结果” if 条件 else “假结果”## 命名空间 和 作用域# 三种 ...
- feature map计算大小公式
http://blog.csdn.net/cheese_pop/article/details/51955915 将整个分成两部分,左边部分,右边部分.右边部分每次其实都是移动stride这么大,左边 ...
- AEE加密解密
from Crypto.Cipher import AESfrom binascii import b2a_hex, a2b_hex class AesHandler(object): def ...
- vue引用文件
1)css引入在vue页面中<style scoped> @import url("../assets/css/home.css");</style>2)j ...
- shell脚本,配置文件加载顺序,以及什么时候加载。
在linux系统中,有/etc/profile,/etc/bashrc ,~/.bash_profile,~/bashrc这四个配置文件,这些文件,会自动的在某些时候加载,也就是点一下,一般都是些别名 ...
- ios copy assign retain
一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...
- Gentoo更新portage记录
小记一下这两天更新服务器版本遇到的各种问题. 服务器系统: Gentoo 第一天 其实本来不打算更新系统的,因为最近想试试免费的SSL证书,于是自然而然搜到了letsencrypt,跟着他们的流程需要 ...
- mysql:破解MySQL密码的一种方法
1, 修改mysql配置文件/etc/my.cnf 12 [mysqld]skip_grant_tables 2, 重启mysql后直接用root用户登录(不用输入密码) 1 $ mysql -uro ...
- laravel富文本编辑和图片上传
---恢复内容开始--- 首先先找到一个适合的编辑器是胜利的一步,选择wangEditor这个编辑器 地址:http://www.wangeditor.com/ 然后选择下载,我是通过网上学习的,所以 ...
- python 列表(增删改查)
列表 :(列表可以嵌套,列表的中的元素可以为任意) 列表的创建:1. a = [1, 2, 3] 2. a = list([1, 2, 3]) 1.查: 索引(下标),都是从0开始 切片 .c ...