codeforces div2 603 E. Editor(线段树)
题目链接: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(线段树)的更多相关文章
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- 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 ...
- Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- 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 ...
- Codeforces #504(div1+div2) 1023D Array Restoration(线段树)
题目大意:给你一个数组,数组是经过q次区间覆盖后的结果,第i次覆盖是把区间内的值赋值为i,其中有若干个地方数值未知(就是0),让你判断这个数组是否可以经过覆盖后得到的,如果可以,输出任意一种可行数组. ...
- codeforces 22E XOR on Segment 线段树
题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...
- Codeforces 588E. A Simple Task (线段树+计数排序思想)
题目链接:http://codeforces.com/contest/558/problem/E 题意:有一串字符串,有两个操作:1操作是将l到r的字符串升序排序,0操作是降序排序. 题解:建立26棵 ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
随机推荐
- Spring Batch 批处理原则与建议
Spring Batch 批处理原则与建议 当我们构建一个批处理的过程时,必须注意以下原则: 通常情况下,批处理的过程对系统和架构的设计要够要求比较高,因此尽可能的使用通用架构来处理批量数据处理,降低 ...
- vue踩坑:vue+ element ui 表单验证有值但验证失败。
一.如图:有值但是验证失败 二. <el-form :model="form" :rules="rules"> <el-form-item l ...
- Verilog-异步FIFO
参考博文:https://blog.csdn.net/alangaixiaoxiao/article/details/81432144 1.概述 异步FIFO设计的关键是产生“写满”和“读空”信号,这 ...
- 记录 Docker 的学习过程 (网络篇)
打开2个会话,分别运行以下命令 # docker run -it -P --name nginx2 nginx #-P 端口随机映射 再打开一个会话查看 运行中的容器 # docker ps -aCO ...
- 机器学习作业(四)神经网络参数的拟合——Matlab实现
题目下载[传送门] 题目简述:识别图片中的数字,训练该模型,求参数θ. 第1步:读取数据文件: %% Setup the parameters you will use for this exerci ...
- 安卓开发中遇到java.net.SocketException: Permission denied
仅需在AndroidManifest.xml添加 <uses-permission android:name="android.permission.INTERNET" /& ...
- python3运行调用htmltestrunner时,报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0
之前解决过一次,又忘了,这次写下来了..百度没有的,跟我环境有关! 环境:自动化运行环境python3.6.5 上期说到了,写了一个bat来运行runallcase.py. 但是双击运行却报错:Uni ...
- Selenium原理
from selenium import webdriver:导入webdriver模块 当导入webdriver模块时,会执行\selenium\webdriver目录下的__init__.py ...
- 如何使用cmd操作数据库
1.首先用win+r输入cmd打开命令工具 (https://www.mysqlzh.com/=>'mysql中文文档地址') 然后用cd 命令进入你mysql的根目录 (bin目录下) 输入命 ...
- 使用wsgiref手撸web框架
模板 前言 要说到应用程序,就不得不提的就是cs架构和BS架构 所谓的cs架构就是client端和server端,就像我们的电脑上的qq,微信等应用程序 bs架构就是浏览器端和server端,我们不需 ...