题目链接:https://codeforces.com/contest/1263/problem/E

题意:一个编译器,每次输入一些字符,R表示光标右移,L表示光标左移,然后有一些左括号(  和 右括号 ),每次会询问当前输入的数据的括号是否合法,如果不合法输出-1,如果合法输出最大合法的括号对数,合法的括号就是()这种形式的。

思路:大致思路是用线段树维护区间一个区间前缀和,初始化前缀和为0。遇到单点更新,(让管辖区间+1,)就让管辖区间-1,,判断是否是合法括号需要判断区间最小值是否为0,且保证1到n区间的前缀和为0(画图思考一下),满足两种条件的同时才能说明此为合法括号序列,最终查询区间最大值就是最大匹配的括号对数(画图思考)。

代码:

 #include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int maxn = 1e6+;
struct node{
int l,r;
int Min,Max;
int val;
int lazy;
}tree[maxn*+];
void pushdown(int index){
if(tree[index].lazy){
tree[index<<].val += (tree[index<<].r-tree[index<<].l+)*tree[index].lazy;
tree[index<<|].val +=(tree[index<<|].r-tree[index<<|].l+)*tree[index].lazy;
tree[index<<].Max += tree[index].lazy;
tree[index<<|].Max += tree[index].lazy;
tree[index<<].Min += tree[index].lazy;
tree[index<<|].Min += tree[index].lazy;
tree[index<<].lazy += tree[index].lazy;
tree[index<<|].lazy += tree[index].lazy;
tree[index].lazy = ;
}
}
void pushup(int index){
tree[index].val = tree[index<<].val+tree[index<<|].val;
tree[index].Max = max(tree[index<<].Max,tree[index<<|].Max);
tree[index].Min = min(tree[index<<].Min,tree[index<<|].Min);
} void build(int l,int r,int index){//建树
tree[index].l = l,tree[index].r = r,tree[index].lazy = ;
if(l == r){
//scanf("%d",&tree[index].val);
tree[index].val = ;
tree[index].Max = tree[index].Min = tree[index].val;
return;
}
int mid = (l+r)>>;//
build(l,mid,index<<);
build(mid+,r,index<<|);
pushup(index);
}
void update(int l,int r,int index,int val){//区间修改
if(l <= tree[index].l && r >= tree[index].r){
tree[index].val += (tree[index].r-tree[index].l+)*val;
tree[index].Max += val;
tree[index].Min += val;
tree[index].lazy += val;//延时标记
return ;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>;
if(l <= mid){
update(l,r,index<<,val);
}
if(r > mid){
update(l,r,index<<|,val);
}
pushup(index);
} LL query_range(int l,int r,int index){//区间查询
if(l <= tree[index].l && r >= tree[index].r){
return tree[index].val;
//return tree[index].mn;
}
pushdown(index);
int mid = (tree[index].l+tree[index].r)>>;
LL res = ;
LL Max = ;
LL Min = inf;
if(l <= mid){
res += query_range(l,r,index<<);
}
if(r > mid){
res += query_range(l,r,index<<|);
}
return res;
} LL query_max(int l,int r,int index){//最大值查询
if(l<= tree[index].l && r>= tree[index].r ){
return tree[index].Max;
}
pushdown(index);
int mid = (tree[index].l + tree[index].r )>>;
LL ans = ;
LL MAX = ,MIN = inf;
if(l <= mid) MAX = max(query_max(l,r,index<<),MAX);
if(r > mid) MAX = max(query_max(l,r,index<<|),MAX);
return MAX;
}
LL query_min(int l,int r,int index){//最小值查询
if(l<= tree[index].l && r>= tree[index].r ){
return tree[index].Min;
}
pushdown(index);
int mid = (tree[index].l + tree[index].r )>>;
LL ans = ;
LL MAX = ,MIN = inf;
if(l <= mid) MIN = min(query_min(l,r,index<<),MIN);
if(r > mid) MIN = min(query_min(l,r,index<<|),MIN);
return MIN;
}
string s;
LL ss[maxn];
int main(){
int n;
scanf("%d",&n);
n = n+;
build(,n,);
cin>>s;
int pos = ;
vector<int> ans;
for(int i = ;i<s.length() ;i++){
if(s[i] == '('){
update(pos,n,,-ss[pos]);
ss[pos] = ;
}
else if(s[i] == ')'){
update(pos,n,,--ss[pos]);
ss[pos] = -;
}
else if(s[i] == 'R'){
pos++;
}
else if(s[i] == 'L'){
if(pos>) pos--;
}
else{
if(ss[pos] == ){
update(pos,n,,-);
}
else if(ss[pos] == -){
update(pos,n,,);
}
ss[pos] = ;
}
if(query_min(,n,) == && query_range(n,n,) == ){
ans.push_back(query_max(,n,));
}
else{
ans.push_back(-);
}
}
for(int i = ;i<ans.size() ;i++){
cout<<ans[i]<<" ";
}
//cout<<endl;
return ;
}

codeforces div2 603 E. Editor(线段树)的更多相关文章

  1. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

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

  3. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  4. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

    E. Correct Bracket Sequence Editor   Recently Polycarp started to develop a text editor that works o ...

  5. Codeforces #504(div1+div2) 1023D Array Restoration(线段树)

    题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...

  6. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  7. Codeforces 588E. A Simple Task (线段树+计数排序思想)

    题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...

  8. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  9. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

随机推荐

  1. R语言函数化编程笔记2

    R语言函数化编程笔记2 我学过很多的编程语言,可以我写的代码很啰嗦,一定是我太懒了.或许是基础不牢地动山摇 1.为什么要学函数 函数可以简化编程语言,减少重复代码或者说面向对象的作用 2.函数 2.1 ...

  2. 51nod(1089 最长回文子串 V2)(hash 加二分)

    1089 最长回文子串 V2(Manacher算法)   回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串. 输入一个字符串Str,输出Str里最长回文子串的长度.   输入 ...

  3. SVN的使用01

    关于svn的使用以及TortoiseSVN常见操作 一.关于svn介绍 在介绍之前提一下,MyEclipse项目组的建立,以及源文件夹的创建. 新建的那一栏点击other 在搜索栏中搜索Java Wo ...

  4. 为data中的某一个对象添加一个属性不起作用——this.$set的正确使用

    this.$set(obj, key, value) 我们在项目开发的过程中,经常会遇到这种情况:为data中的某一个对象添加一个属性 <template> <div class=& ...

  5. [HNOI2003] 消防局的设立 - 树形dp

    仍然是点覆盖集问题,但覆盖半径变成了\(2\) 延续上一题的思路,只是式子更加复杂了 想体验一下min_element大法于是不想优化了 #include <bits/stdc++.h> ...

  6. PyTorch对ResNet网络的实现解析

    PyTorch对ResNet网络的实现解析 1.首先导入需要使用的包 import torch.nn as nn import torch.utils.model_zoo as model_zoo # ...

  7. 四种webAPP横向滑动模式图解—H5页面开发

    一.容器整体滑动(DEMO只演示A-B-C-B,下同) 模拟动画效果见下图(上),滑动分解见下图(下): DEMO地址:http://nirvana.sinaapp.com/demo_slider/s ...

  8. python面试的100题(15)

    41.super函数的具体用法和场景 为了调用父类(超类)的一个方法,可以使用 super() 函数,比如: class A: def spam(self): print('A.spam') clas ...

  9. Docker+JMeter+InfluxDB+Grafana从容器内部发起压测

    1.自由定制JMeter镜像: Dockerfile文件: FROM java:8# 基础镜像 MAINTAINER yangjianliang <526861348@qq.com># 作 ...

  10. 使用在react hooks+antd ListView简单实现移动端长列表功能

    import React, { useState, useEffect } from "react" import { ListView } from "antd-mob ...