Editor
我们把“(”用1表示,“)”用-1表示,其余字母用0表示,这样形成的一个数组,我们求出它的前缀和
sum[],只有当\(sum[n]==0\)且\(min(sum[])==0\)中的时候,才表示括号正好匹配,且最大嵌套数为\(max(sum[])\)
对于一个数组来说,要实现多次单点查询、区间修改、区间求极值这三个功能的话,我们可以利用线段树来提高其效率。
算法的板子没事多敲敲,不然很容易生疏....
代码:
// Created by CAD on 2019/12/3.
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define lson (p<<1)
#define rson (p<<1|1)
using namespace std;
const int maxn=1e6+10;
struct node{
int maxx=0,minn=0;
int sum=0;
}d[maxn<<2];
int a[maxn],laz[maxn<<2];
void pushdown(int s,int t,int p)
{
int m=(s+t)>>1;
d[lson].sum+=laz[p]*(m-s+1),d[rson].sum+=laz[p]*(t-m);
d[lson].maxx+=laz[p],d[rson].maxx+=laz[p];
d[lson].minn+=laz[p],d[rson].minn+=laz[p];
laz[lson]+=laz[p],laz[rson]+=laz[p];
laz[p]=0;
}
void update(int l,int r,int c,int s,int t,int p)
{
if(l<=s&&t<=r)
{
d[p].sum+=(t-s+1)*c,laz[p]+=c;
d[p].minn+=c,d[p].maxx+=c;
return ;
}
int m=(s+t)>>1;
if(laz[p]) pushdown(s,t,p);
if(l<=m) update(l,r,c,s,m,lson);
if(r>m) update(l,r,c,m+1,t,rson);
d[p].maxx=max(d[lson].maxx,d[rson].maxx);
d[p].minn=min(d[lson].minn,d[rson].minn);
d[p].sum=d[lson].sum+d[rson].sum;
}
int que_min(int l,int r,int s,int t,int p)
{
if(l<=s&&t<=r) return d[p].minn;
int m=(s+t)>>1;
if(laz[p]) pushdown(s,t,p);
int minn=inf;
if(l<=m) minn=min(que_min(l,r,s,m,lson),minn);
if(r>m) minn=min(que_min(l,r,m+1,t,rson),minn);
return minn;
}
int que_max(int l,int r,int s,int t,int p)
{
if(l<=s&&t<=r) return d[p].maxx;
int m=(s+t)>>1;
if(laz[p]) pushdown(s,t,p);
int maxx=0;
if(l<=m) maxx=max(que_max(l, r, s, m, lson), maxx);
if(r>m) maxx=max(que_max(l, r, m+1, t, rson), maxx);
return maxx;
}
int getsum(int l,int r,int s,int t,int p)
{
if(l<=s&&t<=r) return d[p].sum;
int m=(s+t)>>1;
if(laz[p]) pushdown(s,t,p);
int sum=0;
if(l<=m) sum+=getsum(l,r,s,m,p<<1);
if(r>m) sum+=getsum(l,r,m+1,t,p<<1|1);
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
string s; cin>>s;
int loc=1;
for(auto i:s)
{
if(i=='L') loc=max(1,loc-1);
else if(i=='R') loc++;
else {
int k=a[loc];
if(i=='(') a[loc]=1;
else if(i==')') a[loc]=-1;
else a[loc]=0;
update(loc,n,-k+a[loc],1,n,1);
}
if(que_min(1,n,1,n,1)!=0||getsum(n,n,1,n,1)!=0) cout<<-1<<" ";
else cout<<que_max(1,n,1,n,1)<<" ";
}
cout<<endl;
return 0;
}
Editor的更多相关文章
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- Bzoj1269 [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3678 Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可正 ...
- Unity3D Editor 扩展
官方教程:链接 EditorLayout API:链接 Handles API:链接 1.首先来个Inspector面板Editor的实现 要实现一个组件在Inspector中的Editor功能,首先 ...
- 推荐eclipse插件Properties Editor
需求:一般我们在做"国际化"功能时,我们需要properties中文表示方式用unicode表示.eclipse默认properties文件编辑器不方便查看,需要我们查看常常查找u ...
- Delphi Code Editor 之 编辑器选项
Delphi Code Editor 之 编辑器选项 可从Code Editor的右键菜单中选择“Properties”菜单项来查看编辑器选项.也可以从主菜单[Tools | Editor Optio ...
- SharedPreferences.Editor 的apply()与commit()方法的区别
commit()的文档 官方文档如下: Commit your preferences changes back from this Editor to the SharedPreferences o ...
- Easyui datagrid editor为combobox时指定数据源
当在datagrid行内部应用添加编辑操作时,引入combobox是非常方便的操作,我在引入combobox时对数据源这快做个总结,在做demo的过程中遇到个问题,就是当你选择了下拉框的值后点击保存, ...
- Microsoft Visual Studio 2008 未能正确加载包“Visual Web Developer HTML Source Editor Package” | “Visual Studio HTM Editor Package”
在安装Microsoft Visual Studio 2008 后,如果Visual Studio 2008的语言版本与系统不一致时,比如:在Windows 7 English System 安装Vi ...
- Devexpress WPF Theme Editor 02
Devexpress WPF Theme Editor 01 对于上次我们生成的主题 开始添加到我们的项目中. 运行Visual Studio和打开一个WPF解决方案,你想申请的主题. 添加的主题集会 ...
- easyui datagrid 动态操作editor 的方法
easyui本身是不提供这么细节的功能的,需要我们自己拓展下: 在easyui.min.js中扩展: $.extend($.fn.datagrid.methods, { addEditor : fun ...
随机推荐
- (四)Spring Boot之配置文件-多环境配置
一.Properties多环境配置 1. application.properties配置激活选项 spring.profiles.active=dev 2.添加其他配置文件 3.结果 applica ...
- .Net Core Swagger:Actions require an explicit HttpMethod binding for Swagger 2.0
添加完Swagger包引用后运行报错:Actions require an explicit HttpMethod binding for Swagger 2.0 第一时间想到了父类控制器 没有添加 ...
- 【原创】大叔经验分享(94)jdbc连接mysql、sqlserver、oracle
Mysql driver下载:https://mvnrepository.com/artifact/mysql/mysql-connector-java import java.sql.*; publ ...
- gin框架实现一个简单的项目 ③
承接:gin框架封装自己的路由 ② 对于一个项目来说,需要将各个功能模块分开,也就是所谓的三层模型,这里介绍一下个人的做法: contorller主要负责路由 model主要负责程序输入输出的数据 s ...
- Java数据结构总述
array list map set 链表..array 和list类似,增删慢,读取快,list长度可变,array长度固定, 链表增删快的list set 是一个没有重复数据的集合 map 是一个 ...
- 三:后台的登录注册接口(moon项目,前面有一,二)
** 项目一共有 16 个页面,是一个电商网销项目,自己在网上的某网上找的一个要做的网站的设计图: 页面主要包括: 登录页 -- 注册页 -- 首页 -- 产品列表页 -- 产品详情页 -- 会员中 ...
- dubbo框架梳理
Dubbo分层 Dubbo框架运行主要分如下九层: 配置层:config 服务代理层:proxy 注册中心层:registry 路由层:cluster 监控层:monitor 远程调用层:protoc ...
- 《python解释器源码剖析》第12章--python虚拟机中的函数机制
12.0 序 函数是任何一门编程语言都具备的基本元素,它可以将多个动作组合起来,一个函数代表了一系列的动作.当然在调用函数时,会干什么来着.对,要在运行时栈中创建栈帧,用于函数的执行. 在python ...
- Linux终端彩色显示输出结果
转义序列符 为了添加个人风格的彩色提示符,我们需要使用转义序列符.一个转义序列符是一个控制命令,它控制Shell执行某个特定的动作.一个转义序列符通常以 ESC开始,在Shell中被记录为" ...
- ZZNUOJ-2155-单身man集合-【标程做法:数位DP-1-10^8,提前暴力打表法: 砍时间复杂度到10^5】
ZZNUOJ-2155: 单身MAN集合 题目描述: 单身man们突然集结起来了,虽然我们不知道它们想要干什么.你作为单身man的首领需要管理好每一只单身man,机智的你给每一只单身man编了一个编号 ...