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 ;
}


2016年省赛G题, Parenthesis的更多相关文章

  1. 2016年省赛 G Triple Nim

    2016年省赛 G Triple Nimnim游戏,要求开始局面为先手必败,也就是异或和为0.如果n为奇数,二进制下最后一位只有两种可能1,1,1和1,0,0,显然异或和为1,所以方案数为0如果n为偶 ...

  2. HDU 5880 Family View (2016 青岛网络赛 C题,AC自动机)

    题目链接  2016 青岛网络赛  Problem C 题意  给出一些敏感词,和一篇文章.现在要屏蔽这篇文章中所有出现过的敏感词,屏蔽掉的用$'*'$表示. 建立$AC$自动机,查询的时候沿着$fa ...

  3. 2016 ACM-ICPC 青岛站网络赛G题 题解

    [参考博客][https://blog.csdn.net/Tawn0000/article/details/82255682] 题意: 将n个数按照每k个一组来合并,合并需要花费的cost是两个数的长 ...

  4. 2015北京网络赛 G题 Boxes bfs

    Boxes Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/contest/acmicpc2015beijingonl ...

  5. 哈尔滨工程大学ACM预热赛 G题 A hard problem(数位dp)

    链接:https://ac.nowcoder.com/acm/contest/554/G Now we have a function f(x): int f ( int x ) {     if ( ...

  6. Little Sub and Piggybank (杭师大第十二届校赛G题) DP

    题目传送门 题意:每天能往存钱罐加任意实数的钱,每天不能多于起那一天放的钱数.如果某一天的钱数恰好等于那天的特价商品,则可以买,求最后的最大快乐值. 思路:先来一段来自出题人的题解: 显然的贪心:如果 ...

  7. ZOJ 3940 Modulo Query (2016年浙江省赛E题,区间折叠 + map运用)

    题目链接  2016 ZJCPC Problem E 考虑一个开区间$[0, x)$对$a_{i}$取模的过程. $[0, x)$中小于$a_{i}$的部分不变,大于等于$a_{i}$的部分被切下来变 ...

  8. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)

    题目链接  2016 Qingdao Online Problem I 题意  在一棵给定的树上删掉一条边,求剩下两棵树的树的直径中较长那的那个长度的期望,答案乘上$n-1$后输出. 先把原来那棵树的 ...

  9. 2019年FJNU低编赛 G题(dfs博弈)

    ###题目链接### 题目大意: 有一个 0 ~ n+1 的数轴,Alice 站在 0 点处,Bob 站在 n+1 点处.在 1 ~ n 上各有着权值. Alice 每次向右移动 1 格或两格 ,Bo ...

随机推荐

  1. codevs 1206 保留两位小数

    http://codevs.cn/problem/1206/ 1206 保留两位小数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  2. Python学习总结8:文件模式及操作方法汇总

    文件操作之前需要文件保证文件存在,并且将文件open os.mknod("test.txt")        创建空文件 fp = open("test.txt" ...

  3. NovaMind使用教程

    NovaMind 使用教程 目前NovaMind在网络上基本没什么中文资料,它自带的"欢迎"导图也只有英文版本.导致很多朋友对这个工具的使用技巧不够了解.今天我把自己的使用心得整理 ...

  4. 利用Hudson持续集成来执行Android自动化测试(转)

    当你套用Athrun.Robotium等框架,针对自己的项目写完了一堆自动化测试脚本后,在Eclipse之外怎么让它们可以持续性地跑起来并展现报告呢? 据我了解,方便的方法大致有两个:其一,利用Hud ...

  5. Java线程总结

    在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: class 类名 extends Thread ...

  6. HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...

  7. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  8. 【php】目录、路径和文件 操作

    目录操作 解析路径: basename() - 返回路径的文件名部分 获取目录部分: dirname() - 返回路径的目录部分 路径信息: pathinfo() - 返回数组(目录名,基本名,扩展名 ...

  9. $.extend,$.fn.extend,$.fn的区别

    jQuery.extend(object) 为jQuery类添加类方法,可以理解为添加静态方法.如: jQuery.extend({ min: function(a, b) { return a &l ...

  10. RMQ(非log2储存方法)

    2016-03-31 RMQ 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 长度为n的数列A,以及q个询问,每次询问一段区间 ...