1809: Parenthesis

Submit      Status     Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 1149     Solved: 326


Description

Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q questions.
The i-th question is whether P remains balanced after pai and pbi  swapped. Note that questions are individual so that they have no affect on others.
Parenthesis sequence S is balanced if and only if:
1.S is empty;
2.or there exists balanced parenthesis sequence A,B such that S=AB;
3.or there exists balanced parenthesis sequence S' such that S=(S').

Input

The input contains at most 30 sets. For each set:
The first line contains two integers n,q (2≤n≤105,1≤q≤105).
The second line contains n characters p1 p2…pn.
The i-th of the last q lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).

Output

For each question, output "Yes" if P remains balanced, or "No" otherwise.

Sample Input

4 2
(())
1 3
2 3
2 1
()
1 2

Sample Output

No
Yes
No

题目链接:CSU 1809

下午又被同学叫去做做题,大约是湖南省某套题目,这回太excited的啦,直接爆零滚粗,然后各种查题解,虽然发现题目难度确实不小且解题程序代码量基本都比较大(其中感觉最骚的是那道地铁,没见过这么骚的建模),但是还是想做几题,然后晚上没事儿看看其他OJ突然发现某篇另外的其他地方的比赛题解里有讲到一种简单的判断括号序列的方法:记左括号为1,右括号为-1,求出前缀和,若结尾为0且前缀和序列没有负数,则说明括号序列合法,然后又想到这题应该可以好好利用这个方法做

那么跟这一题有什么关系呢?考虑题目中所说交换的情况,共四种,除去很容易想到的等价的交换两种(左括号交换左括号,右括号交换右括号),还有左-右与右-左两种,然后我们放到前缀和序列上来考虑,首先你得特判,若开头或结尾处被交换必定不合法(此处已除去等价交换的情况),因为开头必定是'('而结尾必定是')';然后再考虑中间的交换,我们方便一点令a<b(交换本来就是可逆的,a、b互换不影响结果),若'('与')'交换,则说明1被换到后面,-1被换到前面,这里我们分为三步,先把1拿出来,再把-1放到a位置,再把1放到b位置,我们会发现前缀$prefix_{a...b-1}$改变了-2,而$prefix_{b...n}$显然是不变的,那么只需判断改变前的一段是否均大于等于2以至于均-2后不会出现0;再考虑另外一种')'与'('交换,若按前面的前缀和打个草稿会发现这样做一定是合法的,改变一段的值一定为正,那么这题就用RMQ-ST就可以了,代码量不大,但是由于第二种情况忘记分开讨论了Debug很久……

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1e5 + 7;
char s[N];
int prefix[N], Min[N][18]; void init()
{
prefix[0] = 0;
CLR(Min, INF);
}
void RMQ_init(int l, int r)
{
int i, j;
for (i = l; i <= r; ++i)
Min[i][0] = min<int>(Min[i][0], prefix[i]);
for (j = 1; l + (1 << j) - 1 <= r; ++j)
{
for (i = l; i + (1 << j) - 1 <= r; ++i)
{
Min[i][j] = min<LL>(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
}
}
}
int ST(int l, int r)
{
int k = log2(r - l + 1.0);
return min(Min[l][k], Min[r - (1 << k) + 1][k]);
}
int main(void)
{
int n, q, i;
while (~scanf("%d%d", &n, &q))
{
init();
scanf("%s", s + 1);
for (i = 1; i <= n; ++i)
prefix[i] = prefix[i - 1] + (s[i] == '(' ? 1 : -1);
RMQ_init(1, n);
while (q--)
{
int a, b;
scanf("%d%d", &a, &b);
if (a > b)
swap(a, b);
if (s[a] == s[b])
puts("Yes");
else
{
if (a == 1 || b == n || (ST(a, b - 1) < 2 && s[a] == '(' && s[b] == ')'))
puts("No");
else
puts("Yes");
}
}
}
return 0;
}

最新文章

  1. 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题
  2. eclipse护眼颜色和字体大小设置
  3. ListView 中的ImageView Button
  4. 关于 fir.im 你可能不知道的实用小工具
  5. linux php redis扩展的安装和redis服务的安装
  6. Nginx学习笔记(五) 源码分析&amp;内存模块&amp;内存对齐
  7. [转载] 跟着实例学习zookeeper 的用法
  8. java正则表达式Pattern和Matcher
  9. java 连接数据库mysql的方法
  10. 在asp.net中导出表格Excel数据
  11. ext Window点击右上角的关闭(Xbutton)加入监控事件
  12. 我的JS 中级学习篇
  13. 使用python实现计算器功能
  14. 利用JAVA API远程进行HDFS的相关操作
  15. 【整理】Java 8新特性总结
  16. 【备忘】mybatis的条件判断用&lt;choose&gt;
  17. java操作对比两个字符串,将差异数据提取出来
  18. Add Columns to the Web Sessions List
  19. EBS获取code_combination_id(CCID)时段值自动被置为默认值的问题
  20. tensorflow的assgin方法

热门文章

  1. 【BZOJ1045】糖果传递(基于贪心的数学题)
  2. python_29_三级菜单
  3. spring mvc + swagger 配置
  4. SqlServer2000事件探测器的使用
  5. 重温 JSP 与 Servlet
  6. 定位设备--llseek实现
  7. redis redis-cli 操作指令
  8. 【WordPress】CentOS 6.10 测试WP发送邮件失败
  9. Liunx环境--Node部署记录
  10. POJ3436------ACM Computer Factory