Problem G: Parenthesis

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 398  Solved: 75
[Submit][Status][Web Board]

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 比赛的时候,老O查个单词,我后来又查了一下,单词没看完意思,说是"不规则的",发现里面还有一个括号的意思,也就是说只有括号,当时还以为有其他字符,想了好久,好像其他字符在这里没有起到作用
然后是第二个条件,可不可以反推,纠结了好久,最后还是直接模拟了一遍,虽然已经知道会tle,还是写了一下,当时只有这个题目A得人比较多一点。
解题报告:
1、只有括号
2、3个条件就是括号匹配的实质。 思路:
  直接栈模拟是肯定不行的,就算要用栈模拟也不是普通的栈模拟,直接记录左括号的个数,这样模拟。然后这里询问次数10^5,n = 10^5,就是10^10了,这里用线段树优化,每次)(区间都是+2,
  直接yes,要是(),就看-2是否是小于0.
#include <stdio.h>
#include <algorithm> using namespace std;
int temp[];
char str[]; #define INF 0x3f3f3f3f struct node
{
int left,right,val;
} c[*]; void build_tree(int l,int r,int root)
{
c[root].left=l;
c[root].right=r;
if(l==r)
{
c[root].val=temp[l];
return ;
}
int mid=(l+r)/;
build_tree(l,mid,root*);
build_tree(mid+,r,root*+);
c[root].val=min(c[root*].val,c[root*+].val);
} void query(int l,int r,int &min1,int root)
{
if(c[root].left==l&&c[root].right==r)
{
min1=c[root].val;
return ;
}
int mid=(c[root].left+c[root].right)/;
if(mid<l)
query(l,r,min1,root*+);
else if(mid>=r)
query(l,r,min1,root*);
else
{
int min2;
query(l,mid,min1,root*);
query(mid+,r,min2,root*+);
min1=min(min1,min2);
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
scanf("%s",str+);
int counts = ;
for(int i=; i<=n; i++)
{
if(str[i]=='(')
temp[i] = ++counts;
else temp[i] = --counts;
} build_tree(,n,);
int ls,rs;
for(int i=; i<m; i++)
{
scanf("%d%d",&ls,&rs);
if(ls > rs)
swap(ls,rs); if(str[ls]==')'&&str[rs]=='(')
{
puts("Yes");
continue;
}
if(str[ls]==str[rs])
{
puts("Yes");
continue;
}
if(str[ls]=='('&&str[rs]==')')
{
int mins = INF;
query(ls,rs-,mins,);
if(mins<)
puts("No");
else puts("Yes");
continue;
}
}
}
return ;
}


最新文章

  1. Atitit.&#160;Atiposter&#160;发帖机&#160;新特性 poster new feature &#160;&#160;v7 q39
  2. Failed to create AppDomain 'xxx'. Exception has been Failed to create AppDomain
  3. HDU 1131 Count the Trees 大数计算
  4. 【转载】Kafka High Availability
  5. Fast Matrix Operations
  6. +=与join的性能测试
  7. android中的Handler和Runnable
  8. A package manager for Qt
  9. 在线协作沟通工具DesignBoard帮助设计团队更有效地进行沟通与版本管理
  10. 九天学会Java,第三天,选择结构
  11. iOS之 NSTimer(一)
  12. html的语法注意事项
  13. php基础-mysqli
  14. apt-get update 出现错误“ AppStream cache update completed, but some metadata was ignored due to errors. ”
  15. c/c++ 标准库 map set 大锅炖
  16. Ubuntu 16.04安装vsftpd 并开启ftp服务
  17. 微软职位内部推荐-Sr. SW Engineer for Privacy Id
  18. SMS
  19. android 工具类 数据库管理
  20. (转)linux内核调优参数对比和解释

热门文章

  1. Jenkins自动构建
  2. Swift游戏实战-跑酷熊猫 10 视差滚动背景
  3. [转] linux中常用的命令
  4. Java基础(38):Calendar类的应用(优于Date类)
  5. WebDriver:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms
  6. android 测试(转)
  7. Rest服务
  8. getResource().getPath()返回的路径空格变成了 %20
  9. java 网络编程(四)----UDP进阶篇聊天小程序
  10. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON HighpassImage