1809: Parenthesis

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

HINT

题意:

  给你长度n的合法括号匹配和q个询问

  每次询问你 交换ai,bi两个位置的符号,交换后是否还是合法的

题解:

  分块

  (转化为1,)转化为-1

  合法括号序列满足前缀和永远大于等于0

  利用这个每次修改两个位置

  维护这个关系就好了

  分块可过的

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 1e6+, mod = 1e6+, inf = ; int block,n,q,m;
int sum[N],pos[N],mi[N],a[N],b[N],add[N],f[N];
char ch[N];
void init() {
int s = ;
pos[] = ;
memset(b,,sizeof(b));
memset(sum,,sizeof(sum));
memset(add,,sizeof(add));
for(int i = ; i <= n; ++i) b[i] = a[i];
for(int i = ; i <= n; ++i) sum[i] = sum[i-] + b[i];
for(int i = ; i < N; ++i) mi[i] = inf;
for(int i = ; i <= n; ++i) {
if(pos[i] != pos[i-]) s = ;
s += b[i];
mi[pos[i]] = min(mi[pos[i]],s);
}
for(int i = ; i <= n; ++i) add[pos[i]] += b[i];
f[] = ;
s = ;
for(int i = ; i <= n; ++i) {
s+=b[i];
if(s < ) f[i] = ;
else f[i] = f[i-];
}
}
int solve(int l,int r) {
int OK = ;
swap(b[l],b[r]);
if(pos[l] == pos[r]) {
int s = sum[block * (pos[l]-)];
for(int i = block * (pos[l]-) + ; i <= min(pos[l] * block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[l]+; i <= m; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}if(s < ) OK = ;
} else {
int s = sum[block * (pos[l]-)];
for(int i = block*(pos[l]-) + ; i <= min(pos[l] * block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[l]+; i <= pos[r]-; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}
for(int i = block * (pos[r]-) + ; i <= min(pos[r]*block,n); ++i) {
s += b[i];
if(s < ) OK = ;
}
for(int i = pos[r]+; i <= m; ++i) {
if(s + mi[i] < ) OK = ;
s += (add[i]);
}
}
swap(b[l],b[r]);
if(OK == ) return ;
else return ;
}
int main() {
while(scanf("%d%d",&n,&q)!=EOF) {
scanf("%s",ch);
block = int(sqrt(n));
memset(a,,sizeof(a));
memset(pos,,sizeof(pos));
for(int i = ; i <= n; i++) {if(ch[i-] == '(') a[i] = ; else a[i] = -; pos[i]=(i-)/block+;}
if(n%block) m = n/block + ; else m = n/block;
init();
for(int i = ; i <= q; ++i) {
int l,r;
scanf("%d%d",&l,&r);
if(l > r) swap(l,r);
if(solve(l,r) == ) printf("Yes\n");
else printf("No\n");
}
}
return ;
}

湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis的更多相关文章

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

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

  2. 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类

    Problem A: 2016 Time Limit: 5 Sec  Memory Limit: 128 MB Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...

  3. 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路

    1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...

  4. 湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP

    1804: 有向无环图 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 187  Solved: 80[Submit][Status][Web Board ...

  5. 湖南省第十二届大学生计算机程序设计竞赛 A 2016

    1803: 2016 Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:   1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. In ...

  6. 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...

  7. 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...

  8. 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...

  9. 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...

随机推荐

  1. 1.把二元查找树转变成排序的双向链表[BST2DoubleLinkedList]

    [题目]:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表.要求不能创建任何新的结点,只调整指针的指向. 比如将二元查找树 . 10 / \ 6 14 / \ / \ 4 8 12 16 转 ...

  2. ACM/ICPC 之 Floyd范例两道(POJ2570-POJ2263)

    两道以Floyd算法为解法的范例,第二题如果数据量较大,须采用其他解法 POJ2570-Fiber Network //经典的传递闭包问题,由于只有26个公司可以采用二进制存储 //Time:141M ...

  3. 数据结构——B树、B+树

    B树和B+树主要应用于外排序,对于外排序,从硬盘读取的时间要远远大于遍历树的时间,因此要想办法减少从硬盘读取的时间. B树(有时也叫B-树) M阶B树定义如下: 是一种多路搜索树(并不是二叉的):1. ...

  4. cookie相关内容

    ----客户端删除cookie <script type="text/javascript">                        function DelC ...

  5. HTML DOM scale() 方法

    语法 scale(sx, sy) 参数 参数 描述 sx, sy 水平和垂直的缩放因子. 描述 scale() 方法为画布的当前变换矩阵添加一个缩放变换.缩放通过独立的水平和垂直缩放因子来完成.例如, ...

  6. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  7. 非Unicode工程读取Unicode文件

    MyUnicodeReader.h #pragma once /******************************************************************** ...

  8. 单独编译osgQt模块 Qt moc

    从alphapixel网站下载了OSG3.0.1VS2010x64版本的库,但是里面不包括osgQt模块,于是得自己编译 *************osgQtx64.zip工程文件可以去本博客园的“文 ...

  9. 添加thrust的库后出错

    在添加thrust库中的host_vector.h等头文件时 C:\NVIDIA\cudatoolkit\include\thrust\detail\config中的debug.h一直出问题,因此注释 ...

  10. C/C++不同文件夹下包含头文件的方法及#include的使用

    转自:http://blog.sina.com.cn/s/blog_6e0693f70100so42.html 本文主要介绍了如何不同文件夹下使用预处理器指示符#include. 假设我们有如下一个工 ...