Flipping Parentheses~Gym 100803G
Description
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.
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
6 3
((()))
4
3
1 20 9
()((((()))))()()()()
15
20
13
5
3
10
3
17
18
Sample Output
2
2
1
2
20
8
5
3
2
2
3
18
Hint
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 “(()())”.
这题简直爆炸,知道感觉需要用线段树维护,但是不知道查询,而且我线段树血都写不出了
好久好久没有写过线段树了,板子都不会了, 更别说应用了。
题意:就是给你一个匹配好的括号串长度为n,在给你m个操作,每个操作就给翻转摸一个位置的括号,
对于每一个操作,你要找到一括号将其翻转,使得最后这个串还是匹配的,
若存在多个解,翻转最左边的那个括号。
( 为 1, )为-1 ,然后容易联想到前缀和,
然后就是用线段树去维护这个前缀和,
如果是 x = ) 就是找到第一个 ) 这个相对简单 ,用set和线段树都可以做
如果是 x = ( 就是从左到右 找到第一个 ( 这个就只能用线段树实现了
记录了前缀和,其实只要找到一段区间的最小值都大于1就行了;
这个就是整个线段树最难的部分了,
int query1(int rt, int L, int R) {
if (L == R) return L + 1;
int mid = (L + R) >> 1;
pushdown(rt);
if (tree[rt << 1 | 1].r < 2 ) query1(rt << 1 | 1, mid + 1, R);
else query1(rt << 1, L, mid);
}
当时的我怎么也想不出。 太菜了;
注意每一个找到了y 记得更新 y
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 3e5 + ;
char s[maxn];
int sum[maxn];
struct node {
int l, r, add;
} tree[maxn * ];
int fun(int i) {
return s[i] == '(' ? : -;
}
void pushup(int rt) {
tree[rt].l = min(tree[rt << | ].l, tree[rt << ].l);
tree[rt].r = min(tree[rt << | ].r, tree[rt << ].r);
}
void pushdown(int rt) {
tree[rt << ].l += tree[rt].add;
tree[rt << ].r += tree[rt].add;
tree[rt << ].add += tree[rt].add;
tree[rt << | ].l += tree[rt].add;
tree[rt << | ].r += tree[rt].add;
tree[rt << | ].add += tree[rt].add;
tree[rt].add = ;
}
void build(int rt, int L, int R) {
tree[rt].add = ;
if (L == R) {
tree[rt].l = sum[L] - L;
tree[rt].r = sum[L];
return ;
}
int mid = (L + R) >> ;
build(rt << , L, mid);
build(rt << | , mid + , R );
pushup(rt);
}
void updata(int rt, int L, int R, int x, int add) {
if (x <= L) {
tree[rt].add += add;
tree[rt].l += add;
tree[rt].r += add;
return ;
}
pushdown(rt);
int mid = (L + R) >> ;
if (x <= mid) updata(rt << , L, mid, x, add);
updata(rt << | , mid + , R, x, add);
pushup(rt);
}
int query1(int rt, int L, int R) {
if (L == R) return L + ;
int mid = (L + R) >> ;
pushdown(rt);
if (tree[rt << | ].r < ) query1(rt << | , mid + , R);
else query1(rt << , L, mid);
}
int query2(int rt, int L, int R) {
if (L == R ) return L;
int mid = (L + R) / ;
pushdown(rt);
if (tree[rt << ].l < ) query2(rt << , L, mid);
else query2((rt << ) + , mid + , R) ;
} int main() {
int n, q, len, x, y;
while(scanf("%d%d", &n, &q) != EOF) {
s[] = '';
memset(sum, , sizeof(sum));
scanf("%s", s + );
len = strlen(s) - ;
for (int i = ; i <= len ; i++)
sum[i] = sum[i - ] + fun(i);
build(, , len);
for (int i = ; i <= q ; i++) {
scanf("%d", &x);
s[x] = (s[x] == '(' ? ')' : '('); if (s[x] == ')') updata(, , len, x, -);
else updata(, , len, x, ); if (s[x] == ')') y = query2(, , len);
else y = query1(, , len);
s[y] = (s[y] == '(' ? ')' : '(' );
if (s[y]=='(') updata(,,len,y,);
else updata(,,len,y,-);
printf("%d\n", y); } }
return ;
}
Flipping Parentheses~Gym 100803G的更多相关文章
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- CSU - 1542 Flipping Parentheses (线段树)
CSU - 1542 Flipping Parentheses Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld ...
- CSUOJ 1542 Flipping Parentheses
ACM International Collegiate Programming Contest Asia Regional Contest, Tokyo, 2014–10–19 Problem G ...
- Gym 100803G Flipping Parentheses
题目链接:http://codeforces.com/gym/100803/attachments/download/3816/20142015-acmicpc-asia-tokyo-regional ...
- Flipping Parentheses(CSU1542 线段树)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1542 赛后发现这套题是2014东京区域赛的题目,看了排名才发现自己有多low = =! 题目大意 ...
- 2017 United Kingdom and Ireland Programming(Gym - 101606)
题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...
- 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 ...
- Gym 101606F - Flipping Coins - [概率DP]
题目链接:https://codeforc.es/gym/101606/problem/F 题解: 假设 $f[i][j]$ 表示抛 $i$ 次硬币,有 $j$ 个硬币正面朝上的概率. 所以只有两种挑 ...
- Codeforces Gym 100610 Problem A. Alien Communication Masterclass 构造
Problem A. Alien Communication Masterclass Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codefo ...
随机推荐
- Oracle PL/SQL Articles
我是搬运工....http://www.oracle-base.com/articles/plsql/articles-plsql.php Oracle 8i Oracle 9i Oracle 10g ...
- 分布式进阶(十二)Docker固定Container IP
使用pipework工具. 前提:每个Container所做的工作现在还很少,可以不用save.commit. 为了便于通信,自定义一个网桥(192.168.1.180/24),使之IP与宿主主机IP ...
- Linux 安装python爬虫框架 scrapy
Linux 安装python爬虫框架 scrapy http://scrapy.org/ Scrapy是python最好用的一个爬虫框架.要求: python2.7.x. 1. Ubuntu14.04 ...
- UGUI实现NGUI的UIEventListener功能
在unity中处理UI事件时,习惯了使用NGUI的UIEventListener,虽然UGUI也有AddListener,但是一个组件只能对应一个函数,不能在一个函数中同时处理多个事件,显得有些麻烦 ...
- ffplay的快捷键以及选项
ffplay是ffmpeg工程中提供的播放器,功能相当的强大,凡是ffmpeg支持的视音频格式它基本上都支持.甚至连VLC不支持的一些流媒体都可以播放(比如说RTMP),但是它的缺点是其不是图形化界面 ...
- OC语言(四)
二十八.id类型(万能指针) 可以指向任何id对象(本身就是指针,不用*) id相当于NSObject *,类似于一种多态. 二十九.重写构造方法 new方法的实质:分配空间+alloc 和 初始化- ...
- Jumpstart for Oracle Service Bus Development
http://www.oracle.com/technetwork/articles/jumpstart-for-osb-development-page--097357.html Tutorial ...
- 套接字编程相关函数(1:套接字地址结构、字节序转换、IP地址转换)
1. 套接字地址结构 1.1 IPv4套接字地址结构 IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中.下 ...
- C 打印格式小记
转自:http://blog.csdn.net/fivedoumi/article/details/7077504 d,lx,ld,,lu,这几个都是输出32位的 hd,hx,hu,这几个都是输出16 ...
- Leetcode_205_Isomorphic Strings
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46530865 Given two strings s an ...