CF1017G The Tree
/*
这是什么神仙题目QAQ
首先考虑在序列上的问题
先不考虑修改成白色, 一个白点能r被染成黑色 意味着能够找到一个l使得在l-r中的操作1次数大于等于
r - l + 1
我们把初始值覆盖成-1就相当于单点+1求最大后缀和了
然后覆盖成白色, 相当于在这个点减去一些值使得最后到达他的最大后缀是-1, 然后对子树进行覆盖
上树同理 树剖即可
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define pii pair<int, int>
#include<cmath>
#define ll long long
#define M 100010
#define mmp make_pair
using namespace std;
int read() {
int nm = 0, f = 1;
char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
return nm * f;
}
int fa[M], sz[M], son[M], dfn[M], dft, top[M], deep[M], n, q;
vector<int> to[M];
#define ls now << 1
#define rs now << 1 | 1
#define lson l, mid, now << 1
#define rson mid + 1, r, now << 1 | 1
void dfs(int now, int f) {
deep[now] = deep[f] + 1;
sz[now] = 1;
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
dfs(vj, now);
if(sz[son[now]] < sz[vj]) son[now] = vj;
sz[now] += sz[vj];
}
}
void dfs(int now) {
dfn[now] = ++dft;
if(son[now]) {
top[son[now]] = top[now];
dfs(son[now]);
}
for(int i = 0; i < to[now].size(); i++) {
int vj = to[now][i];
if(vj == son[now]) continue;
top[vj] = vj;
dfs(vj);
}
}
int len[M << 2], mx[M << 2], s[M << 2], laz[M << 2];
void pushup(int now) {
mx[now] = max(mx[rs], mx[ls] + s[rs]);
s[now] = s[ls] + s[rs];
}
void add(int now) {
laz[now] = 1;
mx[now] = -1, s[now] = -len[now];
}
void pushdown(int now) {
if(!laz[now]) return;
add(ls);
add(rs);
laz[now] = 0;
}
void build(int l, int r, int now) {
len[now] = r - l + 1;
if(l == r) {
mx[now] = s[now] = -1;
return;
}
int mid = (l + r) >> 1;
build(lson), build(rson);
pushup(now);
}
void modify(int l, int r, int now, int pl, int v) {
if(l > pl || r < pl) return;
if(l == r) {
mx[now] += v, s[now] += v;
return;
}
pushdown(now);
int mid = (l + r) >> 1;
modify(lson, pl, v), modify(rson, pl, v);
pushup(now);
}
void cover(int l, int r, int now, int ln, int rn) {
if(l > rn || r < ln) return;
if(l >= ln && r <= rn) {
add(now);
return;
}
pushdown(now);
int mid = (l + r) >> 1;
cover(lson, ln, rn), cover(rson, ln, rn);
pushup(now);
}
pair<int,int> biao, tmp;
pii operator + (pii a, pii b) {
return mmp(max(b.first, a.first + b.second), a.second + b.second);
}
pii que(int l, int r, int now, int ln, int rn) {
if(l >= ln && r <= rn) return mmp(mx[now], s[now]);
int mid = (l + r) >> 1;
pushdown(now);
if(rn <= mid) return que(lson, ln, rn);
if(ln > mid) return que(rson, ln, rn);
return que(lson, ln, rn) + que(rson, ln, rn);
}
int query(int x) {
tmp = biao;
for(; x; x = fa[top[x]]) {
tmp = que(1, n, 1, dfn[top[x]], dfn[x]) + tmp;
}
return tmp.first;
}
int main() {
biao = mmp(-0x3e3e3e3e, 0);
n = read(), q = read();
for(int i = 2; i <= n; i++) fa[i] = read(), to[fa[i]].push_back(i);
dfs(1, 1);
top[1] = 1;
dfs(1);
build(1, n, 1);
while(q--) {
int op = read(), x = read();
if(op == 1) {
modify(1, n, 1, dfn[x], 1);
}
if(op == 2) {
int t = query(x);
modify(1, n, 1, dfn[x], -(t + 1));
if(sz[x] > 1) cover(1, n, 1, dfn[x] + 1, dfn[x] + sz[x] - 1);
}
if(op == 3) {
puts(query(x) >= 0 ? "black" : "white");
}
}
return 0;
}
CF1017G The Tree的更多相关文章
- CF1017G The Tree 树链剖分
CF1017G The Tree LG传送门 树链剖分好题. 乍一看还以为是道沙比题,然后发现修改操作有点不一样. 但是如果你对基本操作还不太熟练,可以看看我的树链剖分总结 有三个操作: 从一个点往下 ...
- CF1017G——The Tree
传送门:QAQQAQ 题意:给你一棵树,有三种操作,设所有点本来未感染 1:感染节点i,若i被二次感染,则感染i的儿子(若儿子也被感染,则感染孙子,直到到底或者感染了健康点) 2:使i子树全部健康 3 ...
- [CF1017G]The Tree[树链剖分+线段树]
题意 给一棵一开始 \(n\) 个点全是白色的树,以 \(1\) 为根,支持三种操作: 1.将某一个点变黑,如果已经是黑色则该操作对所有儿子生效. 2.将一棵子树改成白色. 3.询问某个点的颜色. \ ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
随机推荐
- 1. docker 在 macOS 中的架构 2. 在macOS系统中,docker pull 下来的镜像存储在哪里?
docker 在 macOS 中的架构: 在macOS中,docker的实现跟在其它Linux系统中略有不同,在其它Linux系统中,操作系统本身就是docker容器的宿主机,docker镜像都是直接 ...
- TypeScript 之 NPM包的类型
https://m.runoob.com/manual/gitbook/TypeScript/_book/doc/handbook/Typings%20for%20NPM%20Packages.htm ...
- java JVM JRE JDK三者之间的关系
JDK在包含JRE之外,提供了开发Java应用的各种工具,比如编译器和调试器. JRE包括JVM和JAVA核心类库和支持文件,是Java的运行平台. JVM是JRE的一部分,Java虚拟机的主要工作是 ...
- 找出n个数中重复最多的10个数
题目很清晰,直接上python代码 import pandas as pd import copy class BenchMark: def __init__(self): self.MIN = 10 ...
- NLB网路负载均衡管理
相对于ARR来说,ARR算是应用级别的负载均衡方案,而NLB则是服务器级别的负载均衡方案.ARR只能做请求入口的消息分发服务,这样如果我们的消息分发服务器给挂掉,那么做再多的应用服务集群也都枉然. A ...
- 找进程的窗口Handle
Process[] ProcessList = Process.GetProcessesByName("mspaint");//画图板 IntPtr test = ProcessL ...
- 使用influxQL进行数据检索(说明)
非官方:InfluxDB 基本命令操作文档 原地址:https://docs.influxdata.com/influxdb/v1.6/query_language/data_exploration/ ...
- 黄聪:PHP调试显示所有错误信息
ini_set('display_errors',1); //错误信息 ini_set('display_startup_errors',1); //php启动错误信息 error_reporting ...
- Building the Unstructured Data Warehouse: Architecture, Analysis, and Design
Building the Unstructured Data Warehouse: Architecture, Analysis, and Design earn essential techniqu ...
- Tomcat实战-调优方案
Tomcat的默认配置,性能并不是最优的,可以通过优化tomcat以此来提高网站的并发能力.提高Tomcat的性能可以分为两个方向. 服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力有决定 ...