题意:给你一个括号序列,这个括号序列将确定一颗二叉树。有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的更多相关文章

  1. Tree Generator™ CodeForces - 1149C (线段树,括号序列)

    大意: 给定括号序列, 每次询问交换两个括号, 求括号树的直径. 用[ZJOI2007]捉迷藏的方法维护即可. #include <iostream> #include <algor ...

  2. 51 nod 1766 树上的最远点对(线段树+lca)

    1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题   n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...

  3. csu 1798(树上最远点对,线段树+lca)

    1798: 小Z的城市 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 60  Solved: 16[Submit][Status][Web Board] ...

  4. 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 ...

  5. Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论

    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...

  6. 【Codeforces 1083C】Max Mex(线段树 & LCA)

    Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...

  7. 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 ...

  8. HDU5266---pog loves szh III (线段树+LCA)

    题意:N个点的有向树, Q次询问, 每次询问区间[L, R]内所有点的LCA. 大致做法:线段树每个点保存它的孩子的LCA值, 对于每一次询问只需要 在线段树查询即可. #include <bi ...

  9. 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 ...

随机推荐

  1. python问答

    1)什么是Python?使用Python有什么好处? Python是一种编程语言,包含对象,模块,线程,异常和自动内存管理.Python的好处在于它简单易用,可移植,可扩展,内置数据结构,并且它是一个 ...

  2. mybatis 自定义查询语句

    通过mybatis插件生成的mapper文件只有基本的增.删.改.查.汇总.但是实际使用场景中,总是有各种需要连表.汇总.分组查询的需求,那我们一般都通过自定义查询语句去实现. 有时候会有表结构更改的 ...

  3. cnblogs添加打赏

    上图上真相 1.进入后台设置---文件 2.上传你的支付宝和微信收款码(注意图片格式为bmp格式) 2.还是上图的位置,选择设置选项,找到博客侧边栏公告(支持HTML代码)(支持JS代码) 3.将如下 ...

  4. javascript 操作cookies详解

    javascript 操作cookies详解 这段操作cookies的方法我使用很久了,但是一直一来没遇到什么问题,今天在做一个在第一个页面保存了cookies,第二个页面获取或者第三个页面获取的功能 ...

  5. JMeter 阶梯式加压测试插件 Concurrency Thread Group

    jmeter阶梯式加压测试:JMeter 阶梯式加压测试插件 Stepping Thread Group 由于Stepping Thread Group插件相对来说过于老旧,已不被官方所推荐. 所以这 ...

  6. vSphere Client部署OVA失敗:無效:SHA256(xxxxxxx.ovf)。

    通過vSphere Client部署OVA失敗. 您會看到此錯誤:OVF包無效,無法部署.以下清單文件條目(第1行)無效:SHA256(xxxxxxx.ovf). 原因 出現此問題的原因是vSpher ...

  7. setTag()与getTag()的使用介绍

    转载博客:http://www.cnblogs.com/topcoderliu/archive/2011/06/07/2074419.html View中的setTag(Onbect)表示给View添 ...

  8. Arrays(一)二次封装自己的数组

    一.二次封装自己的数组 目标:设计int 类型数组, (1)可以根据用户传入的容量设置数组的长度,如果用户未设置容量,默认设置长度为10(考虑使用多态性) (2)判断数组是否为空 (3)获取数组的容量 ...

  9. redis shell命令

    APPEND key value追加一个值到key上 AUTH password验证服务器 BGREWRITEAOF异步重写追加文件 BGSAVE异步保存数据集到磁盘上 BLPOP key [key ...

  10. MySQL导入导出数据和表结构 source和mysqldump

    MySQL导入数据的方式: 1.使用source /dir/test.sql导入数据进入数据库:查询数据库编码格式show variables like "%char%";设置编码 ...