CSUOJ 1542 Flipping Parentheses
ACM International Collegiate Programming Contest
Asia Regional Contest, Tokyo, 2014–10–19
Problem G
Flipping Parentheses
Input: Standard Input
Time Limit: 5 seconds
A string consisting only of parentheses ‘(’ and ‘)’ is called balanced if it is one of the following.
- A string “()” is balanced.
- Concatenation of two balanced strings are balanced.
- When a string s is balanced, so is the concatenation of three strings “(”, s, and “)” in this order.
Note that the condition is stronger than merely the numbers of ‘(’ and ‘)’ are equal. For instance, “())(()” is not balanced.
Your task is to keep a string in a balanced state, under a severe condition in which a cosmic ray may flip the direction of parentheses.
You are initially given a balanced string. Each time the direction of a single parenthesis is flipped, your program is notified the position of the changed character in the string. Then, calculate and output the leftmost position that, if the parenthesis there is flipped, the whole string gets back to the balanced state. After the string is balanced by changing the parenthesis indicated by your program, next cosmic ray flips another parenthesis, and the steps are repeated several times.
Input
The input consists of a single test case formatted as follows.
N Q s
q1
...
The first line consists of two integers N and Q (2 ≤N ≤ 300000, 1 ≤Q≤ 150000). The second line is a string s of balanced parentheses with length N. Each of the following Q lines is an integer qi (1 ≤qi ≤N) that indicates that the direction of the qi-th parenthesis is flipped.
Output
For each event qi, output the position of the leftmost parenthesis you need to flip in order to get back to the balanced state.
Note that each input flipping event qi is applied to the string after the previous flip qi−1 and its fix.
Sample Input 1 Sample Output 1
6 3 ((())) 4 3 1 |
2 2 1 |
Sample Input 2 Sample Output 2
20 9 ()((((()))))()()()() 15 20 13 5 3 10 3 17 18 |
2 20 8 5 3 2 2 3 18 |
In the first sample, the initial state is “((()))”. The 4th parenthesis is flipped and the string becomes “(((())”. Then, to keep the balance you should flip the 2nd parenthesis and get “()(())”. The next flip of the 3rd parenthesis is applied to the last state and yields “())())”. To rebalance it, you have to change the 2nd parenthesis again yielding “(()())”.
解题:利用线段树进行维护
利用前缀和 比如(())那么前缀和分别就是1、2、1、0。观察到,平衡时前缀和都是不小于0的。
现在进行反转,将一个(翻转成)会使得从当前位置开始的前缀和都减-2,为毛是减2呢?因为(这个算1,)这是算-1的,从1到-1就是2。为了维护前缀和,所以从翻转位置开始所有的后缀和都要减2。那么我们如何使得这堆括号再次平衡呢?因为是减了2,所以要找个位置,把此位置的括号翻转,能够使得后面的前缀和+2,以此来保证各前缀和都不小于0,达到平衡。做法就是从左往右,找到第一个右括号,使其翻转。
如果将)翻转成(怎么办呢?同理,此位置开始以后的所有前缀和都要+2,这样全部和又不为0了,那怎么办啊?平衡状态,所有括号方向值的和是为0的。现在是大于0,我们要减回去,当然是找一个(括号,翻转成)。那么找哪个(呢?由于要减回去,还使得各前缀和不能出现负的,所以必须找一段这样的,从最后的前缀和往前走,都得不小于2,以此保证减2后不会为负,这样连续的一段,最前面的位置,就是我们要找的那个(。然后。。。哼哼。。。。
可是可是怎么这样一段连续的不小于2的区间呢?
我们只要维护该区间的最小值即可,只要最小值不小于2,那么该区间的所有值都不会小于2。。。
这样从右往左找,搜线段树。。。
具体做法参考代码。。。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct node {
int lt,rt,theFirst,theMin,lazy,dir;
} tree[maxn<<];
int sum,n,m;
char str[maxn];
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
tree[v].lazy = ;
if(lt == rt) {
tree[v].dir = str[lt] == '('?:-;
sum += tree[v].dir;
tree[v].theMin = sum;
tree[v].theFirst = tree[v].dir == ?INF:lt;
return;
}
int mid = (lt + rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].theFirst = min(tree[v<<].theFirst,tree[v<<|].theFirst);
tree[v].theMin = min(tree[v<<].theMin,tree[v<<|].theMin);
}
int update(int p,int v) {
if(tree[v].lt >= p && tree[v].rt <= p) {
int tmp = tree[v].dir;
tree[v].dir *= -;
tree[v].theFirst = tmp == ?p:INF;
return tmp;
}
int mid = (tree[v].lt + tree[v].rt)>>,ret;
if(p <= mid) ret = update(p,v<<);
if(p > mid) ret = update(p,v<<|);
tree[v].theFirst = min(tree[v<<].theFirst,tree[v<<|].theFirst);
return ret;
}
void pushdown(int v) {
if(tree[v].lazy) {
tree[v<<].lazy += tree[v].lazy;
tree[v<<|].lazy += tree[v].lazy;
tree[v].lazy = ;
}
}
void pushup(int v) {
tree[v].theMin = min(tree[v<<].theMin+tree[v<<].lazy,tree[v<<|].theMin+tree[v<<|].lazy);
}
void update(int lt,int rt,int v,int value) {
if(tree[v].lt >= lt && tree[v].rt <= rt) {
tree[v].lazy += value;
return;
}
pushdown(v);
int mid = (tree[v].lt + tree[v].rt)>>;
if(lt <= mid) update(lt,rt,v<<,value);
if(rt > mid) update(lt,rt,v<<|,value);
pushup(v);
}
int query(int v,int o) {
if(tree[v].theMin+tree[v].lazy >= ) return tree[v].lt;
if(tree[v].lt == tree[v].rt) return o;
pushdown(v);
int ret;
if(tree[v<<|].theMin+tree[v<<|].lazy >= )
ret = query(v<<,tree[v<<|].lt);
else ret = query(v<<|,o);
pushup(v);
return ret;
}
int main() {
int p;
while(~scanf("%d %d",&n,&m)) {
scanf("%s",str+);
sum = ;
build(,n,);
while(m--) {
scanf("%d",&p);
int tmp = update(p,);
update(p,n,,tmp*-);
printf("%d\n",p = tmp == ?tree[].theFirst:query(,n));
update(p,);
update(p,n,,tmp*);
}
}
return ;
}
CSUOJ 1542 Flipping Parentheses的更多相关文章
- CSU - 1542 Flipping Parentheses (线段树)
CSU - 1542 Flipping Parentheses Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Flipping Parentheses~Gym 100803G
Description A string consisting only of parentheses '(' and ')' is called balanced if it is one of t ...
- Flipping Parentheses(CSU1542 线段树)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1542 赛后发现这套题是2014东京区域赛的题目,看了排名才发现自己有多low = =! 题目大意 ...
- CSUOJ 1542 线段树解决括号反向问题
题目大意: 根据初始给定的合法的小括号排序,每次进行一个操作,将第a位的括号反向,找到一个尽可能靠前的括号反向后是整个括号排列合法 数据量十分大,不断进行查询,要用线段树进行logn的复杂度的查询 首 ...
- Gym 100803G Flipping Parentheses
题目链接:http://codeforces.com/gym/100803/attachments/download/3816/20142015-acmicpc-asia-tokyo-regional ...
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest
2014-2015 ACM-ICPC, Asia Tokyo Regional Contest A B C D E F G H I J K O O O O O O A - Bit ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
随机推荐
- jsp布局中关于<iframe>标签的使用
iframe 元素会创建包括另外一个文档的内联框架(即行内框架). 注意:在 HTML 4.1 Strict DTD 和 XHTML 1.0 Strict DTD 中,不支持 iframe 元素. & ...
- 如何在阿里云服务器里配置iis 搭建web服务
IIS,互联网信息服务,一种Web服务组件,利用它,我们可以打开asp.php这些搭建网页所用的文件. 工具/原料 域名 服务器 方法/步骤 登录服务器. 点击开始—>服务器 ...
- 编写SDR SDRAM页突发模式控制器的注意点-下篇
本来是没打算写这些的,但是后面逐渐发现点问题,所以决定再写一个下篇来补充说明一下. 图一 细心的网友会发现上篇末尾的打印是有点问题的,因为我的数据产生器产生的是1-200,1-200,1-200,1- ...
- django 笔记5 外键 ForeignKey
class UsserGroup(models.Model): uid = models.AutoField(primary_key=True) caption = models.CharField( ...
- PostgreSQL Replication之第四章 设置异步复制(4)
4.4 基于流和基于文件的恢复 生活并不总只是黑色或白色:有时也会有一些灰色色调.对于某些情况下,流复制可能恰到好处.在另一些情况下,基于文件复制和PITR是您所需要的.但是也有许多情况下,您既需要流 ...
- ng-show ng-hide ng-if的区别
用途 ng-show ng-hide ng-if三个都可以用来控制页面DOM元素的显示与隐藏. ng-hide条件为true时,隐藏所在元素,false时显示所在元素. ng-show相反,条件为tr ...
- appium使用教程(二)-------------连接手机
1. 安装驱动 说明:如果驱动装不上,可以使用第三方的工具去安装.(一般来说还是用第三方) 大概就是这个样子索. 2. 开启usb调试 1)开发者选项打开(不知道怎么打开的问度娘) 2)开启USB调试 ...
- Linux服务器性能评估与优化
一.影响务器性能因素 影响企业生产环境Linux服务器性能的因素有很多,一般分为两大类,分别为操作系统层级和应用程序级别.如下为各级别影响性能的具体项及性能评估的标准: (1)操作系统级别 内存: C ...
- tab.py
vim tab.py #!/usr/bin/env python # #Tab import sys import readline import rlcompleter import atexit ...
- python 异步IO-aiohttp与简单的异步HTTP客户端/服务器
参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143209814 ...