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。。。
这样从右往左找,搜线段树。。。
具体做法参考代码。。。
1 #include <bits/stdc++.h> 2 #define INF 0x3f3f3f3f 3 using namespace std; 4 const int maxn = 500000; 5 struct node { 6 int lt,rt,theFirst,theMin,lazy,dir; 7 } tree[maxn<<2]; 8 int sum,n,m; 9 char str[maxn]; 10 void build(int lt,int rt,int v) { 11 tree[v].lt = lt; 12 tree[v].rt = rt; 13 tree[v].lazy = 0; 14 if(lt == rt) { 15 tree[v].dir = str[lt] == '('?1:-1; 16 sum += tree[v].dir; 17 tree[v].theMin = sum; 18 tree[v].theFirst = tree[v].dir == 1?INF:lt; 19 return; 20 } 21 int mid = (lt + rt)>>1; 22 build(lt,mid,v<<1); 23 build(mid+1,rt,v<<1|1); 24 tree[v].theFirst = min(tree[v<<1].theFirst,tree[v<<1|1].theFirst); 25 tree[v].theMin = min(tree[v<<1].theMin,tree[v<<1|1].theMin); 26 } 27 int update(int p,int v) { 28 if(tree[v].lt >= p && tree[v].rt <= p) { 29 int tmp = tree[v].dir; 30 tree[v].dir *= -1; 31 tree[v].theFirst = tmp == 1?p:INF; 32 return tmp; 33 } 34 int mid = (tree[v].lt + tree[v].rt)>>1,ret; 35 if(p <= mid) ret = update(p,v<<1); 36 if(p > mid) ret = update(p,v<<1|1); 37 tree[v].theFirst = min(tree[v<<1].theFirst,tree[v<<1|1].theFirst); 38 return ret; 39 } 40 void pushdown(int v) { 41 if(tree[v].lazy) { 42 tree[v<<1].lazy += tree[v].lazy; 43 tree[v<<1|1].lazy += tree[v].lazy; 44 tree[v].lazy = 0; 45 } 46 } 47 void pushup(int v) { 48 tree[v].theMin = min(tree[v<<1].theMin+tree[v<<1].lazy,tree[v<<1|1].theMin+tree[v<<1|1].lazy); 49 } 50 void update(int lt,int rt,int v,int value) { 51 if(tree[v].lt >= lt && tree[v].rt <= rt) { 52 tree[v].lazy += value; 53 return; 54 } 55 pushdown(v); 56 int mid = (tree[v].lt + tree[v].rt)>>1; 57 if(lt <= mid) update(lt,rt,v<<1,value); 58 if(rt > mid) update(lt,rt,v<<1|1,value); 59 pushup(v); 60 } 61 int query(int v,int o) { 62 if(tree[v].theMin+tree[v].lazy >= 2) return tree[v].lt; 63 if(tree[v].lt == tree[v].rt) return o; 64 pushdown(v); 65 int ret; 66 if(tree[v<<1|1].theMin+tree[v<<1|1].lazy >= 2) 67 ret = query(v<<1,tree[v<<1|1].lt); 68 else ret = query(v<<1|1,o); 69 pushup(v); 70 return ret; 71 } 72 int main() { 73 int p; 74 while(~scanf("%d %d",&n,&m)) { 75 scanf("%s",str+1); 76 sum = 0; 77 build(1,n,1); 78 while(m--) { 79 scanf("%d",&p); 80 int tmp = update(p,1); 81 update(p,n,1,tmp*-2); 82 printf("%d ",p = tmp == 1?tree[1].theFirst:query(1,n)); 83 update(p,1); 84 update(p,n,1,tmp*2); 85 } 86 } 87 return 0; 88 }