1809: Parenthesis

Submit Page     Summary    Time Limit: 5 Sec     Memory Limit: 128 Mb     Submitted: 1500     Solved: 398


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

Source

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

题目大意:

给你一个长度为N的括号匹配串,然后有q个查询,表示交换两个位子上的括号之后,询问你字符串还是否构成括号匹配。

思路:

首先预处理出一个前缀和sum【i】,设定(表示1,)表示-1.那么我们开始分类讨论:

①a【x】==a【y】,那么结果一定是Yes.

②a【x】==‘)’&&a【y】==‘(’,那么结果也一定是Yes.因为如果左边的右括号放置在了右边,可以和放置在左边的这个左括号进行匹配,原串保证是匹配的,所以这样交换的结果也一定是Yes.

③a【x】==‘(’&&a【y】==‘)’,那么考虑对前缀和的影响:

撤销x位子上的左括号:从x-------------->n 前缀和全部减去1

撤销y位子上的右括号:从y-------------->n 前缀和全部加上1

放置x位子上那个右括号:从x----------->n 前缀和全部减去1

放置y位子上那个左括号:从y----------->n 前缀和全部加上1

显然,如果有某个前缀和的位子上出现了负数,那么此时这个字符串一定是构不成括号匹配的,所以我们只要判定影响到的部分是否会出现负数即可。

显然,从y---------->n的部分都加上了2,从x---------->y-1的部分都减去了1.

那么我们只要查询之前前缀和中区间【x,y-1】中的最小值是否大于等于2即可,如果是,结果就是Yes.否则就是No.

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 100050
#define inf_int 2e9
int sum[maxn*],val[maxn];
char s[maxn];
void push_up(int i){
sum[i] = min(sum[i<<],sum[i<<|]);
}
void build(int i,int l,int r){
if(l==r){
sum[i] = val[l];
return;
}
int mid = (l+r) >> ;
build(i<<,l,mid);
build(i<<|,mid+,r);//i<<1|1 把i左移1位,然后和1按位或
push_up(i);
}
int querry(int i,int l,int r,int L,int R){
if(L<=l && R>=r)
return sum[i];
int ans = inf_int;
int mid = (l+r) >> ;
if(L<=mid)
ans = min(ans,querry(i<<,l,mid,L,R));
if(R>mid)
ans = min(ans,querry(i<<|,mid+,r,L,R));
return ans;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)) {
scanf("%s",s+);
memset(val,,sizeof(val));
for(int i=;i<=n;i++){
if(s[i]=='(') val[i] = val[i-] + ;
else val[i] = val[i-] - ;
}
build(,,n);
while(m--){
int l,r;
scanf("%d%d",&l,&r);
if(l>r)
swap(l,r);
if(s[l]=='(' && s[r]==')'){
if(querry(,,n,l,r-)<)
printf("No\n");
else printf("Yes\n");
}
else printf("Yes\n");
}
}
return ;
}

CSU 1809 Parenthesis 思维+线段树的更多相关文章

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

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

  2. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  3. CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...

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

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

  5. 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...

  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. CSU 1809 Parenthesis(RMQ-ST+思考)

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

  8. 牛客练习赛53 E-老瞎眼pk小鲜肉(思维+线段树+离线)

    前言 听说是线段树离线查询?? 做题做着做着慢慢对离线操作有点感觉了,不过也还没参透,等再做些题目再来讨论离线.在线操作. 这题赛后看代码发现有人用的树状数组,$tql$.当然能用树状数组写的线段树也 ...

  9. csu 1809 Parenthesis

    题目见此 分析,把'('当成1, ')'当成-1, 计算前缀和sum. 记交换括号左边的序号为u, 右边为v,讨论左右括号: 1.s[u] == '(' && s[v] == ')' ...

随机推荐

  1. HTML 第4章初始CSS3

    什么是CSS? CSS全称为层叠样式表,通常又称为风格样式表. 引用CSS样式: 语法: <h1 styske="color:red;">style属性的应用</ ...

  2. [ubuntu][deepin]系统增加自定义开机启动项

    [ubuntu][deepin]系统增加自定义开机启动项 进行配置 cd /etc/init.d/ ls vim myScript nginx实例 #! /bin/sh # chkconfig: # ...

  3. 你所不知道的 CSS 负值技巧与细节

    写本文的起因是,一天在群里有同学说误打误撞下,使用负的 outline-offset 实现了加号.嗯?好奇的我马上也动手尝试了下,到底是如何使用负的 outline-offset 实现加号呢? 使用负 ...

  4. Java中...的作用

    Java中...的作用,代表接收若干个相同类型的参数 public void testFunction(int...arr){    //接收若干个int类型的参数     for (int i:ar ...

  5. alluxio2.0特性-预览

    项目地址 https://github.com/Alluxio/alluxio/tree/branch-2.0-preview 2.0版本-构思和设计 支持超大规模数据工作负载 Alluxio作为计算 ...

  6. [原创实践]IBM thinkpad T61制作和使用recovery光盘进行出厂系统恢复

    制作系统恢复盘 之前制作了系统恢复光盘,包含Product recovery 光盘1和光盘2,rescure and recovery光盘. 联想笔记本XP系统有一个硬盘分区是用来做恢复的,双击硬盘即 ...

  7. 关于Linux的简单介绍

    Linux: 诞生日期:1991年 开发者:林纳斯·托瓦茨 特点:免费,开源    发行版本:centos|red Hat|Ubuntu|红旗等    思想:一切都是文件 重要文件目录 bin:二进制 ...

  8. git:将代码提交到远程仓库(码云)

    初始化 进入一个任意的文件夹(如D:\aqin_test1\) git init # 初始化,让git将这个文件夹管理起来 git add . # 收集此文件夹下的所有文件 git config -- ...

  9. 洛谷 P1631 序列合并

    题意简述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N^2个和,求这N^2个和中最小的N个. 题解思路 大根堆,先存入n个和,再比较大小,改变堆中元素. 代码 #include & ...

  10. Sqlserver 存储过程中使用事务

    ALTER PROCEDURE [dbo].[Purchase_Create]@Docid varchar(100),    ----  搜索唯一编号@Title varchar(100),    - ...