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 JAVA (judge verdict: Wrong Answer)

import java.util.*;

class Main{

    int n, m, sz;
    char[] kakko;
    int[] a, bkt, add;

    void solve(){
        Scanner sc = new Scanner(System.in);

        n = sc.nextInt();
        m = sc.nextInt();
        kakko = sc.next().toCharArray();

        sz = (int)Math.sqrt(n) - 1;
        a = new int[n];
        bkt = new int[n];
        add = new int[n];
        PriorityQueue<Integer> set = new PriorityQueue<Integer>(10, new Comparator<Integer>(){
                public int compare(Integer a, Integer b){
                    return (int)(a - b);
                }
            });

        a[0] = 1;
        for(int i=1; i<n; i++){
            if(kakko[i]=='(') a[i] = a[i-1] + 1;
            else{
                a[i] = a[i-1] - 1;
                set.add(new Integer(i));
            }
        }

        for(int i=0; i<n; i++){
            int min = Integer.MAX_VALUE;
            for(int j=0; j<sz && j*i<n; j++){
                min = Math.min(min, a[j * i]);
            }
            bkt[i] = min;
        }


        for(int i=0; i<m; i++){
            int q = sc.nextInt()-1;
            int idx = 0;
            if(kakko[q]=='('){
                kakko[q] = ')';
                set.add(new Integer(q));
                add(q, n, -2);
                idx = set.poll();
                kakko[idx] = '(';
                set.remove(new Integer(idx));
                add(idx, n, 2);
            }else{
                kakko[q] = '(';
                set.remove(new Integer(q));
                add(q, n, 2);
                idx = search(q-1);
                kakko[idx] = ')';
                add(idx, n, -2);
                set.add(new Integer(idx));
            }
            //System.out.println(Arrays.toString(kakko));
            System.out.println(idx+1);
        }
    }

    void add(int start, int last, int num){
        int from = 0;
        int to = sz;
        for(int i=0; i<n; i++){
            if(from<=start && last<=to){
                add[i] += num;
                //bkt[i] += num;
            }else{
                for(int j=from; j<to; j++){
                    if(start<=j && j<last){
                        a[j] += num;
                        if(a[j] < bkt[i]) bkt[i] = a[j];
                    }
                }
            }
            from = to;
            to += sz;
        }
    }

    int search(int f){
        //int from = f - f%sz;
        //int to = from + sz;
        int from = 0;
        int to = sz;
        while(!(from<=f && f<to)){
            from = to;
            to += sz;
        }
            
        for(int i=0; i<n; i++){
            if(bkt[i]+add[i]<=1){
                for(int j=Math.min(f, to-1); j>=Math.max(0, from); j--){
                    if(a[j]+add[i]==1) return j+1;
                }
            }
            to = from;
            from -= sz;
        }

        return 0;
    }

    public static void main(String[] args){
        new Main().solve();
    }
}