Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem G:
Flipping Parentheses

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$

$q_1$

.

.

.

$q_Q$

The first line consists of two integers $N$ and $Q$ ($2 \leq N \leq 300000$, $1 \leq Q \leq 150000$). The second line is a string $s$ of balanced parentheses with length $N$. Each of the following $Q$ lines is an integer $q_i$ ($1 \leq q_i \leq N$) that indicates that the direction of the $q_i$-th parenthesis is flipped.

Output

For each event $q_i$, 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 $q_i$ is applied to the string after the previous flip $q_{i−1}$ and its fix.

Sample Input 1

6 3
((()))
4
3
1

Sample Output 1

2
2
1

Sample Input 2

20 9
()((((()))))()()()()
15
20
13
5
3
10
3
17
18

Sample Output 2

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 "(()())".

Solution in C++ (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#define INF 1000000
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class segtree{
public:
	static const int N=1<<19;
	int dp[1<<20];
	segtree(){
		memset(dp,0,sizeof(dp));
	}
	void update(int k,int v){
		k+=N-1;
		dp[k]=v;
		while(k>0){
			k=(k-1)/2;
			dp[k]=min(dp[k*2+1],dp[k*2+2]);
		}
	}

	int query(int a,int b,int k=0,int l=0,int r=N){
		if(b<=l || r<=a)return INF;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		int vl=query(a,b,k*2+1,l,mid);
		int vr=query(a,b,k*2+2,mid,r);
		return min(vl,vr);
	}
};

class rsegtree{
public:
	static const int N=1<<19;
	int dp[1<<20];
	int lazy[1<<20];
	rsegtree(){
		memset(dp,0,sizeof(dp));
	}

	void lazy_eval(int k){
		dp[k]+=lazy[k];
		if(k<N-1){
			lazy[k*2+1]+=lazy[k];
			lazy[k*2+2]+=lazy[k];
		}
		lazy[k]=0;
	}
	void lazy_upd(int k){
		dp[k]=min(dp[k*2+1],dp[k*2+2]);
	}

	void update(int a,int b,int v,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return;
		if(a<=l && r<=b){
			lazy[k]+=v;
			lazy_eval(k);
			return;
		}
		int mid=(l+r)/2;
		update(a,b,v,k*2+1,l,mid);
		update(a,b,v,k*2+2,mid,r);
		lazy_upd(k);
	}

	int query(int a,int b,int k=0,int l=0,int r=N){
		lazy_eval(k);
		if(b<=l || r<=a)return INF;
		if(a<=l && r<=b)return dp[k];
		int mid=(l+r)/2;
		int vl=query(a,b,k*2+1,l,mid);
		int vr=query(a,b,k*2+2,mid,r);
		lazy_upd(k);
		return min(vl,vr);
	}
};

segtree seg;
rsegtree rseg;
int n,q;
string str;
int type[300005];

int main(void){
	scanf("%d%d",&n,&q);
	cin >> str;
	for(int i=0;i<n;i++){
		if(str[i]=='('){
			type[i]=1;
			seg.update(i,INF);
			rseg.update(i+1,n+1,1);
		}else{
			type[i]=-1;
			seg.update(i,i);
			rseg.update(i+1,n+1,-1);
		}
	}
	for(int i=0;i<q;i++){
		int a;
		scanf("%d",&a);
		a--;
		type[a]=-type[a];
		if(type[a]==-1){
			seg.update(a,a);
			rseg.update(a+1,n+1,-2);
			int v=seg.query(0,n);
			printf("%d\n",v+1);
			seg.update(v,INF);
			rseg.update(v+1,n+1,2);
			type[v]=-type[v];
		}else{
			seg.update(a,INF);
			rseg.update(a+1,n+1,2);
			int l=0,r=a+1;
			while(l+1<r){
				int mid=(l+r)/2;
				rseg.update(mid,n+1,-2);
				if(rseg.query(mid,n+1)>=0)r=mid;
				else l=mid;
				rseg.update(mid,n+1,2);
			}
			printf("%d\n",r);
			seg.update(r-1,r-1);
			rseg.update(r,n+1,-2);
		}
	}
	return 0;
}