Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree
3 seconds
256 megabytes
standard input
standard output
Abendsen assigned a mission to Juliana. In this mission, Juliana has a rooted tree with nn vertices. Vertex number 11 is the root of this tree. Each vertex can be either black or white. At first, all vertices are white. Juliana is asked to process qq queries. Each query is one of three types:
- If vertex vv is white, mark it as black; otherwise, perform this operation on all direct sons of vv instead.
- Mark all vertices in the subtree of vv (including vv) as white.
- Find the color of the ii-th vertex.
An example of operation "1 1" (corresponds to the first example test). The vertices 11 and 22 are already black, so the operation goes to their sons instead.
Can you help Juliana to process all these queries?
The first line contains two integers nn and qq (2≤n≤1052≤n≤105, 1≤q≤1051≤q≤105) — the number of vertices and the number of queries.
The second line contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn (1≤pi<i1≤pi<i), where pipi means that there is an edge between vertices ii and pipi.
Each of the next qq lines contains two integers titi and vivi (1≤ti≤31≤ti≤3, 1≤vi≤n1≤vi≤n) — the type of the ii-th query and the vertex of the ii-th query.
It is guaranteed that the given graph is a tree.
For each query of type 33, print "black" if the vertex is black; otherwise, print "white".
题意:给你一颗树,树上一开始点的颜色全是白色。然后有三种操作:1.把一个白色的点v染成黑色,如果该点v本就是黑色,则对他的所有直接子节点做操作1。2.将节点v的子树全部染成白色。3.查询节点v的颜色。对于每个操作3输出其颜色。
题解:我们要使其在一个尽量平均的时间内进行求解,就需要将其改造成一个相对平衡的树,那我们就考虑用树链刨分对这颗树进行分解。我们发觉每个操作1对应使该点所在的一条重链所对应的区间黑色区间右端点后移一位,其他影响到的重链也后移一位。因此我们查询点v是否为黑色,就看从根到v点的所有链对应的区间上,任何黑色区间右端点位移位置是否超过该点v。我们对线段树维护两个值,一个是端点对应区间剩余白色点的个数leaf(可为负数,即透支后面的链的白色点数),另一个是对应区间中最靠右侧的黑色区间的右端点超出该区间右端点距离(也就是我们上文说到的对其他重链的影响长度)execr,一开始是-1因为右端点还是白色。剩余白色点的转移就是子区间白色点数相加,而第二个维护量execr(即向区间右端点后边区间的影响长度)的转移则是max(右子区间execr,左子区间execr-右子区间leaf)。这样我们查询的时候就能通过该点向后影响长度来查询该点是不是被染成黑色。>=0就是被染色了,-1则是未染色。对于操作1,直接对对应的线段树上的单点v的区间的execr++,leaf--。对于操作3,我们将根节点到节点v的所有区间连起来,算算execr是不是>-1,若是则染色了。对于操作2,我们则直接把所有对应的区间清0,即leaf为区间点数,execr为-1。为了防止v的祖先节点对v子树区间的影响,我们对v做操作3查询超出长度k,若>-1,则反向对v点做操作1(k+1)次,即execr-=(k+1),leaf+=(k+1)。
#include<bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define clr_1(x) memset(x,-1,sizeof(x))
#define INF 0x3f3f3f3f
#define LL long long
#define pb push_back
#define ls(i) (i<<1)
#define rs(i) (i<<1|1)
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
using namespace std;
const int N=2e5+;
vector<int> e[N];
int fa[N];
struct seg
{
int l,r,execr,leaf;
bool tag;
}tree[N<<];
int n,m,k,q;
int t,v;
void pushup(int i)
{
tree[i].leaf=tree[i<<].leaf+tree[i<<|].leaf;
tree[i].execr=max(tree[i<<|].execr,tree[i<<].execr-tree[i<<|].leaf);
return ;
}
void init(int i,int l,int r)
{
tree[i]=(seg){l,r,-,,false};
if(l==r)
return ;
int mid=l+r>>;
init(i<<,l,mid);
init(i<<|,mid+,r);
pushup(i);
return ;
}
int top[N],son[N],pre[N],las[N],stot[N],dep[N];
int tot;
void dfs1(int u,int f,int deep)
{
stot[u]=;
dep[u]=deep;
fa[u]=f;
for(auto p:e[u])
{
if(p==f) continue;
dfs1(p,u,deep+);
stot[u]+=stot[p];
if(son[u]== || stot[son[u]]<stot[p])
son[u]=p;
}
return ;
}
void dfs2(int u,int tp)
{
pre[u]=++tot;
top[u]=tp;
if(son[u]) dfs2(son[u],tp);
for(auto p:e[u])
{
if(p==son[u] || p==fa[u])
continue;
dfs2(p,p);
}
las[u]=tot;
return ;
}
void pushdown(int i)
{
if(tree[i].l!=tree[i].r)
{
tree[i<<].tag=tree[i<<|].tag=;
tree[i<<].leaf=tree[i<<].r-tree[i<<].l+,tree[i<<].execr=-;
tree[i<<|].leaf=tree[i<<|].r-tree[i<<|].l+,tree[i<<|].execr=-;
}
tree[i].tag=;
return ;
}
void update(int i,int l,int r,int val)
{
if(tree[i].l>=l && tree[i].r<=r)
{
if(val==INF)//都是区间修改
tree[i].leaf=tree[i].r-tree[i].l+,tree[i].execr=-,tree[i].tag=;
else
tree[i].leaf-=val,tree[i].execr+=val,tree[i].tag=;//都是单点修改
return ;
}
if(tree[i].tag) pushdown(i);
int mid=tree[i].l+tree[i].r>>;
if(l<=mid) update(i<<,l,r,val);
if(r>mid) update(i<<|,l,r,val);
pushup(i);
return ;
}
struct ret{
int execr,leaf;
};
ret operator + (const ret &a,const ret &b)
{
ret c;
c.leaf=a.leaf+b.leaf;
c.execr=max(b.execr,a.execr-b.leaf);
return c;
}
ret query(int i,int l,int r)
{
if(tree[i].l>=l && tree[i].r<=r)
return (ret){tree[i].execr,tree[i].leaf};
if(tree[i].tag) pushdown(i);
int mid=tree[i].l+tree[i].r>>;
ret retur=(ret){-INF,};
if(r>mid) retur=query(i<<|,l,r)+retur;
if(l<=mid) retur=query(i<<,l,r)+retur;
return retur;
}
int que(int x)
{
int tx=top[x];
ret retur=(ret){-INF,},per;
while(tx!=)
{
per=query(,pre[tx],pre[x]);
retur=per+retur;
x=fa[tx],tx=top[x];
}
per=query(,pre[tx],pre[x]);
retur=per+retur;
return retur.execr;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
cin>>n>>q;
for(int i=;i<=n;i++)
{
cin>>v;
e[v].pb(i);
e[i].pb(v);
}
init(,,n);
dfs1(,,);
dfs2(,);
for(int i=;i<=q;i++)
{
cin>>t>>v;
if(t==)
update(,pre[v],pre[v],);
else if(t==)
{
update(,pre[v],las[v],INF);
t=que(v);
if(t>-)
update(,pre[v],pre[v],-(t+));
}
else if(t==)
{
t=que(v);
cout<<(t>-?"black":"white")<<endl;
}
}
return ;
}
Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree的更多相关文章
- E. The Supersonic Rocket Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)
http://codeforces.com/contest/1017/problem/E 凸包模板+kmp #include <cstdio> #include <cstdlib&g ...
- Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)
第一次参加cf的比赛 有点小幸运也有点小遗憾 给自己定个小目标 1500[对啊我就是很菜qvq A. The Rank 难度:普及- n位学生 每个学生有四个分数 然鹅我们只需要知道他的分数和 按分数 ...
- Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) E. The Supersonic Rocket
这道题比赛之后被重新加了几个case,很多人现在都过不了了 算法就是先求凸包,然后判断两个凸包相等 我们可以吧凸包序列化为两点距离和角度 角度如果直接拿向量的叉积是不对的,,因为钝角和锐角的叉积有可能 ...
- 【Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) D】The Wu
[链接] 我是链接,点我呀:) [题意] 给你n个字符串放在multiset中. 这些字符串都是长度为m的01串. 然后给你q个询问 s,k 问你set中存在多少个字符串t 使得∑(t[i]==s[i ...
- Codeforces Round #502
Codeforces Round #502 C. The Phone Number 题目描述:求一个\(n\)排列,满足\(LIS+LDS\)最小 solution 枚举\(LIS\),可证明\(LD ...
- 【Codeforces Round #502 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843032.html B:https://www.cnblogs.com/myx12345/p/9843050.html ...
- Codeforces Round #195 A B C 三题合集 (Div. 2)
A 题 Vasily the Bear and Triangle 题目大意 一个等腰直角三角形 ABC,角 ACB 是直角,AC=BC,点 C 在原点,让确定 A 和 B 的坐标,使得三角形包含一个矩 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- boost::bind的使用
最近在几经波折之后,终于对于boost::bind有点理解了.对于习惯了其他语言的人来说,boost::bind是个挺神奇的东西,它可以将你的方法适配成任何其他的方法.其实这得益于c++的模板以及操作 ...
- 怎么修改oracle用户密码
在以SYSDBA身份登陆时可以修改其他用户的密码,比如: SQL> alter user user01 identified by user10;
- oracle 修改属性
alter table 表名 modify 字段名 类型; alter table 表名 modify 字段名 属性名; alter table TEST modify sbirthday not n ...
- [Ubuntu 14.04] 创建可以用于Android的WIFI热点
Ubuntu的网络管理为创建Wifi热点提供了方便,可是因为它用了ad-hoc网络,所以其创建的Wifi又不能让Android系统使用.这篇文字就是为了解决这个问题 1.Install AP-Host ...
- connect系统调用
/* * Attempt to connect to a socket with the server address. The address * is in user space so we ve ...
- 金蝶K3,名称或代码在系统中已被使用,由于数据移动,未能继续以NOLOCK方式扫描
使用金蝶K3时出现:名称或代码在系统中已被使用:错误代码:3604(E14H)source:Microsoft OLE DB provider for SQL SERVERDetail:由于数据移动, ...
- Scrapy官网程序执行示例
Windows 10家庭中文版本,Python 3.6.4,Scrapy 1.5.0, Scrapy已经安装很久了,前面也看了不少Scrapy的资料,自己尝试使其抓取微博的数据时,居然连登录页面(首页 ...
- js事件兼容处理
js封装事件处理函数,兼容ie,支持事件代理 var eventUtil = { bindEvent: function(el, type, target, callback, popgation) ...
- 制作macOS10.12系列的系统镜像文件
制作macOS10.12系列的系统镜像文件步骤,过程也比较简单,十来个命令.以10.12.6为例,首先,在苹果商店下载系统安装包APP,或者网上下载后把安装APP复制到 应用程序 文件夹. 然后打 ...
- 练习题 --- 写出5种css定位语法
写出至少5种css语法(每种语法不一样)