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 ...
随机推荐
- 【记录】@Configuration注解作用 mybatis @Param作用
参考地址: 1:https://www.cnblogs.com/duanxz/p/7493276.html 2:https://www.wandouip.com/t5i91156/ 3:https:/ ...
- (playbook)ubuntu下ansible安装docker
--- - hosts: test sudo: yes vars: apt_packages_ca: - apt-transport-https - ca-certificates tasks: - ...
- opengl 库glew
OpenGL OpenGL是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库.OpenGL的前身是SGI公司为其图形工作站开发的IRIS GL.IRIS GL是一个工业标准的3D图形软件 ...
- 力扣——single number (只出现一次的数字) python实现
题目描述: 中文: 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 英 ...
- 【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...
- 【leetcode】942. DI String Match
题目如下: Given a string S that only contains "I" (increase) or "D" (decrease), let ...
- MariaDB 更新查询
UPDATE 命令通过更改值来修改现有字段. 它使用SET子句指定要修改的列,并指定分配的新值. 这些值可以是字段的表达式或默认值. 设置默认值需要使用DEFAULT关键字. 该命令还可以使用WHER ...
- 导入csv 到mysql数据库
1.查询导入数据存放位置 show variables like '%secure%'; +--------------------------+-----------------------+ | ...
- kafka 简单安装以及java小demo
文章目录 第1步,下载解压 kafka: 第2步,运行 kafka: 第3步,创建topic 第4步,生产者发送消息 第5步,消费者接收消息 使用 java 客户端 kafka 0.8.0版本demo ...
- laravel新增路由文件
除去原有路由文件,有时为方便路由管理,我们可以新增独立路由文件,如:针对管理后台的路由文件. 1.在routes文件夹下创建新路由文件admin.php 2.在app\Providers\RouteS ...