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. 【转载】在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support

    在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone config ...

  2. SpringMVC 出现 406(Not Acceptable)

    首先,需要清楚,http state 406代表什么意思: 406是HTTP协议状态码的一种,表示无法使用请求的特性来响应请求的网页.一般指客户端浏览器不接受所请求页面的MIME类型. 出现这样的错误 ...

  3. java 框架-分布式服务框架2Dubbo

    https://blog.csdn.net/houshaolin/article/details/76408399 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的 ...

  4. javascript--HTML DOM常用元素对象

    二,Select:访问select元素 属性:.selectedIndex  获取select中当前选中项的下标 .options  获取select中所有的option元素 返回值为数组 .opti ...

  5. JavaScript--关于闭包(closure)

    js代码在执行前会做的几件事情: 1.代码检测 2.预编译:在执行代码之前会对代码中的函数以及变量提前声明 并且做一些其他的处理 1.函数在执行前的一瞬间,会生成一个OA(object action) ...

  6. wince如何扫描条码并且在浏览器上查询数据

    这个挺简单的,winform也适用 public override void OnGetBarcode(string scanStr) { try { Process.Start("iesa ...

  7. 【Day5】3.反爬策略之模拟登录

    import urllib.request as ur import user_agent import lxml.etree as le request = ur.Request( url='htt ...

  8. prometheus 配置项注意事项

    1.job:metrics_path 如果是静态模式下配置job(statics),直接配置job的metrics_path选项是不生效的.需要同时在exporter端指定metrics_path.如 ...

  9. 做一个函数 返回当前日期、当前时间 格式为“XXXX年XX月XX日”

    import time import datetime def time_strf(now_date):#传入0,1,2返回 当前日期.当前时间.当前日期与时间 today=datetime.date ...

  10. Beyond Compare 4提示已经过了30天试用期

    打开Beyond Compare 4,提示已经超出30天试用期限制,解决方法:1.修改C:\Program Files\Beyond Compare 4\BCUnrar.dll ,这个文件重命名或者直 ...