E. 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的更多相关文章

  1. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  2. Bzoj1269 [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3678  Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可正 ...

  3. Unity3D Editor 扩展

    官方教程:链接 EditorLayout API:链接 Handles API:链接 1.首先来个Inspector面板Editor的实现 要实现一个组件在Inspector中的Editor功能,首先 ...

  4. 推荐eclipse插件Properties Editor

    需求:一般我们在做"国际化"功能时,我们需要properties中文表示方式用unicode表示.eclipse默认properties文件编辑器不方便查看,需要我们查看常常查找u ...

  5. Delphi Code Editor 之 编辑器选项

    Delphi Code Editor 之 编辑器选项 可从Code Editor的右键菜单中选择“Properties”菜单项来查看编辑器选项.也可以从主菜单[Tools | Editor Optio ...

  6. SharedPreferences.Editor 的apply()与commit()方法的区别

    commit()的文档 官方文档如下: Commit your preferences changes back from this Editor to the SharedPreferences o ...

  7. Easyui datagrid editor为combobox时指定数据源

    当在datagrid行内部应用添加编辑操作时,引入combobox是非常方便的操作,我在引入combobox时对数据源这快做个总结,在做demo的过程中遇到个问题,就是当你选择了下拉框的值后点击保存, ...

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

  9. Devexpress WPF Theme Editor 02

    Devexpress WPF Theme Editor 01 对于上次我们生成的主题 开始添加到我们的项目中. 运行Visual Studio和打开一个WPF解决方案,你想申请的主题. 添加的主题集会 ...

  10. easyui datagrid 动态操作editor 的方法

    easyui本身是不提供这么细节的功能的,需要我们自己拓展下: 在easyui.min.js中扩展: $.extend($.fn.datagrid.methods, { addEditor : fun ...

随机推荐

  1. (十五)Activitivi5之多用户任务分配

    一.概念 我们在开发的时候,有一种情况是这样的, 我们有一个任务,可以让多个用户中的任何一个人办理即可,比如某个审批任务, 张三,李四,王五他们中的任何一人办理下都行,这时候,我们用到多用户任务分配. ...

  2. (四)Decorator设计模式解决GET/POST请求的乱码问题(转)

    一.Decorator设计模式 1.1.Decorator设计模式介绍 当某个对象的方法不适应业务需求时,通常有2种方式可以对方法进行增强: 编写子类,覆盖需增强的方法. 使用Decorator设计模 ...

  3. IIs发布的项目无法打开问题

    1/查看一下ISAPI筛选器,是否存在2.0,4.0,若缺少东西,就从新安装一下iis,存在某些程序没有被勾选,一般是asp.net3.5,asp.net4.0

  4. adminMongo:mongoDB node GUI(mongoDB图形化界面)

    adminMongo:mongoDB node GUI(mongoDB图形化界面) 获取项目项目 克隆:git clone https://github.com/mrvautin/adminMongo ...

  5. Django rest-framework框架-组件之视图

    视图: a. django class Test(View): ... b. rest_framework class Test(APIView): ... c. GenericAPIView 一般不 ...

  6. controller 层 date 类型的参数,SpringBoot自动转换 dateformat

    这样客户端提交的String 就可以自动转换为Date了!!!

  7. React/react相关小结

    React React组件由React元素组成,React组件使用React.Component或React.PureComponent来生成:React元素使用JSX的语法来编写或使用React.c ...

  8. c#开启线程池超出索引

    这样写会超出索引,foreach好像不会超出,原因可能是开启线程池需要时间,成功开启之后,一次循环已经结束,这个没有验证. 以下这个做法是不对的,我也是看网上的贴这样写,结果是少执行了一个.推荐大家还 ...

  9. js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息

    文件名的传递 ---全路径获取 $('#file').change(function(){ $('#em').text($('#file').val()); }); 文件名的传递 ---只获取文件名 ...

  10. Kubernetes的核心技术概念和API对象

    Kubernetes的核心技术概念和API对象 API对象是K8s集群中的管理操作单元.K8s集群系统每支持一项新功能,引入一项新技术,一定会新引入对应的API对象,支持对该功能的管理操作.例如副本集 ...