CSU 1809 Parenthesis(线段树+前缀和)
Parenthesis
Problem 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:
S is empty;
or there exists balanced parenthesis sequence A,B such that S=AB;
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
【题目链接】CSU 1809 Parenthesis
【题目类型】线段树+前缀和
&题意:
给了一个平衡的括号序列s(平衡是指括号匹配正常)现在q次询问,每次输入两个数a、b;问将s[a]和s[b]交换后是否仍然平衡(即是否正常匹配)平衡则输出“Yes”,否则输出“No”
&题解:
这种括号的题目有一种经典的套路就是遇到'('加一,遇到')'减一,这样整个序列最后的前缀和一定是非负的,同样的这里贪心一下就会发现只有把'(' 和')'交换的时候才会出问题,这时我们就要想一下了,有了括号的套路,你只给一个数组赋值正负一是没什么意义的,所以你要求他的前缀和,放在pre数组中,这样假设每次修改的区间是u,v,也就是只有当s[u] == '(' && s[v] == ')'才需要判断,且u<=v,那么要判断什么呢?你想,正确情况下他的pre是不会有<0的情况的,但如果换了之后,前面u的位置换成了'('就要减1,后面v的位置是加1的,所以不用管,这时候pre在区间[u,v-1]的最小值>=2才可以。
【时间复杂度】O(nlogn)
&代码:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define rep(i,b) for(int i=0;i<(b);i++)
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
const int MAXN = 100000 + 5 ;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[MAXN << 2], pre[MAXN];
inline void Pushmin(int rt) {
sum[rt] = min(sum[rt << 1], sum[rt << 1 | 1]);
}
void Build(int l, int r, int rt) {
if (l == r) {
sum[rt] = pre[l];
return;
}
int m = (l + r) >> 1;
Build(lson);
Build(rson);
Pushmin(rt);
}
int Query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
return sum[rt];
}
int m = (l + r) >> 1;
int ans = INF;
if (L <= m)
ans = min(ans, Query(L, R, lson));
if (R > m)
ans = min(ans, Query(L, R, rson));
return ans;
}
int n, q;
char s[MAXN];
void Solve() {
while (~SII(n, q)) {
scanf("%s", s + 1);
rez(i, 1, n) if (s[i] == '(') pre[i] = pre[i - 1] + 1;
else pre[i] = pre[i - 1] - 1;
Build(1, n, 1);
rep(i, q) {
int u, v;
SII(u, v);
if (v < u) swap(u, v);
if (s[u] == '(' && s[v] == ')') {
int d = Query(u, v - 1, 1, n, 1);
if (d < 2) puts("No");
else puts("Yes");
}
else puts("Yes");
}
}
}
int main() {
Solve();
return 0;
}
CSU 1809 Parenthesis(线段树+前缀和)的更多相关文章
- 湖南省2016省赛题。1809: Parenthesis 线段树
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 给定一串平衡的序列,要求交换两个位置之后,问其是否还平衡. 首先要注意到交换的是两个位置,这 ...
- HDU 5172 GTY's gay friends 线段树+前缀和+全排列
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5172 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- CSU 1809 - Parenthesis - [前缀和+维护区间最小值][线段树/RMQ]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1809 Bobo has a balanced parenthesis sequenc ...
- CSU 1809 Parenthesis 思维+线段树
1809: Parenthesis Submit Page Summary Time Limit: 5 Sec Memory Limit: 128 Mb Submitte ...
- 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...
- CSU 1809 Parenthesis(RMQ-ST+思考)
1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...
- csu 1809 Parenthesis
题目见此 分析,把'('当成1, ')'当成-1, 计算前缀和sum. 记交换括号左边的序号为u, 右边为v,讨论左右括号: 1.s[u] == '(' && s[v] == ')' ...
- 【BZOJ】1651: [Usaco2006 Feb]Stall Reservations 专用牛棚(线段树/前缀和 + 差分)
http://www.lydsy.com/JudgeOnline/problem.php?id=1651 很奇妙.. 我们发现,每一时刻的重叠数选最大的就是答案.... orz 那么我们可以线段树维护 ...
- 【vijos】1750 建房子(线段树套线段树+前缀和)
https://vijos.org/p/1750 是不是我想复杂了.... 自己yy了个二维线段树,然后愉快的敲打. 但是wa了两法.......sad 原因是在处理第二维的更新出现了个小问题,sad ...
随机推荐
- mysql 新建用户
//=============================================================================== //代理商子账户订单服务器为了安全 ...
- Python学习(3)——if语句
虽然在之前接触过C.C++.Java等,但是还是觉得Python写出来的好看o(≧v≦)o~,简洁明了! score = raw_input("score:") score=int ...
- react 不能往组件中传入属性的值为 undefined
在使用 andt design 的时候遇到个需求,需要清除 Select 组件选中后的值,让它变成什么都没选中,显示 placeholder 刚开始以为设置为 null 即可,结果发现设置为 null ...
- 控制文本和外观------Style Binding(Style属性绑定)
目的 style绑定是添加或删除一个或多个DOM元素上的style值.比如当数字变成负数时高亮显示,或者根据数字显示对应宽度的Bar.(注:如果你不是应用style值而是应用CSS class的话,请 ...
- flashbuilder发布release版本
http://blog.vini123.com/1275.html 方法二:选择项目,点击“文件”-“导出”-“Flash Builder”-“发行版”,然后下一步. 方法三:选择项目,右键“属性”, ...
- 【转】SocketRocket:iOS WebSocket客户端开源框架
原文网址:http://blog.csdn.net/zmp1123/article/details/44015507 WebSocket: WebSocket通信协议实现的是基于浏览器的原生socke ...
- Html-双斜杠//开头的URL(依赖协议的URL)
今天看京东HTTPS,发现链接都是这种 <a href="//chaoshi.jd.com">京东超市</a> URL是以双斜杠"//" ...
- linux free命令中buffer与cache的区别
linux free命令中buffer与cache的区别 2012-05-15 个评论 收藏 我要投稿 linux free命令中buffer与cache的区别 ~$ ...
- 013. asp.net统计网站访问人数
Global.asax中的代码: <%@ Application Language="C#" %> <script runat="server" ...
- js移动焦点到最后
当输入框(input/textarea)获得焦点时,将焦点移动到最后,在某些情况下用户体验很好.网上的大部分方法都是针对IE浏览器的.代码如下: var el = document.getElem ...