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

class SegmentTree
  def initialize(arr, l = 0, r = arr.size - 1)
    @l = l
    @r = r
    if @l == @r
      @leftmost_close = (arr[l] == ?) ? l : Float::INFINITY)
    else
      c = (l + r) / 2
      @left_child = self.class.new(arr, l, c)
      @right_child = self.class.new(arr, c + 1, r)
      @leftmost_close = [@left_child.leftmost_close, @right_child.leftmost_close].min
    end
  end

  attr_reader :leftmost_close, :l, :r

  def flip(i)
    if @l == @r
      @leftmost_close = (@leftmost_close == Float::INFINITY ? l : Float::INFINITY)
    else
      (i <= @left_child.r ? @left_child : @right_child).flip(i)
      @leftmost_close = [@left_child.leftmost_close, @right_child.leftmost_close].min
    end
  end
end

class LazySegmentTree
  def initialize(arr, l = 0, r = arr.size - 1)
    @l, @r = l, r
    @lazy = nil
    if @l == @r
      @min = arr[l]
    else
      c = (l + r) / 2
      @left_child = self.class.new(arr, l, c)
      @right_child = self.class.new(arr, c + 1, r)
      @min = [@left_child.min, @right_child.min].min
    end
  end

  attr_reader :min, :l, :r, :left_child, :right_child, :lazy

  def add(l, x)
    if @l == @r
      @min += x
    elsif l == @l
      if @lazy
        @lazy += x
      else
        @lazy = x
      end
    else
      force
      if l <= @left_child.r
        @left_child.add(l, x)
        @right_child.add(@right_child.l, x)
      else
        @right_child.add(l, x)
      end
      @left_child.force
      @right_child.force
      @min = [@left_child.min, @right_child.min].min
    end
  end

  def force
    return if !@lazy
    x = @lazy
    @lazy = nil
    @min += x
    @left_child.add(@left_child.l, x)
    @right_child.add(@right_child.l, x)
  end

  def rightmost_zero_pos(r)
    force
    return -1 if @min > 0
    return @l if @l == @r
    return @left_child.rightmost_zero_pos(r) if r <= @left_child.r
    ret = @right_child.rightmost_zero_pos(r)
    return @left_child.rightmost_zero_pos(@left_child.r) if ret == -1
    ret
  end

  def rightmost_one_pos(r)
    force
    return -1 if @min > 1
    return @min == 1 ? @l : -1 if @l == @r
    return @left_child.rightmost_one_pos(r) if r <= @left_child.r
    ret = @right_child.rightmost_one_pos(r)
    return @left_child.rightmost_one_pos(@left_child.r) if ret == -1
    ret
  end
end

n, q = gets.split.map(&:to_i)
a = gets.chomp.split("")
b = [0]
a.each do |c|
  if c == ?(
    b << b[-1] + 1
  else
    b << b[-1] - 1
  end
end
b.shift

seg1 = SegmentTree.new(a)
seg2 = LazySegmentTree.new(b)
q.times do
  i = gets.to_i - 1
  if a[i] == ?(
    a[i] = ?)
    seg1.flip(i)
    seg2.add(i, -2)
    j = seg1.leftmost_close
    puts j + 1
    seg1.flip(j)
    seg2.add(j, 2)
    a[j] = ?(
  else # a[i] == ?)
    a[i] = ?(
    seg1.flip(i)
    seg2.add(i, 2)
    zero = seg2.rightmost_zero_pos(i)
    j = nil
    if zero == -1
      j = seg2.rightmost_one_pos(i) + 1
    else
      one = seg2.rightmost_one_pos(i)
      if zero + 1 < one
        j = one + 1
      else
        j = zero + 2
      end
    end
    puts j + 1
    seg1.flip(j)
    seg2.add(j, -2)
    a[j] = ?)
  end
end