神做法…%dalao,写的超详细 konjac的博客.

如果觉得上面链接的代码不够优秀好看,欢迎回来看本蒟蒻代码…

CODE WITH ANNOTATION

代码中−6-6−6表示左括号’[’,用−9-9−9表示右括号’]’.

emmmm…

#include<bits/stdc++.h>
using namespace std;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
char ch; int flg = 1; for(;!isdigit(ch=getc());)if(ch=='-')flg=-flg;
for(res=ch-'0';isdigit(ch=getc());res=res*10+ch-'0'); res*=flg;
}
typedef long long LL; const int MAXN = 100005;
const int MAXV = 300005;
const int INF = 0x3f3f3f3f;
int n, q, tot, black, dfn[MAXN], fir[MAXN], cnt, seq[MAXV]; bool col[MAXN];
struct edge { int to, nxt; }e[MAXN<<1];
inline void add(int u, int v) {
e[++cnt] = (edge) { v, fir[u] }, fir[u] = cnt;
e[++cnt] = (edge) { u, fir[v] }, fir[v] = cnt;
}
void dfs(int u, int ff) {
seq[++tot] = -6; //[
seq[dfn[u]=++tot] = u;
for(int i = fir[u]; i; i = e[i].nxt)
if(e[i].to != ff) dfs(e[i].to, u);
seq[++tot] = -9; //]
}
inline void chkmax(int &x, int y) { if(y > x) x = y; };
inline int max(int x, int y, int z) { return max(max(x, y), z); }
struct seg { //维护形如"]]]]][["的串
int L, R, dis; //L:cnt(']'), R:cnt(']')
int suf_plus, suf_minus; //suffix : max(L + R), max(L - R) 前缀最大值
int pre_plus, pre_minus; //prefix : max(R + L), max(R - L) 后缀最大值
inline void init(int i) {
dis = -INF;
L = (seq[i] == -9); //']'
R = (seq[i] == -6); //'['
if(seq[i] > 0 && col[seq[i]]) suf_plus = suf_minus = pre_plus = pre_minus = 0;
else suf_plus = suf_minus = pre_plus = pre_minus = -INF;
}
inline friend seg operator +(const seg ls, const seg rs) {
//MAIN IDEA: use "max()" to avoid category talk(用各种姿势的max避免分类讨论)
int a = ls.L, b = ls.R, c = rs.L, d = rs.R; seg re; re.dis = max(ls.dis, rs.dis);
chkmax(re.dis, ls.suf_plus + rs.pre_minus);
chkmax(re.dis, ls.suf_minus + rs.pre_plus); // a] <-> [d (a and d are symmetrical(对称))
// [b <-> c] (b and c are symmetrical)
re.suf_plus = max(ls.suf_plus - c + d, ls.suf_minus + c + d, rs.suf_plus);
//前两个位置取max就避免了分类讨论,本来是要分(1.b>=c 2.b<c)两种情况考虑的.
//这样取max巧妙地避免写很多if(但也很有可能写爆)
re.pre_plus = max(rs.pre_plus - b + a, rs.pre_minus + b + a, ls.pre_plus); re.suf_minus = max(ls.suf_minus + c - d, rs.suf_minus);
re.pre_minus = max(rs.pre_minus + b - a, ls.pre_minus); if(b >= c) re.L = a, re.R = d + b-c;
else re.L = a + c-b, re.R = d; return re;
}
}t[MAXV<<2];
inline void upd(int i) { t[i] = t[i<<1] + t[i<<1|1]; } //简洁的update 2333
void build(int i, int l, int r) {
if(l == r) { t[i].init(l); return; }
int mid = (l + r) >> 1;
build(i<<1, l, mid);
build(i<<1|1, mid+1, r);
upd(i);
}
void modify(int i, int l, int r, int x) {
if(l == r) { t[i].init(l); return; }
int mid = (l + r) >> 1;
if(x <= mid) modify(i<<1, l, mid, x);
else modify(i<<1|1, mid+1, r, x);
upd(i);
}
int main() {
read(n);
for(int x, y, i = 1; i < n; ++i)
read(x), read(y), add(x, y);
dfs(1, 0);
for(int i = 1; i <= n; ++i) col[i] = 1, ++black;
build(1, 1, tot);
read(q); char s; int x;
while(q--) {
while(!isalpha(s=getc()));
if(s == 'G') {
if(!black) puts("-1");
else if(black == 1) puts("0");
else printf("%d\n", t[1].dis);
}
else {
read(x);
if(!col[x]) ++black;
else --black;
col[x] ^= 1;
modify(1, 1, tot, dfn[x]);
}
}
}

