# 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 𝑛 (1≤𝑛≤106) — the number of commands.

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

Output
In a single line print 𝑛 integers, where the 𝑖-th number is:

−1 if the line received after processing the first 𝑖 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)R(R)Ra)c
outputCopy
-1 -1 1 1 -1 -1 1 1 1 -1 1
Note
In the first example, the text in the editor will take the following form:

(
^
(
^
(a
^
(a
^
(ab
^
(ab
^
(ab)
^
(ab)
^
(a))
^
(a))
^
(())
^
[思路]：我们假设'('为1，')'为-1，使用线段树维护前缀和，线段树上每个叶子结点代表一个前缀和

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
struct Segment_Tree{
struct Node{
int l, r, sum, lazy, maxs, mins;
}tree[MAXN << 2];
inline void push_up(int root){
tree[root].sum = tree[root << 1].sum + tree[root << 1 | 1].sum;
tree[root].mins = min(tree[root << 1].mins, tree[root << 1 | 1].mins);
tree[root].maxs = max(tree[root << 1].maxs, tree[root << 1 | 1].maxs);
return ;
}
inline void push_down(int root){
if(tree[root].lazy){
tree[root << 1].lazy += tree[root].lazy;
tree[root << 1 | 1].lazy += tree[root].lazy;
tree[root << 1].maxs += tree[root].lazy;
tree[root << 1 | 1].maxs += tree[root].lazy;
tree[root << 1].mins += tree[root].lazy;
tree[root << 1 | 1].mins += tree[root].lazy;
tree[root << 1].sum += (tree[root].lazy * (tree[root << 1].r - tree[root << 1].l + 1));
tree[root << 1 | 1].sum += (tree[root].lazy * (tree[root << 1 | 1].r - tree[root << 1 | 1].l + 1));
tree[root].lazy = 0;
}
}
void build(int root, int l, int r){
tree[root].l = l, tree[root].r = r;
tree[root].sum = tree[root].lazy = tree[root].maxs = tree[root].mins = 0;
if(l == r){
return ;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
push_up(root);
}
void update(int root, int l, int r, int val){
if(tree[root].l >= l && tree[root].r <= r){
tree[root].sum += (val * (tree[root].r - tree[root].l + 1));
tree[root].lazy += val;
tree[root].maxs += val;
tree[root].mins += val;
return ;
}
push_down(root);
int mid = (tree[root].l + tree[root].r) >> 1;
if(r <= mid){
update(root << 1, l, r, val);
}
else if(l > mid){
update(root << 1 | 1, l, r, val);
}
else{
update(root << 1, l, mid, val);
update(root << 1 | 1, mid + 1, r, val);
}
push_up(root);
}
int query_sum(int root, int l, int r){
if(l <= tree[root].l && tree[root].r <= r){
return tree[root].sum;
}
push_down(root);
int mid = (tree[root].l + tree[root].r) >> 1;
if(r <= mid){
return query_sum(root << 1, l, r);
}
else if(l > mid){
return query_sum(root << 1 | 1, l, r);
}
else{
return query_sum(root << 1, l, mid) + query_sum(root << 1 | 1, mid + 1, r);
}
}
int query_maxs(int root, int l, int r){
if(l <= tree[root].l && tree[root].r <= r){
return tree[root].maxs;
}
push_down(root);
int mid = (tree[root].l + tree[root].r) >> 1;
if(r <= mid){
return query_maxs(root << 1, l, r);
}
else if(l > mid){
return query_maxs(root << 1 | 1, l, r);
}
else{
return max(query_maxs(root << 1, l, mid), query_maxs(root << 1 | 1, mid + 1, r));
}
}
int query_mins(int root, int l, int r){
if(l <= tree[root].l && tree[root].r <= r){
return tree[root].mins;
}
int mid = (tree[root].l + tree[root].r) >> 1;
push_down(root);
if(r <= mid){
return query_mins(root << 1, l, r);
}
else if(l > mid){
return query_mins(root << 1 | 1, l, r);
}
else{
return min(query_mins(root << 1, l, mid), query_mins(root << 1 | 1, mid + 1, r));
}
}
}seg;
char vids[MAXN];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int len;
cin >> len;
int n = len + 5;
string s;
cin >> s;
seg.build(1, 1, len + 5);
vector<int>vec;
vec.clear();
int num = 1;
memset(vids, 0, sizeof(vids));
for(int i = 0; i < len; i ++){
//cout << "test = " << seg.query_maxs(1, 1, 11) << " " << seg.query_maxs(1, 1, 3) << endl;
if(s[i] == 'L'){
if(num != 1) num --;
if(seg.query_mins(1, n, n) == 0 && seg.query_mins(1, 1, n) >= 0){
vec.push_back(seg.query_maxs(1, 1, n));
}
else{
vec.push_back(-1);
}
//cout << s[i] << " " << seg.query_maxs(1, 1, n) << " " << num << endl;
}
else if(s[i] == 'R'){
num ++;
if(seg.query_mins(1, n, n) == 0 && seg.query_mins(1, 1, n) >= 0){
vec.push_back(seg.query_maxs(1, 1, n));
}
else{
vec.push_back(-1);
}
//cout << s[i] << " " << seg.query_maxs(1, 1, n) << " " << num << endl;
}
else if(s[i] == '('){
if(vids[num] == '('){
seg.update(1, num, n, -1);
}
else if(vids[num] == ')'){
seg.update(1, num, n, 1);
}
seg.update(1, num, n, 1);
vids[num] = s[i];
if(seg.query_mins(1, n, n) == 0 && seg.query_mins(1, 1, n) >= 0){
vec.push_back(seg.query_maxs(1, 1, n));
}
else{
vec.push_back(-1);
}
//cout << s[i] << " " << seg.query_maxs(1, 1, n) << " " << num << endl;
}
else if(s[i] == ')'){
if(vids[num] == '('){
seg.update(1, num, n, -1);
}
else if(vids[num] == ')'){
seg.update(1, num, n, 1);
}
vids[num] = s[i];
seg.update(1, num, n, -1);
if(seg.query_mins(1, n, n) == 0 && seg.query_mins(1, 1, n) >= 0){
vec.push_back(seg.query_maxs(1, 1, n));
}
else{
vec.push_back(-1);
}
//cout << s[i] << " " << seg.query_maxs(1, 1, n) << " " << num << endl;
}
else{
if(vids[num] == '('){
seg.update(1, num, n, -1);
}
else if(vids[num] == ')'){
seg.update(1, num, n, 1);
}
vids[num] = s[i];
if(seg.query_mins(1, n, n) == 0 && seg.query_mins(1, 1, n) >= 0){
vec.push_back(seg.query_maxs(1, 1, n));
}
else{
vec.push_back(-1);
}
//cout << s[i] << " " << seg.query_maxs(1, 1, n) << " " << num << endl;
}
}
for(int i = 0; i < vec.size(); i ++){
cout << vec[i] << " ";
}
cout << endl;
return 0;
}

posted @ 2019-12-02 10:39  moxin0509  阅读(246)  评论(0编辑  收藏  举报