bzoj千题计划252:bzoj1095: [ZJOI2007]Hide 捉迷藏
http://www.lydsy.com/JudgeOnline/problem.php?id=1095
点分树+堆 请去看 http://www.cnblogs.com/TheRoadToTheGold/p/8463436.html
线段树维护括号序列
对树进行dfs,入栈时加一个左括号,出栈时加一个右括号,那么书上两点间的距离=括号序列两点间不匹配括号数
例:
树1--2--3,2为根
括号序列为 (2(3)(1))
2和1的距离 为 ()( = 1, 3和1的距离为 )( =2
具体怎么维护不想写了,去看曹钦翔的冬令营讲稿《数据结构的提炼与压缩》(p29、30)吧
#include<cstdio>
#include<iostream> #define N 100001 using namespace std; int front[N],nxt[N<<],to[N<<],tot; int id[N];
int num[N*]; bool light[N]; #define max(a,b) ((a)>(b) ? (a) : (b)) struct node
{
int a,b,dis;
int right_plus,right_minus,left_plus,left_minus; void get_val(int x)
{
a=b=;
right_plus=right_minus=left_plus=left_minus=dis=-1e7;
if(num[x]==-) b=;
else if(num[x]==-) a=;
else if(!light[num[x]]) right_plus=right_minus=left_plus=left_minus=dis=;
} node operator + (node p)
{
node k;
k.a=max(a,a+p.a-b);
k.b=max(p.b,p.b+b-p.a);
k.dis=max(max(dis,p.dis),max(right_plus+p.left_minus,right_minus+p.left_plus));
int A=a,B=b,C=p.a,D=p.b;
k.right_plus=max(p.right_plus,max(right_plus+D-C,right_minus+D+C));
k.right_minus=max(p.right_minus,right_minus+C-D);
k.left_plus=max(left_plus,max(A+B+p.left_minus,A-B+p.left_plus));
k.left_minus=max(left_minus,B-A+p.left_minus);
return k;
} }tr[N*<<]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
} void dfs(int x,int fa)
{
num[++tot]=-;
id[num[++tot]=x]=tot;
for(int i=front[x];i;i=nxt[i])
if(to[i]!=fa) dfs(to[i],x);
num[++tot]=-;
} void build(int k,int l,int r)
{
if(l==r)
{
tr[k].get_val(l);
return;
}
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
tr[k]=tr[k<<]+tr[k<<|];
} void change(int k,int l,int r,int pos)
{
if(l==r)
{
tr[k].get_val(l);
return;
}
int mid=l+r>>;
if(pos<=mid) change(k<<,l,mid,pos);
else change(k<<|,mid+,r,pos);
tr[k]=tr[k<<]+tr[k<<|];
} int main()
{
int n,u,v;
read(n);
for(int i=;i<n;++i)
{
read(u); read(v);
add(u,v);
}
tot=;
dfs(,);
build(,,tot);
int m; char s[];
read(m);
int cnt=n;
while(m--)
{
scanf("%s",s);
if(s[]=='G')
{
if(cnt<=) printf("%d\n",cnt-);
else printf("%d\n",tr[].dis);
}
else
{
read(u);
if(light[u]) cnt++;
else cnt--;
light[u]^=;
change(,,tot,id[u]);
}
}
return ;
}
bzoj千题计划252:bzoj1095: [ZJOI2007]Hide 捉迷藏的更多相关文章
- 动态点分治:Bzoj1095: [ZJOI2007]Hide 捉迷藏
简介 这是我自己的一点理解,可能写的不好 点分治都学过吧.. 点分治每次找重心把树重新按重心的深度重建成了一棵新的树,称为分治树 这个树最多有log层... 动态点分治:记录下每个重心的上一层重心,这 ...
- [bzoj1095][ZJOI2007]Hide 捉迷藏 点分树,动态点分治
[bzoj1095][ZJOI2007]Hide 捉迷藏 2015年4月20日7,8876 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiaji ...
- bzoj千题计划300:bzoj4823: [Cqoi2017]老C的方块
http://www.lydsy.com/JudgeOnline/problem.php?id=4823 讨厌的形状就是四联通图 且左右各连一个方块 那么破坏所有满足条件的四联通就好了 按上图方式染色 ...
- bzoj千题计划245:bzoj1095: [ZJOI2007]Hide 捉迷藏
http://www.lydsy.com/JudgeOnline/problem.php?id=1095 查询最远点对,带修改 显然可以用动态点分治 对于每个点,维护两个堆 堆q1[x] 维护 点分树 ...
- bzoj千题计划163:bzoj1060: [ZJOI2007]时态同步
http://www.lydsy.com/JudgeOnline/problem.php?id=1060 以激发器所在节点为根 终止节点一定是叶节点 记录点的子树内最深的终止节点 然后从根往下使用道具 ...
- bzoj千题计划196:bzoj4826: [Hnoi2017]影魔
http://www.lydsy.com/JudgeOnline/problem.php?id=4826 吐槽一下bzoj这道题的排版是真丑... 我还是粘洛谷的题面吧... 提供p1的攻击力:i,j ...
- bzoj千题计划280:bzoj4592: [Shoi2015]脑洞治疗仪
http://www.lydsy.com/JudgeOnline/problem.php?id=4592 注意操作1 先挖再补,就是补的范围可以包含挖的范围 SHOI2015 的题 略水啊(逃) #i ...
- bzoj千题计划177:bzoj1858: [Scoi2010]序列操作
http://www.lydsy.com/JudgeOnline/problem.php?id=1858 2018 自己写的第1题,一遍过 ^_^ 元旦快乐 #include<cstdio> ...
- bzoj千题计划317:bzoj4650: [Noi2016]优秀的拆分(后缀数组+差分)
https://www.lydsy.com/JudgeOnline/problem.php?id=4650 如果能够预处理出 suf[i] 以i结尾的形式为AA的子串个数 pre[i] 以i开头的形式 ...
随机推荐
- Java中关于nextInt()、next()和nextLine()的理解
先看解释: nextInt(): it only reads the int value, nextInt() places the cursor in the same line after rea ...
- 论文笔记(1):From Image-level to Pixel-level Labeling with Convolutional Networks
文章采用了多实例学习(MIL)机制构建图像标签同像素语义的关联 . 该方法的训练样本包含了70 万张来自ImageNet的图片,但其语义分割的性能很大程度上依赖于复杂的后处理过程,主要包括图像级语义的 ...
- 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码
前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...
- Spark2.1.0官方文档
Spark 概述 Apache Spark是一个快速和通用的集群计算系统.它提供Java,scala,Python.R语言的APIs,以及支持一般执行图形的优化引擎. 它还支持一组丰富的高级工具,包括 ...
- Node与apidoc的邂逅——NodeJS Restful 的API文档生成
作为后台根据需求文档开发完成接口后,交付给前台(angular vue等)做开发,不可能让前台每个接口调用都去查看你的后台代码一点点查找.前台开发若不懂你的代码呢?让他一个接口一个接口去问你怎么调用, ...
- 常用JS小知识汇总
1 上传图片:html代码 <input id="image" type='file' name='myFile' size='15' onchange="show ...
- 超实用的查看磁盘的命令:lsblk
- 什么是IPFS?(三)
前两篇介绍了IPFS的基本概念, 那么IPFS都可以用来做什么? 如果你认为仅仅是CDN, 永久的web? Are you kidding me ? IPFS可是要 --改变世界的... --变世界的 ...
- Laravel框架中的make方法详解
为什么网上已经有这么多的介绍Laravel的执行流程了,Laravel的容器详解了,Laravel的特性了,Laravel的启动过程了之类的文章,我还要来再分享呢? 因为,每个人的思维方式和方向是不一 ...
- Java集合详解及List源码分析
对于数组我们应该很熟悉,一个数组在内存中总是一块连续的存储空间,数组的创建使用new关键字,数组是引用类型的数据,一旦第一个元素的位置确定,那么后面的元素位置也就确定了,数组有一个最大的局限就是数组一 ...