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

链接:

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;
}

原文地址:https://www.cnblogs.com/YDDDD/p/12053874.html

时间: 2024-08-03 10:56:48

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

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 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

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

Codeforces Round #603 (Div. 2)

传送门 感觉脑子还是转得太慢了QAQ,一些问题老是想得很慢... A. Sweet Problem 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/29 22:36:19 */ #include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <set> #include <ma

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Let D(x) be the number of positive divisors of a positive integer x. For example, D(2)?=?2 (2 is divisible by 1 and 2), D(6)?

HDU5638 / BestCoder Round #74 (div.1) 1003 Toposort 线段树+拓扑排序

Toposort 问题描述 给出nn个点mm条边的有向无环图. 要求删掉恰好kk条边使得字典序最小的拓扑序列尽可能小. 输入描述 输入包含多组数据. 第一行有一个整数TT, 表示测试数据组数. 对于每组数据: 第一行包含3个整数nn, mm和kk (1 \le n \le 100000, 0 \le k \le m \le 200000)(1≤n≤100000,0≤k≤m≤200000), 表示图中结点数目, 图中边的数目以及要删的边数. 接下来mm行, 每行包含两个整数u_iu?i?? and

Codeforces Round #603 (Div. 2)E

http://codeforces.com/contest/1263/problem/E 题意:求合法的括号序列 #include<bits/stdc++.h> using namespace std; typedef long long ll; #define lson root<<1,l,midd #define rson root<<1|1,midd+1,r #define pb push_back const int inf=0x3f3f3f3f; const

Codeforces Round #603 (Div. 2) A. Sweet Problem(数学)

链接: https://codeforces.com/contest/1263/problem/A 题意: You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are r candies in it, the second pile contains only green candies and there are g ca

Codeforces Round #603 (Div. 2) B. PIN Codes

链接: https://codeforces.com/contest/1263/problem/B 题意: A PIN code is a string that consists of exactly 4 digits. Examples of possible PIN codes: 7013, 0000 and 0990. Please note that the PIN code can begin with any digit, even with 0. Polycarp has n (