http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809

给定一串平衡的序列,要求交换两个位置之后,问其是否还平衡。

首先要注意到交换的是两个位置,这两个位置没有大小之分,所以要判断是否swap他们,保持相对大小

然后如果我们把'('当成是1,把')'当成是-1,那么序列平衡,那么前缀和就是0了。

然后考虑下交换的时候,如果就交换相同的字符,那么肯定是Yes了,如果是')' 和 '(',那么也是Yes

因为本来序列就是平衡的,现在这样交换,只不过是把相对位置改了,比如()(),交换2和3,其中和2匹配的1,还是可以看成和交换后的那个字符匹配。

考虑交换的是'(' 和 ')'

因为本来在a位置(字符'(')的时候,前缀和是+1了的,那么现在换了个')'过去,所以[a, b - 1]这段区间的前缀和要-2

[b, lenstr]这段要+2

当然可以更新后,判断前缀和是否为0,+ 中途不能出现负数。

然后注意到只有-2那段区间才会产生负数,所以查询[a, b - 1](注意相对大小)的最小值,如果小于2就NO了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define root 1, n, 1
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
int n, q;
const int maxn = 1e5 + ;
int mi[maxn << ];
int add[maxn << ];
char str[maxn];
int pre_sum[maxn];
void pushUp(int cur) {
mi[cur] = min(mi[cur << ], mi[cur << | ]);
}
void build(int L, int R, int cur) {
if (L == R) {
mi[cur] = pre_sum[L];
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
int query(int begin, int end, int L, int R, int cur) {
if (L >= begin && R <= end) { //子图
return mi[cur];
}
int mid = (L + R) >> ;
int lans = inf;
int rans = inf;
if (begin <= mid) {
lans = query(begin, end, lson);
}
if (end > mid) rans = query(begin, end, rson);
return min(lans, rans);
}
void work() {
cin >> str + ;
for (int i = ; str[i]; ++i) {
if (str[i] == '(') {
pre_sum[i] = pre_sum[i - ] + ;
} else pre_sum[i] = pre_sum[i - ] - ;
}
build(root);
for (int i = ; i <= q; ++i) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
if (str[a] == str[b] || str[a] == ')' && str[b] == '(') {
cout << "Yes" << endl;
} else {
int MI = query(a, b - , root);
// cout << MI << endl;
if (MI < ) {
cout << "No" << endl;
} else cout << "Yes" << endl;
}
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> n >> q) work();
return ;
}

湖南省2016省赛题。1809: Parenthesis 线段树的更多相关文章

  1. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

  2. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  3. Hitcon 2016 Pwn赛题学习

    PS:这是我很久以前写的,大概是去年刚结束Hitcon2016时写的.写完之后就丢在硬盘里没管了,最近翻出来才想起来写过这个,索性发出来 0x0 前言 Hitcon个人感觉是高质量的比赛,相比国内的C ...

  4. 2016多校8th 1008【线段树-神题】

    题意: T N M N个数 M个操作 一个数组A, 有3个操作 1 l r x,a[l]-a[r]都+x 2 l r,a[i]=sqrt(a[i]),l<=i<=r 3 l r,求和,a[ ...

  5. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  6. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面:   Consider an array AA with n elements . Each of its eleme ...

  7. bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)

    题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式 第一行数字N,M接下 ...

  8. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  9. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

随机推荐

  1. html5--5-6 绘制圆/弧

    html5--5-6 绘制圆/弧 学习要点 掌握arc() 方法创建圆弧/曲线(用于创建圆或部分圆) 矩形的绘制方法 rect(x,y,w,h)创建一个矩形 strokeRect(x,y,w,hx,y ...

  2. 阿里云数据库产品HybridDB简介——OLAP数据库,支持行列混合存储,基于数据库Greenplum的开源版本,并且吸收PostgreSQL精髓

    为什么会有HybridDB的诞生?它经历了怎样的研发历程?它的应用场景和情况是怎样的?带着这些问题,InfoQ对阿里云的数据库专家兼Postgres中国社区/中国用户会主席萧少聪先生进行了采访,以下文 ...

  3. mongodb压缩——snappy、zlib块压缩,btree索引前缀压缩

    MongoDB 3.0 WiredTiger Compression and Performance One of the most exciting developments over the li ...

  4. php数组合并

    php的数合并函数: array_merge($arr1, $arr2, ..., $arr{$n}); 如果数组的键名有重复,后面的会覆盖前面的. 如果键名是数字索引,则会重新排列索引,往后累加. ...

  5. oracle 导入imp 命令

    最常用的      imp  name/password@IP:1521/orcl[库] file="c:\123.dmp" full=y ignore=y. 例:imp abc/ ...

  6. VS2008中使用JSONCPP方法小结

    Introduction JSON (JavaScript Object Notation) is a lightweight data-interchange format. It can repr ...

  7. 生产环境下JAVA进程高CPU占用故障排查---temp

    问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...

  8. 《Linux内核修炼之道》精华分享与讨论(5)——Kernel地图:Kconfig与Makefile

    转自:http://blog.csdn.net/fudan_abc/article/details/5340408 Makefile不是Make Love 从前在学校,混了四年,没有学到任何东西,每天 ...

  9. 你忘记的java运算符

    当整数被0除时会得到一个无穷大,或者nan, 所以会抛出数据溢出的异常.

  10. shell脚本 列出所有网卡的ip地址

    #!/bin/bashfor i in `ifconfig | grep -o ^[a-z0-9]*`do ifconfig $i|sed -n 2p|awk '{ print $2 }'|tr -d ...