博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【CodeForces 1263E --- Editor】线段树,延迟更新
阅读量:2038 次
发布时间:2019-04-28

本文共 4285 字,大约阅读时间需要 14 分钟。

【CodeForces 1263E --- Editor】线段树,延迟更新

Description

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.

Sample Input

11

(RaRbR)L)L(

Sample Output

-1 -1 -1 -1 -1 -1 1 1 -1 -1 2

解题思路:

通过维护区间最小值和左右括号是否相等来判断该文本是否符合规定。通过区间最大值来得到匹配的括号层数。

AC代码:

#include 
using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1const int MAXN = 1e6+5;int add[MAXN<<2],sl[MAXN<<2],sr[MAXN<<2];void push_up(int root){
sl[root]=min(sl[lson],sl[rson]); sr[root]=max(sr[lson],sr[rson]);}void push_down(int root){
if(add[root]) {
sl[lson]+=add[root]; sl[rson]+=add[root]; sr[lson]+=add[root]; sr[rson]+=add[root]; add[lson]+=add[root]; add[rson]+=add[root]; add[root]=0; }}void update(int root,int l,int r,int L,int R,int x){
if(l>=L && r<=R) {
add[root]+=x; sl[root]+=x; sr[root]+=x; return; } push_down(root); int mid=(l+r)>>1; if(L<=mid) update(lson,l,mid,L,R,x); if(R>mid) update(rson,mid+1,r,L,R,x); push_up(root);}int main(){
SIS; int n,pos=1,cnt=0; cin >> n; string s,t(n+1,' '); cin >> s; for(int i=0;i

转载地址:http://siyof.baihongyu.com/

你可能感兴趣的文章
volatile与AtomicIntegerfieldupdater 区别与关系
查看>>
mysql timeStamp默认值0000-00-00 00:00:00 报错
查看>>
Spring-data-redis配置及使用示例
查看>>
一个很不错的maven下载服务器
查看>>
maven 报Unable to locate the Javac Compiler in: D:\Program Files\Java\jdk1.6.0_20\..\lib\tools.jar
查看>>
在Maven中新增自定的jar包
查看>>
各种异常产生原因及如何处理解决
查看>>
Maven类包冲突终极解决小技若干
查看>>
junit测试环境搭建(遇到的坑)
查看>>
高可用的工作心得分享
查看>>
Spring Data Redis Version 1.7.1.RELEASE
查看>>
Spring-data-redis:特性与实例(redis 存储对象)
查看>>
linux下mysql配置文件my.cnf最详细解释
查看>>
js中使用jstl中的值
查看>>
eclipse的静态资源文件夹缓存问题
查看>>
Project xxx already exists Add a version or custom suffix using "Name template" in "Advanced" sett
查看>>
Linux 下 查看以及修改文件权限
查看>>
文件类型CRLF line terminators导致sh文件不能执行
查看>>
我的一些简单的shell脚本实例
查看>>
shell一个实例$(($a+1))
查看>>