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用户一定对软件包管理器不会陌生.通过软件包管理器,你可以通过一行命令安装某一个软件包,然后软件包管理器会帮助你从 ...
随机推荐
- 我的NopCommerce之旅(9): 编写Plugin实例
一.基础介绍 ——In computing, a plug-in (or plugin) is a set of software components that add specific abili ...
- postgresql 存储过程动态更新数据
-- 目标:动态更新表中数据 -- 老规矩上代码-----------------------------tablename 表名--feildname 字段名数组--feildvalue 字段值数组 ...
- 修改输入框placeholder的默认样式
一般网页中都用到input的placeholder属性,想让这个默认样式和网页保持一致,就需要重新设定样式,百度百度: :-moz-placeholder { / color: #000; opaci ...
- show processlist使用介绍
这个命令中最关键的就是state列,mysql列出的状态主要有以下几种: Checking table 正在检查数据表(这是自动的).Closing tables 正在将表中修改的数据刷新到磁盘中,同 ...
- webpack前端构建工具学习总结(三)之webpack.config.js配置文件
Webpack 在执行的时候,除了在命令行传入参数,还可以通过指定的配置文件来执行.默认情况下,会搜索当前目录的 webpack.config.js 文件,这个文件是一个 node.js 模块,返回一 ...
- tomcat配置 —— 各个目录的作用
tomcat各目录(文件)作用 tomcat-7.0.50解压版,主目录一览: 我们可以看到主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹,下面对他们分别 ...
- 关于ubuntu终端全屏的时候不能显示底部
最近在win7的电脑上装了ubuntu,也就是双系统.打算之后工作就直接进入ubuntu,减少之前win7和虚拟机之间的切换.进入ubuntu后,发现一个奇怪的问题是,在终端全屏的时候,底部总是有几行 ...
- [转]八款开源Android游戏引擎
八款开源Android游戏引擎 1.Angle Angle是一款专为Android平台设计的,敏捷且适合快速开发的2D游戏引擎,基于OpenGL ES技术开发.该引擎全部用Java代码编写,并且可以根 ...
- Xcode4删除文件后missing file警告
1.运行终端,执行命令行进入missing file目录,然后运行 svn delete nameOfMissingFile 或 git rm nameOfMissingFile 2.删除隐藏的.sv ...
- iOS 随机数(Fixed)
ios 有如下三种随机数方法: 1. srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; 2. ...