E. Editor

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text.

Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left.

Initially, the cursor is in the first (leftmost) character.

Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position.

Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence.

Formally, correct text (CT) must satisfy the following rules:

any line without brackets is CT (the line can contain whitespaces);
If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT;
two consecutively written CT is also CT.
Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me).

The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command.

The correspondence of commands and characters is as follows:

L — move the cursor one character to the left (remains in place if it already points to the first character);
R — move the cursor one character to the right;
any lowercase Latin letter or bracket (( or )) — write the entered character to the position where the cursor is now.
For a complete understanding, take a look at the first example and its illustrations in the note below.

You are given a string containing the characters that the user entered. For the brackets coloring module’s work, after each command you need to:

check if the current text in the editor is a correct text;
if it is, print the least number of colors that required, to color all brackets.
If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is 2, and for the bracket sequence (()(()())())(()) — is 3.

Write a program that prints the minimal number of colors after processing each command.

Input
The first line contains an integer n (1≤n≤106) — the number of commands.

The second line contains s — a sequence of commands. The string s consists of n characters. It is guaranteed that all characters in a string are valid commands.

Output
In a single line print n integers, where the i-th number is:

−1 if the line received after processing the first i commands is not valid text,
the minimal number of colors in the case of the correct text.
Examples
inputCopy
11
(RaRbR)L)L(
outputCopy
-1 -1 -1 -1 -1 -1 1 1 -1 -1 2
inputCopy
11
®R®Ra)c
outputCopy
-1 -1 1 1 -1 -1 1 1 1 -1 1


手动模拟括号序列。

怎么判断括号序列的合法性呢?我们把左括号看成1,右括号看成-1.

我们可以得到一些性质:

  1. 区间sum为0 (左右括号数量一定要相等)
  2. 从第一个位置,到最后一个位置,前缀和一定要大于等于0,所以我们维护前缀和之后,前缀和大于等于0

然后我们就可以维护左右括号的个数,以及前缀和了。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
//#define int long long
using namespace std;
const int N=1e6+10;
int n,pos=1,cnt;
char str[N],a[N];
struct node{int l,r,mx,mi,lazy;}t[N<<2];
inline void push_up(int p){
	t[p].mi=min(t[p<<1].mi,t[p<<1|1].mi);
	t[p].mx=max(t[p<<1].mx,t[p<<1|1].mx);
}
inline void push_down(int p){
	if(t[p].lazy){
		t[p<<1].mx+=t[p].lazy,t[p<<1].mi+=t[p].lazy;
		t[p<<1|1].mx+=t[p].lazy,t[p<<1|1].mi+=t[p].lazy;
		t[p<<1].lazy+=t[p].lazy,t[p<<1|1].lazy+=t[p].lazy;
		t[p].lazy=0;
	}
}
void build(int p,int l,int r){
	t[p].l=l,t[p].r=r;
	if(l==r)	return ; int mid=l+r>>1;
	build(p<<1,l,mid); build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,int v){
	if(t[p].l==l&&t[p].r==r){t[p].mx+=v,t[p].mi+=v,t[p].lazy+=v; return ;}
	push_down(p);	int mid=t[p].l+t[p].r>>1;
	if(r<=mid)	change(p<<1,l,r,v);
	else if(l>mid)	change(p<<1|1,l,r,v);
	else	change(p<<1,l,mid,v),change(p<<1|1,mid+1,r,v);
	push_up(p);
}
signed main(){
	cin>>n;	scanf("%s",str+1); build(1,1,n);
	for(int i=1;i<=n;i++){
		if(str[i]=='R')	pos++;
		else if(str[i]=='L')	pos=max(pos-1,1);
		else if(str[i]=='('){
			change(1,pos,n,1+(a[pos]==')')-(a[pos]=='('));
			if(a[pos]!='(')	cnt++; if(a[pos]==')')	cnt++; a[pos]=str[i];
		}
		else if(str[i]==')'){
			change(1,pos,n,-1-(a[pos]=='(')+(a[pos]==')'));
			if(a[pos]!=')')	cnt--; if(a[pos]=='(') cnt--; a[pos]=str[i];
		}
		else{
			if(a[pos]=='(')	change(1,pos,n,-1),cnt--;
			if(a[pos]==')')	change(1,pos,n,1),cnt++; a[pos]=str[i];
		}
		if(t[1].mi!=0||cnt!=0)	printf("-1 ");
		else	printf("%d ",t[1].mx);
	}
	return 0;
}