luogu2146 [NOI2015]软件包管理器
安装就把根节点到它全设为 1
删除就把以它为根的子树全设为 0
记得标记初始化为-1,因为标记是 0 的情况也是要处理的。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, m, uu, hea[100005], cnt, dep[100005], fa[100005], son[100005], siz[100005];
int top[100005], idx[100005], qwq;
char ss[15];
struct Edge{
int too, nxt;
}edge[200005];
struct SGT{
int sum[400005], tag[400005];
void build(int o, int l, int r){
tag[o] = -1;
if(l==r) sum[o] = 0;
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(l<=mid) build(lson, l, mid);
if(mid<r) build(rson, mid+1, r);
sum[o] = sum[lson] + sum[rson];
}
}
void pushDown(int o, int l, int r, int lson, int rson, int mid){
tag[lson] = tag[o];
tag[rson] = tag[o];
sum[lson] = (mid-l+1) * tag[o];
sum[rson] = (r-mid) * tag[o];
tag[o] = -1;
}
void update(int o, int l, int r, int x, int y, int k){
if(l>=x && r<=y){
sum[o] = (r-l+1) * k;
tag[o] = k;
}
else{
int mid=(l+r)>>1;
int lson=o<<1;
int rson=lson|1;
if(tag[o]!=-1) pushDown(o, l, r, lson, rson, mid);
if(x<=mid) update(lson, l, mid, x, y, k);
if(mid<y) update(rson, mid+1, r, x, y, k);
sum[o] = sum[lson] + sum[rson];
}
}
}sgt;
void add_edge(int fro, int too){
edge[++cnt].nxt = hea[fro];
edge[cnt].too = too;
hea[fro] = cnt;
}
void dfs1(int x, int f){
dep[x] = dep[f] + 1;
fa[x] = f;
siz[x] = 1;
int maxSon=-1;
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=f){
dfs1(t, x);
siz[x] += siz[t];
if(siz[t]>maxSon) maxSon = siz[t], son[x] = t;
}
}
}
void dfs2(int x, int topf){
top[x] = topf;
idx[x] = ++qwq;
if(!son[x]) return ;
dfs2(son[x], topf);
for(int i=hea[x]; i; i=edge[i].nxt){
int t=edge[i].too;
if(t!=fa[x] && t!=son[x])
dfs2(t, t);
}
}
void upd(int xx, int yy){
while(top[xx]!=top[yy]){
if(dep[top[xx]]<dep[top[yy]]) swap(xx, yy);
sgt.update(1, 1, n, idx[top[xx]], idx[xx], 1);
xx = fa[top[xx]];
}
if(dep[xx]>dep[yy]) swap(xx, yy);
sgt.update(1, 1, n, idx[xx], idx[yy], 1);
}
int main(){
cin>>n;
for(int i=2; i<=n; i++){
scanf("%d", &uu);
add_edge(i, uu+1);
add_edge(uu+1, i);
}
dfs1(1, 0);
dfs2(1, 1);
sgt.build(1, 1, n);
cin>>m;
while(m--){
scanf("%s %d", ss, &uu);
uu++;
if(ss[0]=='i'){
int tmp=sgt.sum[1];
upd(1, uu);
printf("%d\n", sgt.sum[1]-tmp);
}
else{
int tmp=sgt.sum[1];
sgt.update(1, 1, n, idx[uu], idx[uu]+siz[uu]-1, 0);
printf("%d\n", tmp-sgt.sum[1]);
}
}
return 0;
}
luogu2146 [NOI2015]软件包管理器的更多相关文章
- [题解向] Luogu2146[NOI2015]软件包管理器
#\(\mathcal{\color{red}{Description}}\) \(Link\) 一道\(zz\)的树剖题\(qwq\). #\(\mathcal{\color{red}{Soluti ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- [BZOJ4196][NOI2015]软件包管理器
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1040 Solved: 603[Submit][Stat ...
- Bzoj 4196: [Noi2015]软件包管理器 树链剖分
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 721 Solved: 419[Submit][Statu ...
- [NOI2015]软件包管理器
4621 [NOI2015]软件包管理器 题目等级 : 钻石 Diamond 题目描述 Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过 ...
- BZOJ_4196_[Noi2015]软件包管理器_树链剖分
BZOJ_4196_[Noi2015]软件包管理器_树链剖分 题意: Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助 ...
- bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2852 Solved: 1668[Submit][Sta ...
- 洛谷 P2146 [NOI2015]软件包管理器 解题报告
P2146 [NOI2015]软件包管理器 题目描述 Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从软件源下载软 ...
- 【BZOJ4196】[Noi2015]软件包管理器 树链剖分
[Noi2015]软件包管理器 树链剖分 Description Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从 ...
随机推荐
- Redis特点
内存存储,速度极快. 支持的数据类型多,相比较其他的Nosql. 键:字符串 值的六种数据结构:字符串,列表,散列,集合,有序集合,HyperLogLog 附加功能强大
- 状态模式和php实现
状态模式: 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类.其别名为状态对象(Objects for States),状态模式是一种对象行为型模式. 模式分析: 在很多情况下, ...
- iOS组件化开发· 什么是组件化
越来越多公司,开始了组件化,你还要等到什么时候...... 说到开发模式,我们最熟知的开发模式 MVC 或者最近比较热门的MVVM.但是我今天说的组件化的开发,其实MVC不是一类的.它其实是····· ...
- spring @RequestBody 和 @RequestParams 同时使用
@RequestBody 和 @RequestParams 是可以同时使用的. @RequestBody 接受的数据类型是 content-type:"application/json&qu ...
- MediaRecord一些使用记录
今天学习了MediaRecord的使用,第一次使用做个记录. MediaRecord作用是声音录制,使用步骤如下: 1.新建出音频文件代码如下: 先创建出用于存储音频文件 File dir = new ...
- 关于搭建系统直播和Thinkphp的杂谈(持续更新)
Q:Access denied for user 'root'@'localhost' 错误 A:第一种:配置文件中把数据库的用户名密码再改一遍,把runtime里的文件删除 第二种:修改syste ...
- sqlite总结1
I Shell下命令行程序CLP I .help II 命令的简写 .e = .quit .s .h = .help II 数据库管理 A 创建数据库 1 CREATE TABLE id_name(i ...
- Sqlserver 2012 Always on技术
使用了Sqlserver 2012 Always on技术后,假如采用的配置是默认配置,会出现Primary server CPU很高的情况发生,比如默认配置如下: 需要自定义来解决这个问题. 我们先 ...
- codevs 4165 高精度求阶乘
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 白银 Silver 题目描述 Description 用高精度计算出S=n! 其中"!"表示阶乘,例如:5!= ...
- window10系统安装Ubuntu18.04系统
写这篇博客整理一下使用虚拟机安装Ubuntu系统,一般常用的虚拟机有VMware以及VirtualBox.鉴于方便,博主用的是virtualbox,虽然不是很美观,但简洁,且完全免费,且不需要在自己配 ...