E. Correct Bracket Sequence Editor
 

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).

Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":

  • 1st bracket is paired with 8th,
  • 2d bracket is paired with 3d,
  • 3d bracket is paired with 2d,
  • 4th bracket is paired with 7th,
  • 5th bracket is paired with 6th,
  • 6th bracket is paired with 5th,
  • 7th bracket is paired with 4th,
  • 8th bracket is paired with 1st.

Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:

  • «L» — move the cursor one position to the left,
  • «R» — move the cursor one position to the right,
  • «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).

After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).

There are pictures illustrated several usages of operation "D" below.

All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.

Polycarp is very proud of his development, can you implement the functionality of his editor?

Input

The first line contains three positive integers nm and p (2 ≤ n ≤ 500 000, 1 ≤ m ≤ 500 000, 1 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.

It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.

Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.

Output

Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

Examples
input
8 4 5
(())()()
RDLD
output
()
 
Note

In the first sample the cursor is initially at position 5. Consider actions of the editor:

  1. command "R" — the cursor moves to the position 6 on the right;
  2. command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
  3. command "L" — the cursor moves to the position 4 on the left;
  4. command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.

Thus, the answer is equal to ().

 题意:

  给你一个字符串只包含(),为合法匹配的括号串

  给你一系列的操作LRD

  问你最后这个串变成什么了

题解:

  每次操作我们用线段树第k大寻找相邻位置对应左右移动

  对于操作就是区间修改了

  可以先用栈预处理这个区间出来

  都能在线段树上操作

#include<bits/stdc++.h>
using namespace std;
const int N = 1e7+, M = 1e6+, mod = 1e9+; typedef long long ll; char s[N],op[N];
int n,m,p,f[N];
stack<int > q;
int l[N],r[N],sum[N],lazy[N];
void pushdown(int k) {
if(lazy[k]==-) return ;
sum[k<<] = ;
sum[k<<|] = ;
lazy[k<<] = ;
lazy[k<<|] = ;
lazy[k] = -;
sum[k] = sum[k<<]+sum[k<<|];
}
void build(int k,int s,int t) {
l[k] = s;r[k] = t;
sum[k] = ;
lazy[k] = -;
if(s==t) {
sum[k] = ;
return ;
}
int mid = (s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
sum[k] = sum[k<<]+sum[k<<|];
}
void update(int k,int s,int t) {
if(lazy[k]!=-) {
pushdown(k);
}
if(l[k]==s&&r[k]==t) {
sum[k] = ;
lazy[k] = ;
return ;
}
int mid = (l[k]+r[k])>>;
if(t<=mid) {
update(k<<,s,t);
}
else if(s>mid) update(k<<|,s,t);
else {
update(k<<,s,mid);
update(k<<|,mid+,t);
}
sum[k] = sum[k<<]+sum[k<<|];
}
int ask(int k,int x) {
if(lazy[k]!=-) pushdown(k);
if(l[k]==x&&r[k]==x) {
return sum[k];
}
int mid = (l[k]+r[k])>>;
if(x<=mid) return ask(k<<,x);
else return ask(k<<|,x);
} int ask(int k,int x,int y) {
if(lazy[k]!=-) pushdown(k);
if(l[k]==x&&r[k]==y) {
return sum[k];
}
int mid = (l[k]+r[k])>>;
if(y<=mid) {
return ask(k<<,x,y);
}
else if(x>mid) return ask(k<<|,x,y);
else {
return ask(k<<,x,mid) + ask(k<<|,mid+,y);
}
sum[k] = sum[k<<]+sum[k<<|];
} int query2(int id, int s, int t, int k){
if(lazy[id]!=-) pushdown(id);
if(s == t){
return s;
}
int mid = (s+t)>>;
if(sum[id<<] >= k) {
return query2(id<<, s , mid, k);
}else {
return query2(id<<|, mid + , t, k - sum[id<<]);
}
} int main() {
scanf("%d%d%d",&n,&m,&p);
scanf("%s%s",s+,op+);
for(int i=;i<=n;i++) {
if(s[i]=='(') {
q.push(i);
}
else {
int k = q.top();
f[i] = k;
f[k] = i;
q.pop();
}
}
build(,,n);
for(int i=;i<=m;i++) {
if(op[i]=='R') {
int tmp = ask(,,p);
tmp+=;
p = query2(,,n,tmp);
}
else if(op[i]=='L') {
int tmp = ask(,,p-) ;
p = query2(,,n,tmp);
}
else {
update(,min(f[p],f[f[p]]),max(f[p],f[f[p]]));
p = max(f[p],f[f[p]]);
int tmp = ask(,,p) ;
if(ask(,p,n)) {
p = query2(,,n,tmp+);
}
else if(tmp) {
p = query2(,,n,tmp);
}
else break;
}
}
for(int i=;i<=n;i++) {
if(ask(,i)) {
printf("%c",s[i]);
}
}
cout<<endl;
return ;
}

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟的更多相关文章

  1. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  2. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

  3. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)

    题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...

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

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

  5. Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp

    D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...

  6. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  7. Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树

    C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...

  8. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  9. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心

    B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...

随机推荐

  1. gdb打印vector

    1.gdb版本大于7.0 (gdb) p yourVector 2.打印整个vector (gdb) p *(yourVector._M_impl._M_start)@yourVector.size( ...

  2. C#微信公众号的开发——服务配置

    最近因为需要用C#开发微信公众号的一些功能,记录一下开发公众号的一些坑..... 首先先介绍一下,微信公众号的官方文档.虽然这个文档我感觉比较糙,但是还是可以借鉴一下让我们摸着石头过河的. 首先我们得 ...

  3. C#接入第三方支付一些小问题

    13年第一次接入支付宝的时候,支付宝的api还不是很好用,费了些劲才完成,本月再次接入的时候发现已经很好用了,接入过程非常顺畅,只出现了一个小问题,我的金额默认是保留了4位小数,支付宝api只接受最多 ...

  4. Cannot find module 'crc'

    这个时候你只需要打开你nodejs安装的目录,在其中执行 npm install crc(这里查什么模块(module)就安装什么模块).

  5. Jquery IE8兼容性

    环境: jsp+jquery-1.11.1.min.js 问题描述: 使用$("#article标签id名").append(“xxxxxxxxx") ,chrome.f ...

  6. PANDAS 数据分析初学者教程

    Pandas 初学者教程       2018-05-19 六尺巷人 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重要的Python包.它不仅提供了很多方法,使得数据处理非常 ...

  7. CentOS 安装dotNetCore

    如果要在CentOS上运行.net Core程序,必须安装.net Core Sdk 具体安装 方法,可以参考微软官方站点说明,非常详细: 1)百度搜索 .Net Core 2)先择CentOS版本: ...

  8. ROS:ubuntu-Ros使用OrbSLAM

    一般无误的官方连接:https://github.com/raulmur/ORB_SLAM ubuntu16.04没有多少改变,还是使用kinetic老代替indigo Related Publica ...

  9. Android读写文件

    1.从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写) String res = ""; try{ InputStream in = getResour ...

  10. WebGL画点程序v3

    本文程序实现画一个点的任务,如下图.其中,点的颜色由Javascript传到片元着色器程序中. 整个程序包含两个文件,分别是: 1. HelloPoint3.html <!DOCTYPE HTM ...