AtCoder F - Parenthesis Checking
原题链接:AtCoder F - Parenthesis Checking
一个全由\('('\)和\(')'\)构成的字符串,由以下两个操作:
1 l r交换字符串第\(l\)个和第\(r\)个字符。2 l r询问\(S[l-r]\)是否是一个合法序列。
很明显是一个线段树操作,这题蓝桥杯貌似有类似的,但是那道题貌似要用平衡树,也是操作之后判断括号序列是否合法,现在终于找到答案了,方法。
我们让\('('\)为\(1\),让\(')'\)为\(-1\),那么这样括号序列就成了只有\(1\)和\(-1\)的一个序列,然后我们用线段树维护一个区间和,那么一个合法括号序列的条件就是这段区间和等于\(0\),然后这个区间的前缀和得大于\(0\)。
区间和好维护,但是区间前缀和怎么维护,那我们就维护一个前缀最小值就\(ok\)了,对于\(pushup\),也就是\(min(左子树的最小值,左子树的和+右子树最小值)\),很巧妙,塞给队友队友直接秒了,然后我想了一天多。
#include <bits/stdc++.h>
using namespace std;
const int N = 2E5 + 10;
int W[N];
struct SegmentTree {
int l, r;
int sum, pre_min;
} tr[N * 4];
void push_up(int u) {
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
tr[u].pre_min = min(tr[u << 1].pre_min, tr[u << 1].sum + tr[u << 1 | 1].pre_min);
}
void build(int u, int l, int r) {
if (l == r) {
tr[u] = { l, r, W[r], W[r] };
}
else {
int mid = l + r >> 1;
tr[u] = { l, r };
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
push_up(u);
}
}
void modify(int u, int x, int v) {
if (tr[u].l == tr[u].r) {
tr[u].sum = tr[u].pre_min = v;
}
else {
int mid = tr[u].l + tr[u].r >> 1;
if (x <= mid) modify(u << 1, x, v);
else modify(u << 1 | 1, x, v);
push_up(u);
}
}
pair<int, int> query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) {
return { tr[u].sum, tr[u].pre_min };
}
else {
int mid = tr[u].l + tr[u].r >> 1;
pair<int, int> left = { 0, 0 };
if (l <= mid) left = query(u << 1, l, r);
pair<int, int> right = { 0, 0 };
if (r > mid) right = query(u << 1 | 1, l, r);
//auto right = query(u << 1 | 1, l, r);
return { left.first + right.first, min(left.second, left.first + right.second) };
}
}
int main() {
int n, q;
string s;
cin >> n >> q;
cin >> s;
for (int i = 0; i < n; i++) W[i + 1] = (s[i] == '(' ? 1 : -1);
build(1, 1, n);
while (q--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 1) {
swap(W[l], W[r]);
modify(1, l, W[l]), modify(1, r, W[r]);
}
else {
auto t = query(1, l, r);
if (t.first == 0 && t.second >= 0) puts("Yes");
else puts("No");
}
}
return 0;
}
AtCoder F - Parenthesis Checking的更多相关文章
- Atcoder F - LCS (DP-最长公共子序列,输出字符串)
F - LCS Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are gi ...
- 【atcoder F - Namori】**
F- Namori http://agc004.contest.atcoder.jp/tasks/agc004_f Time limit : 2sec / Memory limit : 256MB S ...
- Atcoder F - Mirrored(思维+搜索)
题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_d 题意:求rev(N)=N+D的个数,rev表示取反.例如rev(123)=321 题解:具体看 ...
- AtCoder F - Exhausted?
传送门 sxy题解: //Achen #include<algorithm> #include<iostream> #include<cstring> #inclu ...
- Amazon Interview | Set 27
Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...
- Linux LVM 简单操作
查看当前磁盘分区情况fdisk -l 磁盘分区fdisk /dev/sdb# 可能用到的Type :# 8e Linux LVM# fd Linux raid auto 创建PVpvcreate /d ...
- day07 - Python - 面向对象进阶
本节内容: 面向对象高级语法部分异常处理异常处理异常处理 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 1 ...
- Educational Codeforces Round 22 补题 CF 813 A-F
A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...
- elasticsearch7.x集群安装(含head、bigdesk、kibana插件)
网址:https://www.elastic.co 192.168.14.239 es-node1192.168.14.240 es-node2192.168.14.241 es-node3 ==== ...
- Module ngx_http_rewrite_module
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html Directives break if return ...
随机推荐
- Pandas:获取Dataframe索引
解决方案 效果图 参考链接 https://blog.csdn.net/YENTERTAINR/article/details/109254583
- 解决npm install 报错 'proxy' config is set properly. See: 'npm help config'
输入以下命令 npm config set proxy null npm config set https-proxy null 之后重新安装即可 文章参考 https://blog.csdn.net ...
- Go命令
build: 编译包和依赖 clean: 移除对象文件 doc: 显示包或者符号的文档 env: 打印go的环境信息 bug: 启动错误报告 fix: 运行go tool fix fmt: 运行gof ...
- SQLite入门指南:轻松学习带有实例的完整教程(含示例)
SQLite官网:https://www.sqlite.org/index.html 源视频教程:https://www.bilibili.com/video/BV1Zz411i78o 菜鸟教程文档: ...
- 使用 Go 语言实现二叉搜索树
原文链接: 使用 Go 语言实现二叉搜索树 二叉树是一种常见并且非常重要的数据结构,在很多项目中都能看到二叉树的身影. 它有很多变种,比如红黑树,常被用作 std::map 和 std::set 的底 ...
- P1880 [NOI1995] 石子合并 题解
区间DP. 首先将其复制一遍(因为是环),也就是经典的破环成链. 设 \(f[i][j]\) 表示将 \(i\) 到 \(j\) 段的石子合并需要的次数. 有 \[f[i][j] = 0(i = j) ...
- 从 HTTP/1.1 到 HTTP/3
从 HTTP/1.1 到 HTTP/3,解决了一些旧协议的问题,引入了好用的新功能. HTTP/1.1 HTTP/1.1 通过在传输层和应用层之间增加 SSL/TSL 解决数据不安全的问题,但它本身还 ...
- servlet系列:简介和基本使用以及工作流程
目录 一.简介 二.Servlet实现 三.基本使用 1.引入pom依赖 2.实现Servlet规范,重写service方法 3.配置web.xml 4.配置Tomcat 6.运行 四.Servlet ...
- 状压DP-学习笔记
状压DP 状压 \(DP\) 是一种基于二进制数的 \(DP\). T1 题目大意 将一个整数 \(N\) 分解成若干个小整数的乘积,满足: 分解出的整数必须来自集合 \(S\). 分解出的整数必须互 ...
- Api接口如何防止被刷?
当今,越来越多的应用程序和服务都提供了API接口,使得开发人员可以方便地与这些应用程序和服务进行交互.但是,由于API接口是公开的,因此很容易被黑客利用,对系统造成损害.为了确保API接口的安全性 ...