Problem: Example

Input

6 3
((()))
4
3
1


Output

2
2
1

Solution (in CPP):

Solution:
#include <cstdio>
#include <set>
#include <vector>
#include <climits>
using namespace std;

// Abstract Lazy Segment Tree (????±?????±?????????¶?????°???????????¨)
// Verified: AOJ ????????°??¨ verify ?????¨??¨

const int SIZE = 1 << 19;
template<typename T>
struct lazysegtree {
    // ?????????????????????
    T node[2*SIZE], lazy[2*SIZE], I;
    bool need_upd[2*SIZE];

    // ????????¬????????§??? (update, query ??? 2 ???????????????)
    // update function ??????????????????????????¢????????????
    // upd_f(X, Y, l, r) -> ????????? [l, r) ??§???????????????????????? X ??? Y ????????????
    // lazy ???????????? update ????????¨??????????????? 1 ??????????????¨?????°?????????
    T (*upd_f)(T, T, int, int), (*qry_f)(T, T);

    // ???????????¨?????????????????????????????¨?????? node ??¨ lazy ??????????????§?????????
    lazysegtree(T (*op1)(T, T, int, int), T (*op2)(T, T), T X) {
        upd_f = op1;
        qry_f = op2;
        I = X;
        fill(lazy, lazy+2*SIZE, I);
        fill(node, node+2*SIZE, I);
        fill(need_upd, need_upd+2*SIZE, false);
    }

    // ?????? vec ????????§?????????
    void init(vector<T> vec) {
        int N = (int)vec.size();
        for(int i=0; i<N; i++) {
            node[SIZE-1+i] = vec[i];
        }
        for(int i=SIZE-2; i>=0; i--) {
            node[i] = qry_f(node[2*i+1], node[2*i+2]);
        }
    }

    void lazy_eval(int k, int l, int r) {
        if(!need_upd[k]) return;
        node[k] = upd_f(node[k], lazy[k], l, r);
        if(r - l > 1) {
            lazy[2*k+1] = upd_f(lazy[2*k+1], lazy[k], 0, 1);
            lazy[2*k+2] = upd_f(lazy[2*k+2], lazy[k], 0, 1);
            need_upd[2*k+1] = need_upd[2*k+2] = true;
        }
        lazy[k] = I;
        need_upd[k] = false;
    }

    // ???????????? [a, b) ??????????????? val ??????????????????
    // (upd_f ?????¨????????????)
    void update(int a, int b, T val, int l=0, int r=SIZE, int k=0) {
        lazy_eval(k, l, r);
        if(b <= l || r <= a) return;
        if(a <= l && r <= b) {
            lazy[k] = upd_f(lazy[k], val, 0, 1);
            need_upd[k] = true;
            lazy_eval(k, l, r);
        }
        else {
            int mid = (l + r) / 2;
            update(a, b, val, l, mid, 2*k+1);
            update(a, b, val, mid, r, 2*k+2);
            node[k] = qry_f(node[2*k+1], node[2*k+2]);
        }
    }

    // ???????????? [a, b) ?????????????????¨???????????????
    // (qry_f ?????¨????????????)
    T query(int a, int b, int l=0, int r=SIZE, int k=0) {
        lazy_eval(k, l, r);
        if(b <= l || r <= a) return I;
        if(a <= l && r <= b) return node[k];
        int mid = (l + r) / 2;
        T vl = query(a, b, l, mid, 2*k+1);
        T vr = query(a, b, mid, r, 2*k+2);
        return qry_f(vl, vr);
    }
};

int add(int a, int b, int l, int r) {
    if(a == INT_MAX) return b;
    return a + b;
}

int qry(int a, int b) {
    return min(a, b);
}

lazysegtree<int> seg(add, qry, INT_MAX);
int main() {
    int N, Q; scanf("%d%d", &N, &Q);

    vector<int> array(N), info(N);
    set<int> open, close;
    for(int i=0; i<N; i++) {
        char c; scanf(" %c", &c);
        if(c == '(') {
            info[i]++;
            open.insert(i);
            array[i]++;
        }
        if(c == ')') {
            close.insert(i);
            array[i]--;
        }
        if(i != 0) array[i] += array[i-1];
    }

    seg.init(array);
    while(Q--) {
        int q, idx; scanf("%d", &q); q--;
        if(info[q]) {
            // '(' -> ')'
            seg.update(q, N, -2);
            info[q] ^= 1;
            open.erase(q);
            close.insert(q);

            // ?????? idx ????????? ')' ??? '(' ???
            idx = *(close.begin());
            seg.update(idx, N, 2);
            info[idx] ^= 1;
            open.insert(idx);
            close.erase(idx);
        }
        else {
            // ')' -> '('
            seg.update(q, N, 2);
            info[q] ^= 1;
            close.erase(q);
            open.insert(q);

            int lb = -1, ub = N;
            while(ub - lb > 1) {
                int mid = (ub + lb) / 2;
                int val = seg.query(mid, N);
                // printf("mid = %d, val = %d\n", mid, val);
                if(val > 1) ub = mid;
                else lb = mid;
            }

            idx = *(open.lower_bound(ub));
            seg.update(idx, N, -2);
            info[idx] ^= 1;
            close.insert(idx);
            open.erase(idx);
        }
        printf("%d\n", idx + 1);
    }
    return 0;
}