原题链接: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的更多相关文章

  1. Atcoder F - LCS (DP-最长公共子序列,输出字符串)

    F - LCS Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement You are gi ...

  2. 【atcoder F - Namori】**

    F- Namori http://agc004.contest.atcoder.jp/tasks/agc004_f Time limit : 2sec / Memory limit : 256MB S ...

  3. Atcoder F - Mirrored(思维+搜索)

    题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_d 题意:求rev(N)=N+D的个数,rev表示取反.例如rev(123)=321 题解:具体看 ...

  4. AtCoder F - Exhausted?

    传送门 sxy题解: //Achen #include<algorithm> #include<iostream> #include<cstring> #inclu ...

  5. Amazon Interview | Set 27

    Amazon Interview | Set 27 Hi, I was recently interviewed for SDE1 position for Amazon and got select ...

  6. Linux LVM 简单操作

    查看当前磁盘分区情况fdisk -l 磁盘分区fdisk /dev/sdb# 可能用到的Type :# 8e Linux LVM# fd Linux raid auto 创建PVpvcreate /d ...

  7. day07 - Python - 面向对象进阶

    本节内容: 面向对象高级语法部分异常处理异常处理异常处理 经典类vs新式类 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 1 ...

  8. Educational Codeforces Round 22 补题 CF 813 A-F

    A The Contest 直接粗暴贪心 略过 #include<bits/stdc++.h> using namespace std; int main() {//freopen(&qu ...

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

  10. Module ngx_http_rewrite_module

    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html Directives     break     if     return    ...

随机推荐

  1. std::queue 中遇到释放内存错误的问题

    项目上有个需求要用到 std::queue 顺序处理消息事件 简单的示例如下: struct MyEvent { MyEvent() { event_ = CreateEvent(nullptr, 0 ...

  2. deepin install mariadb

    输入指令: sudo apt-get install mariadb-server mariadb-client

  3. Django2.2:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

    报错截图: 解决方案: 打开django/views下的debug.py文件,转到line331行: with Path(CURRENT_DIR, 'templates', 'technical_50 ...

  4. cdn 引入的资源需要通过 externals 排除打包哦~

    cdn 指的是通过相互连接的网络系统,使用最靠近用户的服务器将音乐.图片等资源以高效率和低成本的方式将内容传递给用户. 在 webpack 中,我们可能会将引入的第三方资源会编译成单独的文件,作为静态 ...

  5. 3.0 Python 迭代器与生成器

    当我们需要处理一个大量的数据集合时,一次性将其全部读入内存并处理可能会导致内存溢出.此时,我们可以采用迭代器Iterator和生成器Generator的方法,逐个地处理数据,从而避免内存溢出的问题. ...

  6. Docker下elasticsearch8部署、扩容、基本操作实战(含kibana)

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇记录了用docker搭建ElasticS ...

  7. 8、Mybatis之自定义映射

    8.1.环境搭建 8.1.1.创建新module 创建名为mybatis_resultMap的新module,过程参考5.1节 8.1.2.创建t_emp和t_dept表 CREATE TABLE ` ...

  8. 2、搭建MyBatis

    2.1.开发环境 IDE:idea 2019.2 构建工具:maven 3.8.4 MySQL版本:MySQL 5.7 MyBatis版本:MyBatis 3.5.7 MySQL不同版本的注意事项 ( ...

  9. Linux查看磁盘空间,文件系统、挂载

    Linux磁盘空间,文件系统.挂载 概述 在使用以下命令查看磁盘使用情况时 df -h du -sh 目标路径 作为初级开发者,Linux入门级选手,可能不禁要问Linux系统的文件系统跟window ...

  10. 《CTFshow-Web入门》01. Web 1~10

    @ 目录 web1 题解 web2 题解 web3 题解 web4 题解 web5 题解 原理 web6 题解 原理 web7 题解 web8 题解 web9 题解 原理 web10 题解 ctf - ...