题目链接: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. 自己的系统重装之后,怎么去重新的装官方的office办公软件,详细教程

    1  访问官网地址--微软,并通过自己的微软账号进行登录,转到下面的界面 2   点击上图的菜单栏的offce菜单项,跳转到下图 3  点击  菜单栏的产品  之后选择  查看office的全部的历史 ...

  2. react-native简单使用

    基本组件的使用介绍 View: Text: TextInput: Image: Button: ActivityIndicator: ScrollView:这是一个列表滚动的组件 ListView:也 ...

  3. 删除在wireshark中保存的filter的方法

    现在想删除下图的filter,方法是:Edit->preferences->Filter Expressions

  4. canvas转盘转动?

    怎么实现类似转盘转动的效果? 现在这种实现思路是,canvas每次draw()并不是让图形在旋转,而是让每一份的颜色改变到达好像是转动的效果, 但是现在有一个问题,一开始渲染的颜色数量于份数是相同的, ...

  5. 报表平台需求文档(V0.0.0.1)

    功能实现和发布版本严格遵照文档上内容. 1    主框架搭建 前端 样式模仿“钉钉工作台“ 2   前端页面 A  数据库配置页面 (1) 本系统(必须)[存入配置文件] 数据库配置 (2) 其他数据 ...

  6. Qt Gui 第一章~第二章

    一.Qt启动 qmake -project; 创建xxx.pro qmake xxx.pro; 生成makefile文件 make:构建该程序,生成可执行文件 运行程序:windows:xxx:mac ...

  7. DataGrid 的DataSource重新加载数据

    DataGrid 的DataSource重新加载数据,若直接重新给DataSource赋值是没有效果的,若只是修改原有数据中的单个值,此方法有效,但是针对完全不一样的数据直接重新赋值的方式是无效的,此 ...

  8. C语言回文链表

    要求:请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true思路:利用快慢双指针+反转半链 ...

  9. OpenCV-Mat结构详解

    前面博客中Mat函数谈到一些理解,但是理解的比较浅显,下面谈谈通道,行列等意义: Mat的常见属性 opencv中type类型· CV_<bit_depth>(S|U|F)C<num ...

  10. 《深入理解Java虚拟机》读书笔记十

    第十一章  晚期(运行期)优化 1.HotSpot虚拟机内的即时编译 解释器与编译器: 许多Java虚拟机的执行引擎在执行Java代码的时候都有解释执行(通过解释器执行)和编译执行(通过即时编译器产生 ...