题目链接: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. 利用GRC进行安全研究和审计 – 将无线电信号转换为数据包(转)

    0x00 介绍 InGuardians作为一家从事信息安全研究和咨询的公司,自创立以来不但关注着web应用的渗透测试,网络取证,嵌入式设备等领域也致力于无线网络的评估方法上面的研究.在期间无线网络评估 ...

  2. .Net Core 智能提示汉化包

    在.Net Core 2.x 版本,Microsoft 官方没有提供 .Net Core 正式版的多语言安装包.因此,我们在用.Net Core 2.x 版本作为框架目标编写代码时,智能提成是英文的. ...

  3. asp.net core 配置文件动态更新

    IOptions<T> //站点启动后,获取到的值永远不变 IOptionsSnapshot<T> //站点启动后,每次获取到的值都是配置文件里的最新值 (reloadOnCh ...

  4. python xlrd 模块(获取Excel表中数据)

    python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...

  5. IntelliJ IDEA 2019.3安装与激活(附注册码)

    转载声明:本文是根据 https://segmentfault.com/a/1190000021220727?utm_source=tag-newest 并结合个人的安装完成情况进行了改动,从而生成此 ...

  6. java编码解码过程

    最近做项目的时候,有时会遇到中文乱码的问题,网上查询了很多资料,发现大多都是只讲解决方案,并没有讲到为什么要使用这种方案,这种方案的原理是什么? 最典型的就是连接数据库的URL,我们一般把它放到cla ...

  7. pom中<scope></scope>一些理解

    compile:默认值,表示当前依赖包,要参与当前项目的编译,后续测试,运行时,打包provided:代表在编译和测试的时候用,运行,打包的时候不会打包进去test:表示当前依赖包只参与测试时的工作: ...

  8. 使用yum安装报错:[Errno 256] No more mirrors to try

    背景:我使用yum方式安装软件时,比如zabbix这种软件,我们在安装时一般都是直接到zabbix官网,按照官方的步骤进行安装,但是有一个问题,官方的服务器不在国内,时常会在安装时导致超时报错.此时解 ...

  9. java通过浏览器请求头(User-Agent)获取访问者设备信息以及系统版本

    个人博客 地址:http://www.wenhaofan.com/article/20181125220342 在开发AutuBlog项目时需要做后台的登录记录,想起浏览器的User-Agent,于是 ...

  10. wget安装nginx

    #下载: wget http://nginx.org/download/nginx-1.8.0.tar.gz #解压: tar -zxvf nginx-1.8.0.tar.gz #安装依赖插件 yum ...