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: Accepted)

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
using namespace std;

const int kMax = 1048600;
const int kInf = 1000000000;

string S;
int N, Q;
int Qk[150010];

set< int > L, R;
int Tm[kMax], Td[kMax];

void read();
void solve();

void update(int, int, int, int, int, int);
int query(int, int, int, int, int);

int main() {
  read();
  solve();
  return 0;
}

void read() {
  ios::sync_with_stdio(false);

  cin >> N >> Q >> S, S = "#" + S;
  for (int i = 0; i < Q; ++i)
    cin >> Qk[i];
}

void solve() {
  fill(&Tm[0], &Tm[kMax], 0);

  for (int i = 1, s = 0; i <= N; ++i) {
    if (S[i] == '(')
      ++s, L.insert(i);
    else
      --s, R.insert(i);
    update(1, 0, N, i - 1, i, s);
  }

  for (int qi = 0; qi < Q; ++qi) {
    int p = Qk[qi], t;

    if (S[p] == '(') {
      // flip query
      S[p] = ')';
      update(1, 0, N, p - 1, N, -2);
      L.erase(p), R.insert(p);

      // find answer
      t = *R.begin();
      cout << t << "\n";

      // flip answer
      S[t] = '(';
      update(1, 0, N, t - 1, N, 2);
      R.erase(t), L.insert(t);
    }
    else {
      // flip query
      S[p] = '(';
      update(1, 0, N, p - 1, N, 2);
      R.erase(p), L.insert(p);

      // find answer
      int low = 1, up = N, mid;
      while (low < up) {
        mid = (low + up) / 2;
        if (query(1, 0, N, mid - 1, N) >= 2)
          up = mid;
        else
          low = mid + 1;
      }
      t = *L.lower_bound(up);
      cout << t << "\n";

      // flip answer
      S[t] = ')';
      update(1, 0, N, t - 1, N, -2);
      L.erase(t), R.insert(t);
    }
  }
}

void update(int u, int l, int r, int ql, int qr, int qd) {
  if (ql <= l && r <= qr)
    Tm[u] += qd, Td[u] += qd;
  else {
    int mid = (l + r) / 2;

    if (Td[u] != 0) {
      update(2 * u,     l, mid, l, mid, Td[u]);
      update(2 * u + 1, mid, r, mid, r, Td[u]);
      Td[u] = 0;
    }

    if (ql < mid) update(2 * u,     l, mid, ql, qr < mid ? qr : mid, qd);
    if (qr > mid) update(2 * u + 1, mid, r, ql > mid ? ql : mid, qr, qd);

    Tm[u] = min(Tm[2 * u], Tm[2 * u + 1]);
  }
}

int query(int u, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return Tm[u];
  else {
    int mid = (l + r) / 2;

    if (Td[u] != 0) {
      update(2 * u,     l, mid, l, mid, Td[u]);
      update(2 * u + 1, mid, r, mid, r, Td[u]);
      Td[u] = 0;
    }

    int ret = kInf;

    if (ql < mid) ret = min(ret, query(2 * u,     l, mid, ql, qr < mid ? qr : mid));
    if (qr > mid) ret = min(ret, query(2 * u + 1, mid, r, ql > mid ? ql : mid, qr));

    return ret;
  }
}