Codeforces 1149C 线段树 LCA
题意:给你一个括号序列,这个括号序列将确定一颗二叉树。有q次询问,每次询问输出这颗树的直径。
思路:https://blog.csdn.net/Huah_2018/article/details/89788074
代码:
#include <bits/stdc++.h>
#define ls (x << 1)
#define rs ((x << 1) | 1)
using namespace std;
const int maxn = 200010;
char s[maxn];
struct SegmentTree {
int mx, mi, lmx, rmx, sum, ans;
void init(int x) {
mx = x, mi = x, lmx = -x, rmx = -x, sum = x, ans = 0;
};
};
SegmentTree tr[maxn * 4];
void pushup(int x) {
int k = x;
tr[x].mx = max(tr[ls].mx, tr[rs].mx + tr[ls].sum);
tr[x].mi = min(tr[ls].mi, tr[rs].mi + tr[ls].sum);
tr[x].lmx = max(tr[ls].lmx, tr[rs].lmx - tr[ls].sum);
tr[x].lmx = max(tr[x].lmx, tr[ls].mx - 2 * (tr[rs].mi + tr[ls].sum));
tr[x].rmx = max(tr[ls].rmx, tr[rs].rmx - tr[ls].sum);
tr[x].rmx = max(tr[x].rmx, tr[rs].mx - 2 * tr[ls].mi + tr[ls].sum);
tr[x].sum = tr[ls].sum + tr[rs].sum;
tr[x].ans = max(tr[ls].ans, tr[rs].ans);
tr[x].ans = max(tr[x].ans, max(tr[ls].lmx + tr[rs].mx + tr[ls].sum, tr[rs].rmx + tr[ls].mx - tr[ls].sum));
}
void build(int x, int l, int r) {
if(l == r) {
tr[x].init(s[l] == '(' ? 1 : -1);
return;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
pushup(x);
}
void update(int x, int l, int r, int pos) {
if(l == r) {
tr[x].init(s[l] == '(' ? 1 : -1);
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(ls, l, mid, pos);
else update(rs, mid + 1, r, pos);
pushup(x);
}
int main() {
int n, T;
scanf("%d%d", &n, &T);
n = 2 * n - 2;
int u, v;
scanf("%s",s + 1);
build(1, 1, n);
printf("%d\n", tr[1].ans);
while(T--) {
scanf("%d%d", &u, &v);
swap(s[u], s[v]);
update(1, 1, n, u);
update(1, 1, n, v);
printf("%d\n", tr[1].ans);
}
}
Codeforces 1149C 线段树 LCA的更多相关文章
- Tree Generator™ CodeForces - 1149C (线段树,括号序列)
大意: 给定括号序列, 每次询问交换两个括号, 求括号树的直径. 用[ZJOI2007]捉迷藏的方法维护即可. #include <iostream> #include <algor ...
- 51 nod 1766 树上的最远点对(线段树+lca)
1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...
- csu 1798(树上最远点对,线段树+lca)
1798: 小Z的城市 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 60 Solved: 16[Submit][Status][Web Board] ...
- BZOJ 2588: Spoj 10628. Count on a tree-可持久化线段树+LCA(点权)(树上的操作) 无语(为什么我的LCA的板子不对)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 9280 Solved: 2421 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- 【Codeforces 1083C】Max Mex(线段树 & LCA)
Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...
- codeforces 916E Jamie and Tree dfs序列化+线段树+LCA
E. Jamie and Tree time limit per test 2.5 seconds memory limit per test 256 megabytes input standard ...
- HDU5266---pog loves szh III (线段树+LCA)
题意:N个点的有向树, Q次询问, 每次询问区间[L, R]内所有点的LCA. 大致做法:线段树每个点保存它的孩子的LCA值, 对于每一次询问只需要 在线段树查询即可. #include <bi ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
随机推荐
- 2018-5-26-Latex-去掉行号
title author date CreateTime categories Latex 去掉行号 lindexi 2018-05-26 10:32:25 +0800 2018-2-13 17:23 ...
- 作业(二)—python实现wc命令
Gitee地址:https://gitee.com/c1e4r/word-count(为什么老师不让我们用github) 0x00 前言 好久没发博客了,感觉自己的学习是有点偷懒了.这篇博客也是应专业 ...
- ubuntu服务器端使用无界面selenium+ chrome + headless
本来想直接用Ubuntu系统里面的firefox来实现selenium自动操作签到的,但是总是出各种问题.没办法,改为Chrome.参考:Ubuntu 线上无界面服务器 使用selenium chro ...
- Codeforces 1188D Make Equal DP
题意:给你个序列,你可以给某个数加上2的幂次,问最少多少次可以让所有的数相等. 思路(官方题解):我们先给序列排序,假设bit(c)为c的二进制数中1的个数,假设所有的数最后都成为了x, 显然x &g ...
- Codeforces 360E 贪心 最短路
题意及思路:https://blog.csdn.net/huanghongxun/article/details/49846927 在假设所有边都是最大值的情况下,如果第一个人能比第二个人先到,那就缩 ...
- java命令-jstat/ javap
jstat命令对应用程序资源和性能进行实时监控 常用参数列举如下: 1. jstat -class pid 显示加载class的数量.所占空间.所耗时间等信息 2.jstat -compiler pi ...
- 工具类Collections、Arrays(传入的是数组)
Collections类: 1. Collections.sort(list) //对list集合进行排序前提是 list里面存储的对象已经实现了 comparable接口 2. Collecti ...
- 【持久层】Druid简介
Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBoss DataSou ...
- 【多线程】synchronized 和ReentrantLock
1. 锁的实现 synchronized 是 JVM 实现的,而 ReentrantLock 是 JDK 实现的. 2. 性能 新版本 Java 对 synchronized 进行了很多优化,例如自旋 ...
- PHP filter_list() 函数
定义和用法 filter_list() 函数返回包含所有得到支持的过滤器的一个数组. 语法 filter_list() 提示和注释 注释:该函数的结果不是过滤器 ID,而是过滤器名称.请使用 filt ...