Codeforces 670E - Correct Bracket Sequence Editor - [线段树]
题目链接:https://codeforces.com/contest/670/problem/E
题意:
给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号)。
有如下操作:
1、往左移动一下光标;
2、往左移动一下光标;
3、删除当前光标指向的括号,以及和它匹配的那个括号,以及这两个括号之间的所有括号。
要求你给出在做完所有操作后的括号串。
题解:
用线段树维护,每个括号是否存在,存在记为 $1$,被删掉了记为 $0$。
然后我们只需要实现:①区间求和、②区间赋值、③根据 $k$ 求最小的 $x$ 满足在区间 $[1,x]$ 上求和正好等于 $k$。
移动光标可以通过①和③来完成;删除可以通过②来删除,删除完之后的光标移动依然可以靠①③完成。
时间复杂度是 $O(n + m \log n)$,分别是建树的 $O(n)$ 以及 $m$ 次操作的时间复杂度。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+;
int n,m,p;
char s[maxn];
char op[maxn]; #define ls (rt<<1)
#define rs (rt<<1|1)
struct Node{
int l,r;
int val;
bool del;
void update() {
val=, del=;
}
}o[maxn<<];
void pushdown(int rt)
{
if(o[rt].del)
{
o[ls].update();
o[rs].update();
o[rt].del=;
}
}
inline void pushup(int rt) {
o[rt].val=o[ls].val+o[rs].val;
}
void build(int rt,int l,int r)
{
o[rt].l=l, o[rt].r=r;
o[rt].del=;
if(l==r)
{
o[rt].val=;
return;
}
int mid=(l+r)>>;
build(ls,l,mid);
build(rs,mid+,r);
pushup(rt);
}
void del(int rt,int st,int ed)
{
if(st<=o[rt].l && o[rt].r<=ed)
{
o[rt].update();
return;
}
pushdown(rt);
int mid=(o[rt].l+o[rt].r)>>;
if(st<=mid) del(ls,st,ed);
if(mid<ed) del(rs,st,ed);
pushup(rt);
}
int sum(int rt,int st,int ed)
{
if(st>ed) return ;
if(st<=o[rt].l && o[rt].r<=ed) return o[rt].val;
pushdown(rt);
int mid=(o[rt].l+o[rt].r)>>;
int res=;
if(st<=mid) res+=sum(ls,st,ed);
if(mid<ed) res+=sum(rs,st,ed);
pushup(rt);
return res;
}
int idx(int rt,int k)
{
if(o[rt].l==o[rt].r) return ;
pushdown(rt);
if(o[ls].val>=k) return idx(ls,k);
else return (o[ls].r-o[ls].l+)+idx(rs,k-o[ls].val);
} stack<int> S;
int bro[maxn]; int main()
{
cin>>n>>m>>p;
scanf("%s",s+);
for(int i=;i<=n;i++)
{
if(s[i]=='(') S.push(i);
if(s[i]==')') bro[S.top()]=i, bro[i]=S.top(), S.pop();
} scanf("%s",op+); build(,,n);
for(int q=;q<=m;q++)
{
if(op[q]=='L')
{
p=idx(,sum(,,p)-);
}
if(op[q]=='R')
{
p=idx(,sum(,,p)+);
}
if(op[q]=='D')
{
int st=min(p,bro[p]);
int ed=max(p,bro[p]);
del(,st,ed);
if(sum(,ed+,n)>)
p=idx(,sum(,,ed)+);
else
p=idx(,sum(,,st-));
}
} for(int i=;i<=n;i++)
{
if(sum(,i,i)>) printf("%c",s[i]);
}
}
Codeforces 670E - Correct Bracket Sequence 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(list和迭代器函数模拟)
E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces 670E - Correct Bracket Sequence Editor - [链表]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]
题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...
- CodeForces 670E Correct Bracket Sequence Editor
链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表
E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...
- 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- cf670E Correct Bracket Sequence Editor
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...
- (中等) UESTC 94 Bracket Sequence,线段树+括号。
There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...
随机推荐
- java 字符串中参数化符号${}的解析
我们在很多地方都能看到代表参数意义的符号${},可能我们在写一些框架的时候,有时候也需要用到这个符号,但他们是如何精确解析的?或者说需要我们自已写的时候,如何写?我们先来看以下的几个场景: 1.字符串 ...
- JProfiler进行Java运行时内存分析
原文地址:https://www.cnblogs.com/onmyway20xx/p/3963735.html 在最近的工作中,通过JProfiler解决了一个内存泄漏的问题,现将检测的步骤和一些分析 ...
- Mydumper介绍
Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具.开发人员主要来自MySQL,Facebook,SkySQL公司.目前已经在一些线上使用了Mydumper. 一.Myd ...
- Android在开发中的使用技巧之解决ScrollView嵌套RecyclerView出现的系列问题
根据已上线的app里总结出来的实用小技巧 相信大家都遇到过ScrollView嵌套RecyclerView或者RecyclerView嵌套RecyclerView来使用, 也会遇到一堆奇奇怪怪的问题, ...
- Xilinx 常用模块汇总(verilog)【03】
作者:桂. 时间:2018-05-10 2018-05-10 21:03:44 链接:http://www.cnblogs.com/xingshansi/p/9021919.html 前言 主要记 ...
- mysql8.0.11修改root密码,其他创建用户和删除用户
1.7. 查询用户密码: 查询用户密码命令:mysql> select host,user,authentication_string from mysql.user; host: 允许用户登录 ...
- Spring Boot 2.0 入门指南
0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ...
- mysql索引hash索引和b-tree索引的区别
Hash 索引结构的特殊性,其检索效率非常高,索引的检索可以一次定位,不像B-Tree 索引需要从根节点到枝节点,最后才能访问到页节点这样多次的IO访问,所以 Hash 索引的查询效率要远高于 B-T ...
- 如何生成ExecutionGraph及物理执行图
http://chenyuzhao.me/2017/02/06/flink%E7%89%A9%E7%90%86%E8%AE%A1%E5%88%92%E7%94%9F%E6%88%90/ https:/ ...
- MySQL四种事务隔离级别详解
本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...