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++11 (judge verdict: Time Limit Exceeded)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#define DEBUG(x) cerr << "L" << __LINE__ << " " << #x << " = " << x << endl

const int INF = 1e9;
const int sqrtN = 300;

struct SegmentArray1 {
  int B;
  vector<int> data;
  vector<int> add;
  vector<int> min;
  SegmentArray1(int n) {
    B = (n + sqrtN - 1) / sqrtN;
    data.assign(B * sqrtN, 0);
    add.assign(B, 0);
    min.assign(B, 0);
  }
  void update(int a, int b, int v) {
    for(int k = 0; k < B; ++k) {
      int l = k * sqrtN, r = (k + 1) * sqrtN;
      if(r <= a || b <= l) continue;
      if(a <= l && r <= b) {
        add[k] += v;
        continue;
      }
      min[k] = INF;
      int st = std::max(a, l), en = std::min(b, r);
      for(int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) {
        if(st <= i && i < en) data[i] += v;
        if(min[k] > data[i]) min[k] = data[i];
      }
    }
  }
  int search(int p) {
    int a = 0;
    int b = p + 1;
    for(int k = B - 1; k >= 0; k--) {
      int l = k * sqrtN, r = (k + 1) * sqrtN;
      if(r <= a || b <= l) continue;
      if(a <= l && r <= b) {
        if(add[k] + min[k] < 2) {
          for(int j = sqrtN - 1; j >= 0; --j) {
            if(add[k] + data[k * sqrtN + j] < 2) {
              return k * sqrtN + j + 1;
            }
          }
        }
      }
      int st = std::max(a, l), en = std::min(b, r);
      for(int i = en - 1; i >= st; --i) {
        if(add[k] + data[i] < 2) {
          return i + 1;
        }
      }
    }
  }
};

struct SegmentArray2 {
  int B;
  vector<bool> data;
  vector<bool> stat;
  SegmentArray2(int n) {
    B = (n + sqrtN - 1) / sqrtN;
    data.assign(B * sqrtN, false);
    stat.assign(B, false);
  }
  void update(int p, bool v) {
    data[p] = v;
    int k = p / sqrtN;
    stat[k] = false;
    for(int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) {
      stat[k] = stat[k] || data[i];
    }
  }
  int search() {
    for(int i = 0; i < B; ++i) {
      if(stat[i]) {
        for(int j = 0; j < sqrtN; ++j) {
          int k = i * sqrtN + j;
          if(data[k]) return k;
        }
      }
    }
  }
  bool get(int p) {
    return data[p];
  }
};

char S[300001];

int main() {
  int N, Q; scanf("%d%d", &N, &Q);
  scanf("%s", S);
  SegmentArray1 seg1(N);
  SegmentArray2 seg2(N);
  for(int i = 0; i < N; ++i) { 
    seg1.update(i, N, (S[i] == '(' ? 1 : -1));
    if(S[i] == ')') seg2.update(i, true);
  }
  for(int q = 0; q < Q; ++q) {
    int p; scanf("%d", &p);
    --p;
    if(seg2.get(p) == false) {
      // S[p] = ')';
      seg2.update(p, true);
      seg1.update(p, N, -2);
      int np = seg2.search();
      // S[np] = '(';
      seg2.update(np, false);
      seg1.update(np, N, +2);
      printf("%d\n", np + 1);
    }
    else {
      // S[p] = '(';
      seg2.update(p, false);
      seg1.update(p, N, +2);
      int np = seg1.search(p);
      // S[np] = ')';
      seg2.update(np, true);
      seg1.update(np, N, -2);
      printf("%d\n", np + 1);
    }
  }
}