软件包管理器(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用户一定对软件包管理器不会陌生. 通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖 ...
随机推荐
- 一条SQL语句在MySQL中是如何执行的
概览 本篇文章会分析下一个sql语句在mysql中的执行流程,包括sql的查询在mysql内部会怎么流转,sql语句的更新是怎么完成的. 一.mysql架构分析 mysql主要分为Server层和存储 ...
- javase(13)_网络编程
一.概述 1.网络编程的核心是IP.端口(表示应用程序).协议三大元素 2.网络编程的本质是进程间通信 3.网络编程的2个主要问题:1是定位主机,2是数据传输 二.网络通信的概念 1.网络通信协议 计 ...
- Spring根据XML配置文件注入属性 其实也是造bean,看看是使用constructor还是setter顺带完成属性赋值
方法一使用setter方法 package com.swift; public class Book { private String bookName; public void setBook(St ...
- IAP介绍
iOS应用调置 wjforstudy分享了IAP的一些基本知识.在论坛的地址是:http://www.cocoachina.com/bbs/read.php?tid=92060 1.在开始IAP开发 ...
- SVN的配置
Xcode 是开发人员建立 Mac OS X 应用程序的最快捷方式,也是利用新的苹果电脑公司技术的最简单的途径,而SVN是版本控制工具,那么Xcode SVN又是什么呢?如何配置Xcode SVN? ...
- Greenplum/Deepgreen(单机/伪分布)安装文档
Greenplum/Deepgreen数据库安装(单机/伪分布) 首先去官网下载centos7:https://www.centos.org/download/,选择其中一个镜像下载即可,网上随意下载 ...
- shell基础学习-难点重点学习
来自shell13问 -e : 啟用反斜線控制字符的轉換(參考下表) -E:關閉反斜線控制字符的轉換(預設如此) -n : 取消行末之換行符號(與 -e 選項下的 \c 字符同意) 要取消一個变量,在 ...
- linux下如何编译运行c程序
GCC是Linux操作系统下一个非常重要的源代码编译工具,有着许多重要的选项,支持许多不同语言的编译,如C.C++.Ada.Fortran.Objective.Perl.Python.Ruby以及Ja ...
- Luogu 2216 [HAOI2007]理想的正方形 (单调队列优化)
题意: 给出一个 N×M 的矩阵,以及一个数值 K ,求在给定的矩阵中取出一个 K×K 的矩阵其中最大值减去最小值的最小值. 细节: 没有细节来发暴力走天下,20分也是分啊~~~ QAQ. 分析: 感 ...
- java 位向量
public class BitVectory { private int count; private int[] a; private static final int BIT_LEN = 32; ...