[BZOJ 1095] [ZJOI2007]Hide 捉迷藏——线段树+括号序列(强..)的更多相关文章

  1. BZOJ 1095: [ZJOI2007]Hide 捉迷藏(线段树维护括号序列)

    这个嘛= =链剖貌似可行,不过好像代码长度很长,懒得打(其实是自己太弱了QAQ)百度了一下才知道有一种高大上的叫括号序列的东西= = 岛娘真是太厉害了,先丢链接:http://www.shuizilo ...

  2. [bzoj1095][ZJOI2007]Hide 捉迷藏——线段树+括号序列

    题目大意 给定一棵所有点初始值为黑的无权树,你需要支援两种操作: 把一个点的颜色反转 统计最远黑色点对. 题解 本题是一个树上的结构.对于树上的结构,我们可以采用点分治.树链剖分等方法处理,这个题用了 ...

  3. 洛谷.4115.Qtree4/BZOJ.1095.[ZJOI2007]Hide捉迷藏(动态点分治 Heap)

    题目链接 洛谷 SPOJ BZOJ1095(简化版) 将每次Solve的重心root连起来,会形成一个深度为logn的树,就叫它点分树吧.. 我们对每个root维护两个东西: 它管辖的子树中所有白点到 ...

  4. bzoj 1095 [ZJOI2007]Hide 捉迷藏(括号序列+线段树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1095 [题意] 给定一棵树,树上颜色或白或黑而且可以更改,多个询问求最远黑点之间的距离 ...

  5. BZOJ.1095.[ZJOI2007]捉迷藏(线段树 括号序列)

    BZOJ 洛谷 对树DFS得到括号序列.比如这样一个括号序列:[A[B[E][F[H][I]]][C][D[G]]]. 那比如\(D,E\)间的最短距离,就是将\(D,E\)间的括号序列取出:][[] ...

  6. bzoj1095: [ZJOI2007]Hide 捉迷藏 线段树维护括号序列 点分治 链分治

    这题真是十分难写啊 不管是点分治还是括号序列都有一堆细节.. 点分治:时空复杂度$O(n\log^2n)$,常数巨大 主要就是3个堆的初始状态 C堆:每个节点一个,为子树中的点到它父亲的距离的堆. B ...

  7. 【刷题】BZOJ 1095 [ZJOI2007]Hide 捉迷藏

    Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩 捉迷藏游戏.他们的家很大且构造很奇特,由N个屋子和N-1条 ...

  8. BZOJ 1095: [ZJOI2007]Hide 捉迷藏

    Description 一棵树,支持两个操作,修改一个点的颜色,问树上最远的两个白点距离. Sol 动态点分治. 动态点分治就是将每个重心连接起来,形成一个跟线段树类似的结构,当然它不是二叉的... ...

  9. [ZJOI2007]捉迷藏 (线段树,括号序列)

    大意: 给定树, 要求维护一个点集, 支持删点添点, 询问点集直径. 本题做法比较多. 一个显然的做法是, 线段树维护区间直径, 然后根据点集直径的性质, 合并后直径端点一定是四个端点其中两个, 枚举 ...

随机推荐

  1. 2019icpc南京网络赛

    B. super_log(扩展欧拉函数) 题意:求aa...(b个a)模M的值. 思路:递归用欧拉函数求解,我们知道欧拉降幂公式: 如果讨论b和φ(p)的关系会很麻烦,网上证明了一种精妙的方法,只需重 ...

  2. 【转帖】k8s-kubectl命令大全

    https://www.cnblogs.com/fuyuteng/p/9458282.html 学习一下 Kubectl命令行管理对象 类型 命令 描述 基础命令 create 通过文件名或标准输入创 ...

  3. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  4. 文件操作:w,w+,r,r+,a,wb,rb

    1.文件操作是什么? 操作文件: f = open("文件路径",mode="模式",encoding="编码") open() # 调用操 ...

  5. oracle修改TNSLSNR的端口

    oracle 服务一启动 TNSLSNR.exe 会占用8080端口,这时,如果我们其他程序需要使用8080端口就会比较麻烦,所以需要改一下端口: 用dba账户登录 CMD>sqlplus sy ...

  6. [经验分享] Docker网络解决方案-Weave部署记录

    前面说到了Flannel的部署,今天这里说下Docker跨主机容器间网络通信的另一个工具Weave的使用.当容器分布在多个不同的主机上时,这些容器之间的相互通信变得复杂起来.容器在不同主机之间都使用的 ...

  7. 关于VSS(Volume Shadow Copy Service)一

    在开发windows VSS应用程序时 我们应该先下载相关SDK,微软描述如下 When developing your own VSS application, you should observe ...

  8. 高并发之nginx限制

    Nginx限速模块分为哪几种?按请求速率限速的burst和nodelay参数是什么意思?漏桶算法和令牌桶算法究竟有什么不同?本文将带你一探究竟. 我们会通过一些简单的示例展示Nginx限速限流模块是如 ...

  9. 一种移动端position:absolute布局:

    一种移动端position:absolute布局:   1.他父级不需要加上 position:relative; 如果父级不是不是body,则加position:absolute; 2.红色加量部分 ...

  10. MYSQL 删除语句(数据)

    删除数据(DELETE)     如果你失忆了,希望你能想起曾经为了追求梦想的你.   数据库存储数据,总会有一些垃圾数据,也会有一些不需要用的数据了,这些情况下,我们就可以删除这些数据,释放出一定的 ...