4196: [Noi2015]软件包管理器
Time Limit: 10 Sec Memory Limit: 512 MB
Submit: 412 Solved: 251
[Submit][Status][Discuss]
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
一开始所有的软件包都处于未安装状态。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn=;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int N,M;
int dep[maxn],siz[maxn],fa[maxn],son[maxn],top[maxn],id[maxn];
int val[maxn];
int num;
char s[];
vector<int> to[maxn];
inline void dfs1(int rt,int fath,int deep){
dep[rt]=deep; siz[rt]=; son[rt]=; fa[rt]=fath;
for(int i=;i<to[rt].size();i++){
int y=to[rt][i];
if(y!=fa[rt]){
dfs1(y,rt,deep+);
siz[rt]+=siz[y];
if(siz[son[rt]]<siz[y]){
son[rt]=y;
}
}
}
}
inline void dfs2(int rt,int tp){
top[rt]=tp;
id[rt]=++num;
if(son[rt]!=) dfs2(son[rt],tp);
for(int i=;i<to[rt].size();i++){
int y=to[rt][i];
if(y!=fa[rt]&&y!=son[rt]){
dfs2(y,y);
}
}
} struct Tree{
int l,r,sum,lazy;
}tree[maxn*];
inline void build(int rt,int l,int r){
tree[rt].l=l; tree[rt].r=r;
if(l==r){
tree[rt].sum=;
tree[rt].lazy=;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid); build(rt<<|,mid+,r);
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
inline void update_son(int rt){
int d=tree[rt].lazy;
if(d!=){
tree[rt<<].sum+=(tree[rt<<].r-tree[rt<<].l+)*d;
if(tree[rt<<].sum<) tree[rt<<].sum=;
if(tree[rt<<].sum>(tree[rt<<].r-tree[rt<<].l+))
tree[rt<<].sum=(tree[rt<<].r-tree[rt<<].l+);
tree[rt<<|].sum+=(tree[rt<<|].r-tree[rt<<|].l+)*d;
if(tree[rt<<|].sum<) tree[rt<<|].sum=;
if(tree[rt<<|].sum>(tree[rt<<|].r-tree[rt<<|].l+))
tree[rt<<|].sum=(tree[rt<<|].r-tree[rt<<|].l+);
tree[rt<<].lazy=tree[rt].lazy; tree[rt<<|].lazy=tree[rt].lazy;
tree[rt].lazy=;
}
}
inline void change(LL rt,LL l,LL r,LL delta){
if(l<=tree[rt].l&&tree[rt].r<=r){
tree[rt].sum+=(tree[rt].r-tree[rt].l+)*delta;
if(tree[rt].sum<) tree[rt].sum=;
if(tree[rt].sum>(tree[rt].r-tree[rt].l+)) tree[rt].sum=(tree[rt].r-tree[rt].l+);
tree[rt].lazy=delta;
return ;
}
update_son(rt);
LL mid=(tree[rt].l+tree[rt].r)>>;
if(l<=mid) change(rt<<,l,r,delta);
if(mid+<=r) change(rt<<|,l,r,delta);
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
} inline LL query(LL rt,LL l,LL r){
if(l<=tree[rt].l&&tree[rt].r<=r){
return tree[rt].sum;
}
update_son(rt);
LL ans=;
LL mid=(tree[rt].l+tree[rt].r)>>;
if(l<=mid) ans+=query(rt<<,l,r);
if(mid+<=r) ans+=query(rt<<|,l,r);
return ans;
}
inline void ASK(int u,int v){
int tp1=top[u],tp2=top[v];
int ans=,tmpv=v;
if(u==-){//卸载
ans=query(,id[v],id[v]);
if(ans==){
ans=query(,id[v],id[v]+siz[v]-);//子树权值之和
printf("%d\n",ans);
change(,id[v],id[v]+siz[v]-,-);
}
else printf("0\n");
}
else{//安装
ans=query(,id[v],id[v]);
if(ans==){
while(tp1!=tp2){
if(dep[tp1]<dep[tp2]){
swap(tp1,tp2);
swap(u,v);
}
ans+=query(,id[tp1],id[u]);
change(,id[tp1],id[u],);
u=fa[tp1],tp1=top[u];
}
if(dep[u]>dep[v]) swap(u,v);
ans+=query(,id[u],id[v]);
ans=dep[tmpv]-ans;
change(,id[u],id[v],);
printf("%d\n",ans);
}
else printf("0\n");
}
}
int main(){
N=read();
for(int i=,u;i<=N;i++){
u=read(); u++;
to[u].push_back(i); to[i].push_back(u);
}
dfs1(,,); dfs2(,);
build(,,num);
M=read();
for(int i=,x;i<=M;i++){
scanf("%s%d",s,&x);
x++;
if(s[]=='i'){
ASK(,x);
}
else if(s[]=='u'){
ASK(-,x);
}
}
return ;
}
现再提供造数据的程序(根据需要调数据范围),给那些死都调不出来的小伙伴们。。。
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("makedata.out","w",stdout);
srand(time());
int N,M;
N=rand()%+;
cout<<N<<endl;
for(int i=;i<=N-;i++){
int to=rand()%i;
cout<<to<<" ";
}
cout<<endl;
M=rand()%+;
cout<<M<<endl;
for(int i=;i<=M;i++){
int kin=rand()%;
if(kin==) cout<<"install"<<" ";
else cout<<"uninstall"<<" ";
cout<<rand()%N<<endl;
}
return ;
}
4196: [Noi2015]软件包管理器的更多相关文章
- 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用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...
- 4196. [NOI2015]软件包管理器【树链剖分】
Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...
- 【刷题】BZOJ 4196 [Noi2015]软件包管理器
Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软件包,同时自动解决所有的依赖( ...
- [BZOJ4196][NOI2015]软件包管理器
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1040 Solved: 603[Submit][Stat ...
- [BZOJ4196][NOI2015]软件包管理器(树链剖分)
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2166 Solved: 1253[Submit][Sta ...
- [NOI2015]软件包管理器
4621 [NOI2015]软件包管理器 题目等级 : 钻石 Diamond 题目描述 Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过 ...
随机推荐
- Exchange Powershell:Get-Counter (List connections to OWA )
使用方法: Get-CASActiveUsers -server server1,server2 Get-CASMailbox | Get-CASActiveUsers $RPC = Get-Coun ...
- TIME_WAIT Accumulation and Port Exhaustion
客户端实现连接的唯一性 HTTP The Definitive Guide 4.2.7 TIME_WAIT Accumulation and Port Exhaustion TIME_WAIT por ...
- 关于一个非常非常无语的bug,与君共勉
今天,哦,不对,是昨天晚上,我花了大概四十分钟去找一个bug,结果还没找到 错误代码" $('#sendAreaInfo').citypicker('reset'); $('#sendAre ...
- intelij IDEA在启动tomcat时控制台日志乱码
1.在idea安装目录的bin下修改idea.exe.vmoptions和idea64.exe.vmoptions,添加 -Dfile.encoding=UTF-8 -Dconsole.encodin ...
- Centos locate 文件搜索命令(十一)
locate命令 locate 文件名 在后台数据库中按文件名搜索,搜索速度更快 /var/lib/mlocate #locate命令所搜索的后台数据库 updatedb 更新数据库 locate搜索 ...
- 剑指Offer——二叉搜索树的第k个结点
题目描述: 给定一颗二叉搜索树,请找出其中的第k大的结点. 例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4 分析: 二叉搜索树中序遍历就是从小到大.只 ...
- [golang]内存不断增长bytes.makeSlice
------------------------------------------ 2015.7月更新 后面发现这里其实有一个sb的问题,在于内存回收和释放. 每个http请求,都会带一个http. ...
- SpringBoot-模板渲染
模板 开发Web站点的本质,其实就是根据浏览器发起的请求(输入),生成HTML代码返回给浏览器(输出).在之前的学习中,我们已经通过文件的形式存储起来而不是直接在Java代码中生成HTML代码.另一方 ...
- Linux PHP7的openssl扩展安装
Linux环境下使用PHPmailer发送邮件时,出现如下错误: SMTP -> ERROR: Failed to connect to server: Unable to find the s ...
- 工作笔记——限定input上传文件对话框中能选取的文件的格式
原文:http://www.dengzhr.com/frontend/1059 input[file]标签的accept属性可用于指定上传文件的 MIME类型 . 例如,想要实现默认上传图片文件的代码 ...