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 的询问更新答案. 我们先考虑最暴力的做法, ...
随机推荐
- collections中namedtuple的用法
我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: p = (1, 2) 但是,看到(1, 2),很难看出这个tuple是用来表示一个坐标的.这时,namedtuple就派上了用 ...
- pandas一些基本操作(DataFram和Series)_3
import pandas as pd;import numpy as np#通过一维数组创建Chinese = np.array([89,87,86])print(Chinese)print(pd. ...
- parameter– tRPRE and tRPST
DDR读数据有效之前,有一段时间DQS(DQS#)需为低(高),此段时间即为read preamble,tRPRE. 同理,读数据结束之前,某段时间为read postamble,tRPST.
- 左神算法书籍《程序员代码面试指南》——2_02在单链表和双链表中删除倒数第k个字节
[题目]分别实现两个函数,一个可以删除单链表中倒数第K个节点,另一个可以删除双链表中倒数第K个节点.[要求]如果链表长度为N,时间复杂度达到O(N),额外空间复杂度达到O(1).[题解]从头遍历链表, ...
- leetcode 699. Falling Squares 线段树的实现
线段树实现.很多细节值得品味 都在注释里面了 class SegTree: def __init__(self,N,query_fn,update_fn): self.tree=[0]*(2*N+2) ...
- js之简单工厂模式
简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口. 这种模式主要用在所实例化的类型在编译期并不能确定, 而是在执行期决定的情况. 说的通俗点,就像公司茶水间的饮料 ...
- UVA1416/LA4080 Warfare And Logistics
题目大意:有N个点,M条路,如果两条路不连通的话,就将这两条路的距离设置为L 现在要求你求出每两点之间的最短距离和 接着要求 求出炸断 给出的M条路中的一条路后,每两点之间的最短距离和的最大值(翻译来 ...
- Leetcode438.Find All Anagrams in a String找到字符串中所有字母异位词
给定一个字符串 s 和一个非空字符串 p,找到 s 中所有是 p 的字母异位词的子串,返回这些子串的起始索引. 字符串只包含小写英文字母,并且字符串 s 和 p 的长度都不超过 20100. 说明: ...
- 用Python的requests库作接口测试——上传文件
POST一个多部分编码(Multipart-Encoded)的文件 Requests使得上传多部分编码文件变得很简单: >>> url = 'http://httpbin.org/p ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...