题意:给你一个括号序列,这个括号序列将确定一颗二叉树。有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. Vue小白篇 -Vue 的模板语法

    可以插入任何你想插入的内容,除了 if-else if-else用三元运算符代替 <div id="box"> <!--模板语法--> <h2> ...

  2. mysql安装与修改密码

    数据库基本概念:数据的仓库 数据库服务器-->数据库-->表-->记录-->属性(列,字段) unix下数据库服务安装: apt-get install -y mysql-se ...

  3. html5中的选择器

    1.html5中的属性选择器 <body> <style type=text/css> <!--1>完全匹配选择器--> [id=test]{ color:r ...

  4. MySQL事务调优

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11429239.html 数据库事务 数据库事务是数据库系统执行过程中的一个逻辑处理单元,保证一个数据库 ...

  5. sparksql笔记

    1.sparksql是Spark用来处理结构化数据的一个模块,它提供了两个抽象DataFrame和DataSet并且作为分布式SQL查询引擎的作用. Hive SQL转换成MapReduce然后提交到 ...

  6. layui导出表格全部数据

    layui自带的导出表格,只能导出当前页面,如果当前页包含全部数据,那不就是导出全部数据了吗,所以我给导出事件单独定义了一个请求,当触发这个请求时,在后台查询数据时不要按接收的page 和 limit ...

  7. win10操作系统 64位 原版 百度网盘下载

    iso镜像文件4.57G,这里压缩成两个两个包便于上传网盘: 使用时候,直接下载两个压缩包解压成镜像文件便可安装: 链接:https://pan.baidu.com/s/1JNgxuBzdzFpp-p ...

  8. teradata在虚拟机安装客户端sql Assistant

    学习链接:https://www.w3cschool.cn/teradata/? 1.安装过程

  9. 58、salesforce学习笔记(五)

    Set集合 Set<String> set1 = new Set<String>(); set1.add('1'); set1.add('2'); Set<String& ...

  10. ubuntu安装完成后需要做的事情

    1.删除libreoffice libreoffice虽然是开源的,但是Java写出来的office执行效率实在不敢恭维,装完系统后果断删掉 [html] view plain copy sudo a ...