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用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从 ...
随机推荐
- prerender-spa-plugin预处理vue项目实践
由于公司想要把商城做由之前的php和前端混合开发改版为前后端分离,所以拿现在手上的vue项目来实践一下 https://github.com/chrisvfritz/prerender-spa-plu ...
- mint 有线网络未管理的解决
sudo gedit /etc/NetworkManager/NetworkManager.conf 修改managed=true 参考链接:http://forum.ubuntu.org.cn/vi ...
- 从零开始利用vue-cli搭建简单音乐网站(三)
1.利用router-link在组件之间传递数据 如上图,MainPage.vue中主要有8个推荐曲目数据,主要实现方式是建立好主页面模板,然后用v-for循环获取返回的music对象,然后分别绑定曲 ...
- {g2o}Installation Notes:ccmake
main reference: http://www.cnblogs.com/gaoxiang12/p/3776107.html "注意libqglviewer-qt4-dev只在ubunt ...
- JavaScript_1_简介
1. JavaScript属于客户端脚本语言 2. JavaScript用来改进网页设计.验证表单.检测浏览器.创建cookies,以及更多的应用 a. 是为HTML设计者提供的一种编程工具 b. 可 ...
- Session 机制和 Cookie 机制
Session 机制和 Cookie 机制 HTTP协议是无状态的, 而Cookie和Session都是在无状态的基础上希望实现有状态的效果, 两者是在客户端或者是服务端使用缓存等手段来实现状态的维护 ...
- 分布式文件系统ceph介绍
ceph哲学思想 1. 每个组件必须支持扩展 2.不存在单点故障 3.解决方案必须是基于软件的.开源的.适应能力强 4.任何可能的一切必须自我管理 存在的意义:帮助企业摆脱昂贵的专属硬件 ceph目标 ...
- python基础一 day10(1)
要背的:
- python基础一 day9 函数升阶(3)
局部命名空间一般之间是独立,局部命名空间是调用函数时生成的函数的名字指向它所在的地址局部不会对全局产生影响,除非加global.# def max(a,b):# return a if a>b ...
- Asp.Net Core 进阶(二) —— 集成Log4net
Asp.Net Core 支持适用于各种内置日志记录API,同时也支持其他第三方日志记录.在我们新建项目后,在Program 文件入口调用了CreateDefaultBuilder,该操作默认将添加以 ...