G - Parenthesis

Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Description

Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions.
The i-th question is whether P remains balanced after p ai and p bi  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≤10 5,1≤q≤10 5).
The second line contains n characters p 1 p 2…p n.
The i-th of the last q lines contains 2 integers a i,b i (1≤a i,b i≤n,a i≠b i).

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

Hint

 思路:线段树;
 当括号匹配时我们有所有的前缀和sum〉=0;那么当我们交换两个括号时仅会改变sum[a]----sum[b]的值 ,并且只有当a为(  b为 )才可能不匹配,那么当这两交换,这个区间段所有的值会减去2,那么我们用线段树维护sum的最小值即可,然后与2比较。复杂n*log(n)
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<queue>
5 #include<stdlib.h>
6 #include<string.h>
7 using namespace std;
8 char str[100005];
9 int tree[100005*4];
10 int ans[100005];
11 void build(int l,int r,int k);
12 int ask(int l,int r,int k,int nn,int mm);
13 int main(void)
14 {
15 int n,m;
16 while(scanf("%d %d",&n,&m)!=EOF)
17 {
18 scanf("%s",str);
19 int l = strlen(str);
20 ans[0] = 0;
21 for(int i = 0; i < l; i++)
22 {
23 if(str[i] == '(')
24 {
25 ans[i+1] = 1;
26 }
27 else ans[i+1] = -1;
28 }
29 for(int i = 1; i <= l; i++)
30 {
31 ans[i] = ans[i] + ans[i-1];
32 }
33 build(1,l,0);
34 while(m--)
35 {
36 int x,y;
37 scanf("%d %d",&x,&y);
38 if(x > y)swap(x,y);
39 if(str[x-1]==str[y-1]||str[x-1]==')')
40 {
41 printf("Yes\n");
42 }
43 else
44 {
45 int ak = ask(x,y-1,0,1,l);
46 if(ak<2)
47 printf("No\n");
48 else printf("Yes\n");
49 }
50 }
51 }return 0;
52 }
53 void build(int l,int r,int k)
54 {
55 if(l == r)
56 {
57 tree[k] = ans[l];
58 }
59 else
60 {
61 build(l,(l+r)/2,2*k+1);
62 build((l+r)/2+1,r,2*k+2);
63 tree[k] = min(tree[2*k+1],tree[2*k+2]);
64 }
65 }
66 int ask(int l,int r,int k,int nn,int mm)
67 {
68 if(nn>r||mm<l)
69 {
70 return 1e9;
71 }
72 else if(l<=nn&&r>=mm)
73 {
74 return tree[k];
75 }
76 else
77 {
78 int x = ask(l,r,2*k+1,nn,(nn+mm)/2);
79 int y = ask(l,r,2*k+2,(nn+mm)/2+1,mm);
80 return min(x,y);
81 }
82 }
 

Parenthesis的更多相关文章

  1. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  2. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  3. 2016年省赛G题, Parenthesis

    Problem G: Parenthesis Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 398  Solved: 75[Submit][Status ...

  4. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  5. CSU 1809 Parenthesis(线段树+前缀和)

    Parenthesis Problem Description: Bobo has a balanced parenthesis sequence P=p1 p2-pn of length n and ...

  6. HDU 5831 Rikka with Parenthesis II (栈+模拟)

    Rikka with Parenthesis II 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...

  7. sre_constants.error: unbalanced parenthesis

    Traceback (most recent call last):   File "androidmarket82.py", line 108, in <module> ...

  8. BZOJ3300: [USACO2011 Feb]Best Parenthesis

    3300: [USACO2011 Feb]Best Parenthesis Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 89  Solved: 42 ...

  9. hdu_5831_Rikka with Parenthesis II(模拟)

    题目链接:hdu_5831_Rikka with Parenthesis II 题意: 给你一些括号的排列,必须交换一次,能不能将全部的括号匹配 题解: 模拟一下括号的匹配就行了,注意要特判只有一对括 ...

  10. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

随机推荐

  1. MySQL 的查询优化

    说起 MySQL 的查询优化,相信大家收藏了一堆奇技淫巧:不能使用 SELECT *.不使用 NULL 字段.合理创建索引.为字段选择合适的数据类型..... 你是否真的理解这些优化技巧?是否理解它背 ...

  2. centos 安装reids

    1.安装tcl支持 yum install tcl 2.安装redis我们以最新的2.8.9为例 $ wget http://download.redis.io/releases/redis-2.8. ...

  3. 《手把手教你》系列技巧篇(四十六)-java+ selenium自动化测试-web页面定位toast-下篇(详解教程)

    1.简介 终于经过宏哥的不懈努力,偶然发现了一个toast的web页面,所以直接就用这个页面来夯实一下,上一篇学过的知识-处理toast元素. 2.安居客 事先声明啊,宏哥没有收他们的广告费啊,纯粹是 ...

  4. Pytorch学习笔记08----优化器算法Optimizer详解(SGD、Adam)

    1.优化器算法简述 首先来看一下梯度下降最常见的三种变形 BGD,SGD,MBGD,这三种形式的区别就是取决于我们用多少数据来计算目标函数的梯度,这样的话自然就涉及到一个 trade-off,即参数更 ...

  5. Java交换数组元素

    Java 交换数组元素 代码示例 import java.util.Arrays; import java.util.Collections; import java.util.List; impor ...

  6. Android Bitmap 全面解析(二)加载多张图片的缓存处理

    一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~ ...

  7. maven内存溢出解决方法

    maven内存溢出(InvocationTargetException: PermGen space) 解决方案:maven脚本:mvn.bat文件@REM set MAVEN_OPTS=-Xdebu ...

  8. 【Services】【Web】【tomcat】配置tomcat支持https传输

    1. 基础: 1.1. 描述:内网的tomcat接到外网nginx转发过来的请求之后需要和外网的客户端进行通讯,为了保证通讯内容的安装,使用tomcat使用https协议. 1.2. 链接:http: ...

  9. java多线程3:synchronized

    线程安全 多个线程共同访问一个对象的实例变量,那么就可能出现线程不安全的问题. 先看一段代码示例,定义一个对象 MyDomain1 public class MyDomain1 { private i ...

  10. 为什么在集合中不能使用int关键字作为类型

    解释: 1.Int是基本数据类型,Integer是Int的引用类型,定义集合的时候不能使用基本数据类型,需要使用对应的引用类型 2.int是基本数据类型,Integer是他的包装类,包装类主要用在类型 ...