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用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从 ...
随机推荐
- HDU Atlantis 线段树 表达区间 矩形面积相交
http://acm.hdu.edu.cn/showproblem.php?pid=1542 我的做法是把x轴的表示为线段,然后更新y 不考虑什么优化的话,开始的时候,把他们表达成线段,并按y排序,然 ...
- java emoji表情存储解决方案
1.问题产生情况 我遇到这个问题是做微信开发的时候有些有用的头像用了微信的emoji表情,然而我的mysql数据库用的编码是utf8_general_ci,就是utf-8编码,结果也就报错误了. 2. ...
- javaoo面向对象
- js实现文本框验证和实现小数的加减乘除
<script type="text/javascript"> //加法 var m=accAdd(1.22,1.22); //减法 var m1=accSub(1.2 ...
- AngularJs数据绑定原理
注 这篇博文主要是写给新手的,是给那些刚刚开始接触Angular,并且想了解数据帮定是如何工作的人.如果你已经对Angular比较了解了,那强烈建议你直接去阅读源代码. Angular用户都想知道数据 ...
- TitleBar(标题栏)的使用
github地址:https://github.com/buhuiming/BHMAndroid 使用compile 'com.bhm.sdk.bhmlibrary:BHMLibrary:1.1.2' ...
- ThreadLocal遇到线程池时, 各线程间的数据会互相干扰, 串来串去
最近遇到一个比较隐蔽而又简单地问题,在使用ThreadLocal时发现出现多个线程中值串来串去,排查一番,确定问题为线程池的问题,线程池中的线程是会重复利用的,而ThreadLocal是用线程来做Ke ...
- jquery最常用的几个方法。——可删除
jquery使用手册:http://www.eduyo.com/doc/jquery/cheatsheet.html $(this).hasClass("input_money") ...
- SSave ALAsset image to disk fast on iOS
I am using ALAsset to retrieve images like that: [[asset defaultRepresentation] fullResolutionImage] ...
- winhex与磁盘格式与 数据恢复
第一阶段: 熟悉WinHex的使用. n 熟悉磁盘工具的使用. n 利用WinHex查看物理磁盘和逻辑磁盘. n 了解WinHex中相关工具的用法. 以管理员身份运行winhex(以便之后修改) 上方 ...