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. Swift游戏实战-跑酷熊猫 07 平台的移动

    这节内容我们来实现平台是怎么产生移动动画的. 要点 1 利用数组存放平台 var platforms=[Platform]() 2 有新的平台产生存放进数组 platforms.append(plat ...

  2. Leetcode: Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  3. HTML调用servlet(二)

    5.修改数据 5.1编写查询条件页面 修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改.修改之后,数据会提交到数据库,数据库中保存更新以后的数据. 查询出单条数 ...

  4. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

  5. Android 封装Dialog

    package com.example.myandroid01; import android.support.v7.app.ActionBarActivity; import android.os. ...

  6. java正则表达式练习

    package shb.java.demo3; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 正则表达式简 ...

  7. paper 72 :高动态范围(HDR)图像 HDR (High Dynamic Range)

    In standard rendering, the red, green and blue values for a pixel are each represented by a fraction ...

  8. opencv常用数据结构之:IplImage

    typedef struct_IplImage{      int nSize; //IplImage大小      int ID; //版本(=0)      int nChannels; //大多 ...

  9. python字符串列表字典相互转换

    字符串转换成字典 json越来越流行,通过python获取到json格式的字符串后,可以通过eval函数转换成dict格式: >>> a='{"name":&qu ...

  10. exe文件打开方式(恢复EXE文件关联)

    文件关联损坏常常是计算机病毒造成的,目前网络上有很多相关修复工具,相对来说,System Repair Engineer 支持的修复格式是比较齐全的,这个工具可以在http://www.kztechs ...