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 ...
随机推荐
- JavaWeb学习记录(十七)——JSP九大隐式对象
public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOE ...
- 课堂所讲整理:Set和Map
Set相关知识: package org.hanqi.array; import java.util.*; public class Test2 { public static void main(S ...
- java的nio之:浅析I/O模型
也许很多朋友在学习NIO的时候都会感觉有点吃力,对里面的很多概念都感觉不是那么明朗.在 进入Java NIO编程之前,我们今天先来讨论一些比较基础的知识:I/O模型.下面本文先从同步和异步的概念 说起 ...
- ps色阶
三原色
- JSBinding + SharpKit / Important Notes
Serialization of List<T> is not supported. 1 public int v; // SUPPORTED 2 public GameObject go ...
- CSS3弹性盒模型之box-flex
对于之前讲过的box-sizing属性,对于页面布局很有用,但是突然发现它依然存在一些问题,前面例子中不会存在问题,不代表它没有问题.如果元素的个数整除100%的时候呢,比较3个元素,那么第一个盒子的 ...
- unity, GL.TexCoord or GL.Color must put before GL.Vertex!!!
GL.Begin(GL.QUADS); //in unity, should use left hand rule //RU GL.TexCo ...
- 解决 nginx https反向代理http协议 302重定向localtion到http问题
location /rest { #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remot ...
- Readonly和Disabled的区别
readonly 把输入的字段设为只读,但是没有禁用 readonly=” readonly”; disabled 禁用一个input元素. disabled="disabled" ...
- openstack(liberty): devstack之screen
在devstack的stack.sh文件中,可以看到所有配置的service启动方式有两种,根据是否USE_SCREEN进行是在screen window中启动,还是直接起. 默认情况,USE_SCR ...