链接:

https://codeforces.com/contest/1263/problem/E

题意:

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.

思路:

先想了线段树,但是不知道怎么维护。

看了题解,发现维护前缀和,前缀和的最大最小值。

最大值判断嵌套深度,最小值没有负数说明满足条件。

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6+10; struct Node
{
int sum;
int maxv, minv;
int lazy;
}node[MAXN*4]; char s[MAXN], e[MAXN];
int n, ans[MAXN]; void Build(int root, int l, int r)
{
node[root].sum = node[root].maxv = node[root].minv = node[root].lazy = 0;
if (l == r)
return ;
int mid = (l+r)/2;
Build(root<<1, l, mid);
Build(root<<1|1, mid+1, r);
} void PushUp(int root)
{
node[root].sum = node[root<<1].sum+node[root<<1|1].sum;
node[root].minv = min(node[root<<1].minv, node[root<<1|1].minv);
node[root].maxv = max(node[root<<1].maxv, node[root<<1|1].maxv);
} void PushDown(int root, int l, int r)
{
if (node[root].lazy != 0)
{
int mid = (l+r)/2;
node[root<<1].lazy += node[root].lazy;
node[root<<1|1].lazy += node[root].lazy;
node[root<<1].sum += node[root].lazy*(mid-l+1);
node[root<<1|1].sum += node[root].lazy*(r-mid);
node[root<<1].minv += node[root].lazy;
node[root<<1|1].minv += node[root].lazy;
node[root<<1].maxv += node[root].lazy;
node[root<<1|1].maxv += node[root].lazy;
node[root].lazy = 0;
}
} void Update(int root, int l, int r, int ql, int qr, int v)
{
if (qr < l || r < ql)
return;
if (ql <= l && r <= qr)
{
node[root].sum += (r-l+1)*v;
node[root].maxv += v;
node[root].minv += v;
node[root].lazy += v;
return;
}
PushDown(root, l, r);
int mid = (l+r)/2;
Update(root<<1, l, mid, ql, qr, v);
Update(root<<1|1, mid+1, r, ql, qr, v);
PushUp(root);
} int QuerySum(int root, int l, int r, int p)
{
if (l == r)
return node[root].sum;
int mid = (l+r)/2;
PushDown(root, l, r);
if (p <= mid)
return QuerySum(root<<1, l, mid, p);
else
return QuerySum(root<<1|1, mid+1, r, p);
} int main()
{
scanf("%d", &n);
scanf("%s", s);
Build(1, 1, n);
int p = 1, len = strlen(s);
for (int i = 0;i < len;i++)
{
if (s[i] == 'L')
{
if (p>1)
p--;
}
else if (s[i] == 'R')
p++;
else
{
if (s[i] == '(' && e[p] == ')')
Update(1, 1, n, p, n, 2);
else if (s[i] == ')' && e[p] == '(')
Update(1, 1, n, p, n, -2);
else if (s[i] == '(' && e[p] != '(')
Update(1, 1, n, p, n, 1);
else if (s[i] == ')' && e[p] != ')')
Update(1, 1, n, p, n, -1);
else if (s[i] != '(' && s[i] != ')')
{
if (e[p] == '(')
Update(1, 1, n, p, n, -1);
else if (e[p] == ')')
Update(1, 1, n, p, n, 1);
}
e[p] = s[i];
}
int sum = QuerySum(1, 1, n, n);
// cout << sum << endl;
int minv = node[1].minv;
if (sum == 0 && minv >= 0)
ans[i] = node[1].maxv;
else
ans[i] = -1;
}
for (int i = 0;i < len;i++)
cout << ans[i] << ' ';
cout << endl; return 0;
}

Codeforces Round #603 (Div. 2) E. Editor(线段树)的更多相关文章

  1. Codeforces Round #603 (Div. 2) E. Editor 线段树

    E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...

  2. Codeforces Round #603 (Div. 2) E. Editor

    E. Editor 题目链接: https://codeforces.com/contest/1263/problem/E 题目大意: 输入一个字符串S1含有‘(’ , ‘)’ , ‘R’ , ‘L’ ...

  3. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  4. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  5. Codeforces Round #765 Div.1 F. Souvenirs 线段树

    题目链接:http://codeforces.com/contest/765/problem/F 题意概述: 给出一个序列,若干组询问,问给出下标区间中两数作差的最小绝对值. 分析: 这个题揭示着数据 ...

  6. 【转】Codeforces Round #406 (Div. 1) B. Legacy 线段树建图&&最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

  7. Codeforces Round #406 (Div. 2) D. Legacy 线段树建模+最短路

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)

    Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...

  9. Codeforces Round #603 (Div. 2) E - Editor(线段树,括号序列)

随机推荐

  1. [转帖]疑似兆芯开先KX-7000跑分曝光:IPC性能大幅提升

    疑似兆芯开先KX-7000跑分曝光:IPC性能大幅提升 https://www.bilibili.com/read/cv4028300 数码 11-23 1589阅读28点赞22评论 尽管有ARM架构 ...

  2. C语言结构体的“继承”

    这里说的继承有点像C++里的父类和子类,实际上是结构体类型的强制转换,最近看Linux内核源码时经常接触到这种方法,在这里就当作是一个简单的学习吧. 下面给出一个Demo,很简单,分别定义了一个fat ...

  3. Golang 调用 C/C++,例子式教程

    大部分人学习或者使用某样东西,喜欢在直观上看到动手后的结果,才会有继续下去的兴趣. 前言: Golang 调用 C/C++ 的教程网上很多,就我目前所看到的,个人见解就是比较乱,坑也很多.希望本文能在 ...

  4. python利用kruskal求解最短路径的问题

    python利用kruskal算法求解最短路径的问题,修改参数后可以直接使用 def kruskal(): """ kruskal 算法 ""&quo ...

  5. windows上 nginx 配置代理服务,配置多域名,以及最简单实现跨域配置

    Nginx,不用多说啦,大家都熟悉的不能再熟悉了,它是一款轻量级的高性能Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,最近在本地研究将nginx和resin配合使用,使服务 ...

  6. 2019 百合佳缘java面试笔试题 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.百合佳缘等公司offer,岗位是Java后端开发,因为发展原因最终选择去了百合佳缘,入职一年时间了,也成为了面 ...

  7. Mybatis事物浅谈

    本篇文章主要对Mybatis事物进行基础的介绍.先回顾JDBC事物,再了解Mybatis里面的事物应用. 1.JDBC的事务管理回顾 JDBC的事务管理是基于Connection对象实现的: 开启事务 ...

  8. Django--母版

    目录 母版 语法 案例 在之前的两个小程序中,可以发现在写html页面的时候有很多重复的代码 而在python中,为了避免写重复代码,我们通过函数.模块或者类来进行实现,所以在Django里面也有这样 ...

  9. English--五大基本句型基本概念

    English|五大基本句型基本概念 英语的基本句型是整个英语的框架体系,所以,祝愿看到此文的伙伴们,都可以牢牢掌握! 前言 目前所有的文章思想格式都是:知识+情感. 知识:对于所有的知识点的描述.力 ...

  10. 关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)

    关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能:可能和其他插件有冲突)