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++14 (judge verdict: Accepted)

#include <bits/stdc++.h>

#define fto(i, l, r) for(int i = l; i <= r; ++i)
#define fdto(i, r, l) for(int i = r; i >= l; --i)
#define debug(a) cout << #a << ": " << a << endl

using namespace std;
const int oo = 1000000007;
const int MAXN = 300005; 

int n, q;
char s[MAXN];
set<int> pos;

struct Node {
  int m, val;
};

Node t[4*MAXN];

void propagate(const int &id, const int &l, const int &r) {
  int &x = t[id].val;
  if (x != 0) {
    fto(i, 0, 1) {
      t[2*id + i].m += x;
      t[2*id + i].val += x;
    }
    x = 0;
  }
}

void update(int id, int l, int r, const int &i, const int &j, const int &x) {
  if (j < l || i > r) return;
  if (i <= l && r <= j) {
    t[id].val += x;
    t[id].m += x;
    return;
  }
  // printf("%d %d %d %d\n", id, l, r, t[id].m);
  propagate(id, l, r);
  int m = (l + r)/2;
  update(2*id, l, m, i, j, x); update(2*id+1, m+1, r, i, j, x);
  t[id].m = min(t[2*id].m, t[2*id+1].m);
}

void check(int id, int l, int r) {
  // printf("%d %d %d %d %d\n", id, l, r, t[id].m, t[id].val);
  if (l < r) {
    int m = (l+r)/2;
    check(2*id, l, m);
    check(2*id+1, m+1, r);
  }  
}

int query(int id, int l, int r, const int x) {
  // printf("%d %d %d %d %d\n", id, l, r, x, t[id].m);
  if (t[id].m >= x)
    return l;
  if (l == r)
    return l+1;
  propagate(id, l, r);
  int m = (l+r)/2;
  int ans = query(2*id+1, m+1, r, x);
  if (ans == m+1)
    ans = query(2*id, l, m, x);
  return ans;
}

void flip(int i) {
  if (s[i] == '(') {
    s[i] = ')';
    update(1, 0, n-1, i, n-1, -2);
    pos.insert(i);
  } else {
    s[i] = '(';
    update(1, 0, n-1, i, n-1, 2);
    pos.erase(i);
  }
}

int main() {
  scanf("%d%d", &n, &q);
  scanf("%s", s);

  fto(i, 0, n-1) {
    // debug(i);
    if (s[i] == ')') {
      pos.insert(i);
      update(1, 0, n-1, i, n-1, -1);
    } else 
      update(1, 0, n-1, i, n-1, 1);
  }

  // check(1, 0, n-1);

  fto(i, 1, q) {
    int p; scanf("%d", &p);
    --p;

    // debug(i);
    int ans;
    if (s[p] == '(') {
      flip(p);
      ans = *pos.begin();
    } else {
      flip(p);
      // if (i == 2)
      //   check(1, 0, n-1);
      ans = query(1, 0, n-1, 2);
    }
    
    flip(ans);
    // printf("%s\n", s);
    // for(int x: pos)
    //   printf("%d ", x);
    // puts("");
    printf("%d\n", ans+1);
  }
  
  return 0;
}