Problem: Example

Input

6 3
((()))
4
3
1


Output

2
2
1

Solution (in CPP):

Solution:
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const double EPS = 1e-8;
const int MOD = 1000000007;
const int INF = 1<<28;

class SegTree{
	typedef int SegT;
	static const SegT defvalue = INF;
	private:
		vector<SegT> val, lazy;
		vector<int> lazyf;
		int n;
		
		SegT combine(SegT a, SegT b){
			return min(a, b);
		}
		SegT merge(SegT a, SegT b){
			return a+b;
		}
		
		void applyLazy(SegT k){
			if(lazyf[k]){
				lazyf[k] = 0;
				val[k] = merge(val[k], lazy[k]);
				if(k*2+2 < lazy.size()){
					lazy[k*2+1] = lazyf[k*2+1] ? merge(lazy[k*2+1], lazy[k]) : lazy[k];
					lazy[k*2+2] = lazyf[k*2+2] ? merge(lazy[k*2+2], lazy[k]) : lazy[k];
					lazyf[k*2+1] = lazyf[k*2+2] = 1;
				}
			}
		}
	public:
		SegTree(int size){
			n=1;
			while(n<size)n<<=1;
			val=vector<SegT>(2*n, defvalue);
			lazy=vector<SegT>(2*n, 0);
			lazyf=vector<int>(2*n, 0);
		}
		SegTree(const vector<SegT> &in){
			n=1;
			while(n<in.size())n<<=1;
			val=vector<SegT>(2*n, defvalue);
			lazy=vector<SegT>(2*n, -1);
			lazyf=vector<int>(2*n, 0);
			for(int i=n-1 + in.size()-1;i>=0;i--){
				if(n-1 <= i) val[i] = in[i - (n-1)];
				else val[i] = combine(val[i*2+1],val[i*2+2]);
			}
		}
		//[a, b) の値をaに更新する
		void update(int a,int b,SegT x,int k=0,int l=0,int r=-1){
			if(r==-1)r=n;
			applyLazy(k);
			if(r<=a||b<=l) return;
			if(a<=l&&r<=b){
				lazy[k] = x;
				lazyf[k] = 1;
				applyLazy(k);
			}else{
				update(a,b,x,k*2+1,l,(l+r)/2);
				update(a,b,x,k*2+2,(l+r)/2,r);
				val[k] = combine(val[k*2+1], val[k*2+2]);
			}
		}
		//[a, b) の値を取ってくる
		SegT query(int a,int b,int k=0,int l=0,int r=-1){
			if(r==-1)r=n;
			if(r<=a||b<=l) return defvalue;
			applyLazy(k);
			if(a<=l&&r<=b){
				return val[k];
			}else{
				return combine(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
			}
		}
		void print(){
			cout << val << endl;
			cout << lazy << endl;
			cout << lazyf << endl;
		}
};


int n, m;
string s;
int main(){
	ios::sync_with_stdio(false);
	set<int> close;
	cin >> n >> m >> s;
	vi d(s.size());
	REP(i, s.size()){
		d[i] = (i?d[i-1]:0) + (s[i]=='(' ? 1 : -1);
		if(s[i]==')') close.insert(i);
	}
	
	SegTree seg(d);
	
	REP(q, m){
		int t;
		cin >> t;t--;
		if(s[t] == '('){
			seg.update(t, n, -2);
			close.insert(t);
			s[t] = ')';
			int t = *close.begin();
			seg.update(t, n, 2);
			close.erase(t);
			s[t] = '(';
			cout << t+1 << endl;
		}else if(s[t] == ')'){
			seg.update(t, n, 2);
			close.erase(t);
			s[t] = '(';
			
			int l=0, r=t;
			while(l<r-1){
				int med=(l+r)/2;
				if(seg.query(med, t) <= 1) l = med;
				else r = med;
			}
			int t = r;
			seg.update(t, n, -2);
			close.insert(t);
			s[t] = ')';
			cout << t+1 << endl;
		}
	}
	return 0;
}