Codeforces 1150E(树、线段树)
要点
- 括号序列平衡度即树深度的性质
- 相当于中序遍历,则两点间最浅的地方即是LCA的性质
- 线段树维护\(d(a) + d(c) - 2*d(lca(a,c))\),一层层剥,思考维护这个量需要什么,结果维护一大堆。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 2e5 + 5;
int n, q;
char s[maxn];
class SegmentTree {
public:
#define ls (p << 1)
#define rs (p << 1 | 1)
struct node {
int l, r;
int depth;//its root is s[l]
int ans;//d(a) + d(c) - 2d(lca(a, c))
int lmax;//d(a) - 2d(lca(a, c))
int rmax;//d(c) - 2d(lca(a, c))
int mxd;//max depth
int mnd;//min depth
void init(int val) {
mxd = mnd = depth = val;
lmax = rmax = -val;
ans = 0;
}
}t[maxn * 3];
void Push_up(int p) {
// b = lca(a, c), a <= b <= c
t[p].depth = t[ls].depth + t[rs].depth;
t[p].mxd = max(t[ls].mxd, t[rs].mxd + t[ls].depth);
t[p].mnd = min(t[ls].mnd, t[rs].mnd + t[ls].depth);
t[p].lmax = max(t[ls].lmax, t[rs].lmax - t[ls].depth);//a, b both in l or r
t[p].lmax = max(t[p].lmax, t[ls].mxd - 2 * (t[rs].mnd + t[ls].depth));//a in l and b in r
t[p].rmax = max(t[ls].rmax, t[rs].rmax - t[ls].depth);//b, c both in l or r
t[p].rmax = max(t[p].rmax, t[rs].mxd + t[ls].depth - 2 * t[ls].mnd);//b int l and c in r
t[p].ans = max(t[ls].ans, t[rs].ans);//a,b,c all in l or r
t[p].ans = max(t[p].ans, max(t[ls].lmax + t[rs].mxd + t[ls].depth, t[ls].mxd + t[rs].rmax - t[ls].depth));//ab in l, c in r || a in l, bc in r
}
void Build(int l, int r, int p) {
t[p].l = l, t[p].r = r;
if (l == r) {
t[p].init(s[l] == '(' ? 1 : -1);
return;
}
int mid = (l + r) >> 1;
Build(l, mid, ls);
Build(mid + 1, r, rs);
Push_up(p);
}
void Modify(int l, int r, int p) {
if (t[p].l == t[p].r) {
t[p].init(s[l] == '(' ? 1 : -1);
return;
}
int mid = (t[p].l + t[p].r) >> 1;
if (l <= mid) Modify(l, r, ls);
if (mid < r) Modify(l, r, rs);
Push_up(p);
}
}tree;
int main() {
scanf("%d %d", &n, &q);
scanf("%s", s + 1);
n = (n - 1) << 1;
tree.Build(1, n, 1);
printf("%d\n", tree.t[1].ans);
for (int a, b; q; q--) {
scanf("%d %d", &a, &b);
if (s[a] != s[b]) {
swap(s[a], s[b]);
tree.Modify(a, a, 1);
tree.Modify(b, b, 1);
}
printf("%d\n", tree.t[1].ans);
}
}
Codeforces 1150E(树、线段树)的更多相关文章
- 浅谈树套树(线段树套平衡树)&学习笔记
0XFF 前言 *如果本文有不好的地方,请在下方评论区提出,Qiuly感激不尽! 0X1F 这个东西有啥用? 树套树------线段树套平衡树,可以用于解决待修改区间\(K\)大的问题,当然也可以用 ...
- Vasya and a Tree CodeForces - 1076E(线段树+dfs)
I - Vasya and a Tree CodeForces - 1076E 其实参考完别人的思路,写完程序交上去,还是没理解啥意思..昨晚再仔细想了想.终于弄明白了(有可能不对 题意是有一棵树n个 ...
- Codeforces 787D. Legacy 线段树建模+最短路
D. Legacy time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- CodeForces 91B Queue (线段树,区间最值)
http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...
- Codeforces 343D WaterTree - 线段树, DFS序
Description Translated by @Nishikino_Maki from Luogu 行吧是我翻的 Mad scientist Mike has constructed a roo ...
- codeforces 787D - Legacy 线段树优化建图,最短路
题意: 有n个点,q个询问, 每次询问有一种操作. 操作1:u→[l,r](即u到l,l+1,l+2,...,r距离均为w)的距离为w: 操作2:[l,r]→u的距离为w 操作3:u到v的距离为w 最 ...
- Subtree Minimum Query CodeForces - 893F (线段树合并+线段树动态开点)
题目链接:https://cn.vjudge.net/problem/CodeForces-893F 题目大意:给你n个点,每一个点有权值,然后这n个点会构成一棵树,边权为1.然后有q次询问,每一次询 ...
- Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)
Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...
随机推荐
- python dict 实现swich
使用dict实现swich,通过不同的键映射不同的函数. swich = { 'hour1':pred.getper1htable, 'hour6':pred.getper6htable, 'hour ...
- Office宏的基本利用
前言 Office宏,译自英文单词Macro.宏是Office自带的一种高级脚本特性,通过VBA代码,可以在Office中去完成某项特定的任务,而不必再重复相同的动作,目的是让用户文档中的一些任务自动 ...
- WCF服务编程-基础
WCF是微软建立新一代的分布式应用及面向服务应用的标准平台,是基于原有.NET Framework 2.0的扩展.虽然在WCF发布不久就已经在项目中使用WCF技术了.但是由于在项目中还没有较大规模的应 ...
- RQNOJ--2 开心的金明(01背包)
题目:http://www.rqnoj.cn/problem/2 分析:这个题目每一种物品都是有"选"或"不选"两种情况. 属于01背包问题.物品的价格相当于背 ...
- C#计算两个时间的时间差,精确到年月日时分秒
喏,计算两个时间的时间差,精确到年月日时分秒 看起来比较笨的方法了,不知道有没有改进 DateTime d1 = new DateTime(2016, 4, 1, 0, 0, 0); DateTime ...
- Errors were encountered while processing: mysql-server-5.5
ubuntu 中运行完sudo apt-get install curl之后,最后出现: ldconfig deferred processing now taking place Errors we ...
- springboot+atomikos+多数据源管理事务(mysql 8.0)
jta:Java Transaction API,即是java中对事务处理的api 即 api即是接口的意思 atomikos:Atomikos TransactionsEssentials 是一个为 ...
- MUI使用vue示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- java记录CST时间类型的字符串转换问题
下列代码可以实现把Java的CST格式的时间字符串转为为Date对象和所需要的日期时间格式! String dateStr = "Wed Sep 16 11:26:23 CST 2009&q ...
- 判断是否微信浏览器,获取cookie,获取URL来源等
function isWeiXin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenge ...