【CF1263E】Editor(线段树,栈)
题意:有一个无限长度的文本编辑器,刚开始没有内容,光标在第一格,接下来有n个操作,操作可能有3种:
1.光标左移一格,如果已经在第一格则不动
2.光标右移一格
3.将当前光标所在格的字符改成输入的字符
每次操作后问当前所有内容对于括号序列是否合法,若非法输出-1,否则输出括号的最大层数
n<=1e6
思路:最大层数即将左括号看做-1,右括号看做1之后的最大前缀和
做法一:线段树单点修改
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,ll>P;
#define N 2000000+10
#define M 200000+10
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1
#define fors(i) for(auto i:e[x]) if(i!=p) const int MOD=,inv2=(MOD+)/;
//int p=1e4+7;
//double eps=1e-6;
int dx[]={-,,,};
int dy[]={,,-,}; struct node
{
int s,mx,mn;
}t[N<<]; char s[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} ll readll()
{
ll v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void build(int l,int r,int p)
{
t[p].s=t[p].mx=t[p].mn=;
if(l==r) return;
int mid=(l+r)>>;
build(l,mid,ls);
build(mid+,r,rs);
} void pushup(int p)
{
t[p].s=t[ls].s+t[rs].s;
t[p].mx=max(t[ls].mx,t[ls].s+t[rs].mx);
t[p].mn=min(t[ls].mn,t[ls].s+t[rs].mn);
} void update(int l,int r,int x,int d,int p)
{
if(l==r)
{
t[p].s=d;
t[p].mx=d;
t[p].mn=d;
return;
}
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,d,ls);
else update(mid+,r,x,d,rs);
pushup(p);
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int n=read();
build(,1e6+,);
int now=;
rep(i,,n)
{
char c=getchar();
if(c=='L')
{
if(now>) now--;
if(t[].s!=||t[].mn<) printf("-1 ");
else printf("%d ",t[].mx);
//printf("now=%d t[1].s=%d t[1].mx=%d\n",now,t[1].s,t[1].mx);
continue;
}
if(c=='R')
{
now++;
if(t[].s!=||t[].mn<) printf("-1 ");
else printf("%d ",t[].mx);
//printf("now=%d t[1].s=%d t[1].mx=%d\n",now,t[1].s,t[1].mx);
continue;
}
int d=;
s[now]=c;
if(c=='(') d=;
if(c==')') d=-;
update(,1e6+,now,d,);
if(t[].s!=||t[].mn<) printf("-1 ");
else printf("%d ",t[].mx);
//printf("now=%d t[1].s=%d t[1].mx=%d\n",now,t[1].s,t[1].mx);
}
return ;
}
思路二:用栈维护光标左边的内容,右边的内容和当前左部分前缀和对于真正值的偏移量,维护的信息和线段树差不多
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,ll>P;
#define N 2000000+10
#define M 200000+10
#define INF 1e9
#define fi first
#define se second
#define MP make_pair
#define pb push_back
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1
#define fors(i) for(auto i:e[x]) if(i!=p) const int MOD=,inv2=(MOD+)/;
//int p=1e4+7;
//double eps=1e-6;
int dx[]={-,,,};
int dy[]={,,-,}; int a[N],s[N]; struct Set
{
int mn,mx;
int cnt[];
Set():mn(),mx(),cnt{}{cnt[]=;}
void Insert(int x)
{
cnt[x+]++;
mn=min(mn,x);
mx=max(mx,x);
}
void Erase(int x)
{
cnt[x+]--;
while(cnt[mn+]==) mn++;
while(cnt[mx+]==) mx--;
}
}; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} ll readll()
{
ll v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int n=read();
//printf("n=%d\n",n);
int offset=,p=,tot=;
Set l,r;
rep(i,,n)
{
char c=getchar();
if(c=='L')
{
if(p>)
{
l.Erase(s[p]);
p--;
r.Insert(-offset);
offset+=a[p];
}
}
else if(c=='R')
{
offset-=a[p];
r.Erase(-offset);
p++;
s[p]=s[p-]+a[p-];
l.Insert(s[p]);
}
else
{
int v=;
if(c=='(') v=;
if(c==')') v=-;
offset+=v-a[p];
tot+=v-a[p];
a[p]=v;
}
if(tot!=||min(l.mn,offset+s[p]+r.mn)<) printf("-1\n");
else printf("%d\n",max(l.mx,offset+s[p]+r.mx));
}
return ;
}
【CF1263E】Editor(线段树,栈)的更多相关文章
- 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 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- 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 div2 603 E. Editor(线段树)
题目链接:https://codeforces.com/contest/1263/problem/E 题意:一个编译器,每次输入一些字符,R表示光标右移,L表示光标左移,然后有一些左括号( 和 右括 ...
- hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!
题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- 2019南昌网络赛-I(单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...
- 网络赛 I题 Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 题意:在给出的序列里面找一个区间,使区间最小值乘以区间和得到的值最大,输出这个最大值. 思路:我们枚举每一个数字,假设是 ...
- 南昌邀请赛I.Max answer 单调栈+线段树
题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...
随机推荐
- dual Oracle兼容
CREATE OR REPLACE VIEW dual ASSELECT NULL::"unknown"WHERE 1 = 1;
- P1816忠诚
这是一个区间查询最值的问题,用线段树来做. 建树的时候,这里不是求和,应该是e[k].w=min(e[k*2].w,e[k*2+1].w),所以这里要注意以下,其次是查询的时候,因为本题不用让我们修改 ...
- AppCan模拟器调试
来源: 1, 页面CSS调试 2, JS调试 3, 插件请打包后手机调试 4, 打开另一个页面示例: appcan.button("#myBtn", "ani-uct&q ...
- redis在php中实际应用-list
1.LPUSH Redis Lpush 命令将一个或多个值插入到列表头部. 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作. 当 key 存在但不是列表类型时,返回一个错误.(在Re ...
- 使用Python基于OpenCV的验证码识别
Blog:https://blog.csdn.net/qq_40962368/article/details/89312429(Verification_Code_Identification) 步骤 ...
- Java小程序—录屏小程序(上半场)
做软件的三个步骤: (1)做什么? (2)怎么做? (3)动手做! ok,我们今天要做的是一个录屏软件,那怎么做呢?首先,我们小时候都玩过一种小人书,就是当你快速翻动书页时,书中的人物就会活灵活现的动 ...
- sql server lower函数
lower()函数 --将大写字符数据转换为小写字符数据 语法:lower(character_expression) --character_expression是指定要进行转换的字符串
- ECharts模拟百度迁徙实例
本实例原始信息: 作者:anix 演示地址:Echarts模拟迁徙 源码地址:GitHub-ananix-qianxi 前言 "百度地图春节人口迁徙大数据"(简称"百度迁 ...
- tp5实现Redis的简单使用
方法1: Controller <?php namespace app\index\controller; use think\Controller; use think\session\dri ...
- ELK监控交换机日志
一.首先部署logstash监控UDP514端口,新建一个配置文件cisco.conf 交换机是通过配置rsyslog服务器来将日志发送到日志服务器的,所以需要在logstash上配置rsyslog监 ...