题意:有一个无限长度的文本编辑器,刚开始没有内容,光标在第一格,接下来有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(线段树,栈)的更多相关文章

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

  2. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

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

  4. codeforces div2 603 E. Editor(线段树)

    题目链接:https://codeforces.com/contest/1263/problem/E 题意:一个编译器,每次输入一些字符,R表示光标右移,L表示光标左移,然后有一些左括号(  和 右括 ...

  5. hdu4699 Editor 2013 多校训练第十场 D题 数列维护 splay | 线段树 | 栈!!!!!

    题意:维护一个文本编辑,并且查询最大前缀和. 写了splay,wa了13次 过了之后觉着特傻逼.发现题解两个栈就可以了,光标前后维护两个栈,维护前面的栈的前缀和 和 最大前缀和. 哎,傻逼,太弱了,还 ...

  6. The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...

  7. 2019南昌网络赛-I(单调栈+线段树)

    题目链接:https://nanti.jisuanke.com/t/38228 题意:定义一段区间的值为该区间的和×该区间的最小值,求给定数组的最大的区间值. 思路:比赛时还不会线段树,和队友在这题上 ...

  8. 网络赛 I题 Max answer 单调栈+线段树

    题目链接:https://nanti.jisuanke.com/t/38228 题意:在给出的序列里面找一个区间,使区间最小值乘以区间和得到的值最大,输出这个最大值. 思路:我们枚举每一个数字,假设是 ...

  9. 南昌邀请赛I.Max answer 单调栈+线段树

    题目链接:https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value of a in ...

随机推荐

  1. SpreadJS 纯前端表格控件 V12.2 发布更新

    用不到100行代码,在前端实现Excel的全部功能 千万前端开发者翘首企盼,SpreadJS V12.2 终发布更新:六大功能特性,带来更多便利,用不到100行代码,在前端实现Excel的全部功能! ...

  2. Kick Start 2019 Round F Teach Me

    题目链接 题目大意 有 $N$ 个人,$S$ 项技能,这些技能用 $1, 2, 3, \dots, S$ 表示 .第 $i$ 个人会 $c_i$ 项技能($ 1 \le c_i \le 5 $).对于 ...

  3. P1004方格取数

    这是提高组得一道动态规划题,也是学习y氏思考法的第一道题. 题意为给定一个矩阵,里面存有一些数,你从左上角开始走到右下角,另一个人从右下角开始走到左上角,使得两个人取数之和最大,当然一个数只可以取走一 ...

  4. LOJ526「LibreOJ β Round #4」子集

    题目 算是比较裸的题吧. 首先我们把符合要求的\((i,j)\)建一条边,那么我们要求的就是最大团. 转化为补图的最小独立集. 然后我们来证明补图是一个二分图. \((u,v)\)有边\(\Leftr ...

  5. Luogu P5354 [Ynoi2017]由乃的OJ

    题目 这题以前叫睡觉困难综合征. 首先我们需要知道起床困难综合征怎么做. 大概就是先用一个全\(0\)和全\(1\)的变量跑一遍处理出每一位\(1\)和\(0\)最后会变成什么. 然后高位贪心:如果当 ...

  6. AppCan模拟器调试

    来源: 1, 页面CSS调试 2, JS调试 3, 插件请打包后手机调试 4, 打开另一个页面示例: appcan.button("#myBtn", "ani-uct&q ...

  7. python3.7 安装Scrapy 失败问题

    python的Scrapy框架,需要Twisted依赖以及VC++ 14 以上的环境,这些就不再赘述.讲讲今天安装Twisted和Scrapy遇到的其他问题. 首先就是直接安装Twisted成功后,安 ...

  8. Windows下使用SVN版本控制工具

    一.SVN工作原理 SVN(SubVersion)的基本工作思路是这样的:在一台服务器上建立一个源代码库,库里可以存放许多不同项目的源程序,由源代码库管理员统一管理这些源程序. 每个用户在使用源代码库 ...

  9. Springboot提示数据库连接问题Connection is not available

    2019-05-29 11:19:51.824 WARN 854 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL ...

  10. (转)Redis持久化的几种方式

    radis持久化的几种方式 1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服 ...