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 ...
 
随机推荐
- 越狱Season 1-Episode 17: J-Cat
			
Season 1, Episode 17: J-Cat -Pope: Hey, that's looking good. 嗨,看起来真棒 You're making some real progres ...
 - 【JZOI2002】【BZOJ1477】【P1371】青蛙的约会
			
看lzx的模板才写出来的,我之前的思路好想错了 chad_orz 原题: 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝着对方 ...
 - java的nio之:java的nio系列教程之SocketChannel
			
Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道.可以通过以下2种方式创建SocketChannel: 打开一个SocketChannel并连接到互联网上的某台服务器. ...
 - java多线程:并发包中ReentrantLock锁的公平锁原理
			
一:锁的原理结构 (1)锁对象内部维护了一个同步管理器的对象AbstractQueuedSynchronizer,AbstractOwnableSynchronizer (2)该对象其实是一个抽象类, ...
 - 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach
			
From Facial Parts Responses to Face Detection: A Deep Learning Approach ICCV 2015 从以上两张图就可以感受到本文所提方法 ...
 - Microservice  Orleans
			
https://azure.microsoft.com/en-us/blog/containers-docker-windows-and-trends/ https://channel9.msdn.c ...
 - jquery下ie的margin-left   ----bug 以及parseInt方法bug
			
ie下使用jquery的方法css('margin-left')可能会出现'auto'----从而使结果不可计算,即便使用parseInt()方法也不行 因为parseInt()方法的bug是如果参数 ...
 - Linux-内存管理机制、内存监控、buffer/cache异同
			
在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,主要特点是,无论物理内存有多大,Linux 都将其充份利用,将 ...
 - java中abstract
			
abstract(抽象)修饰符,可以修饰类和方法 1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半 ...
 - SQL server 2008 Express Edition实现自动备份和自动删除备份
			
1.查看SQL 版本: select @@VERSION --可以看到 Express Edition 精简免费版 Microsoft SQL Server 2008 R2 (SP2) - 10.50 ...