Codeforces Gym 100803G Flipping Parentheses 线段树+二分

时间:2023-02-01 20:49:24

Flipping Parentheses

题目连接:

http://codeforces.com/gym/100803/attachments

Description

A string consisting only of parentheses ‘(’ and ‘)’ is called balanced if it is one of the following.

• A string “()” is balanced.

• Concatenation of two balanced strings are balanced.

• When a string s is balanced, so is the concatenation of three strings “(”, s, and “)” in this

order.

Note that the condition is stronger than merely the numbers of ‘(’ and ‘)’ are equal. For instance,

“())(()” is not balanced.

Your task is to keep a string in a balanced state, under a severe condition in which a cosmic ray

may flip the direction of parentheses.

You are initially given a balanced string. Each time the direction of a single parenthesis is

flipped, your program is notified the position of the changed character in the string. Then,

calculate and output the leftmost position that, if the parenthesis there is flipped, the whole

string gets back to the balanced state. After the string is balanced by changing the parenthesis

indicated by your program, next cosmic ray flips another parenthesis, and the steps are repeated

several times

Input

The input consists of a single test case formatted as follows.

The first line consists of two integers N and Q (2 ≤ N ≤ 300000, 1 ≤ Q ≤ 150000). The second

line is a string s of balanced parentheses with length N. Each of the following Q lines is an

integer qi (1 ≤ qi ≤ N) that indicates that the direction of the qi-th parenthesis is flipped.

Output

For each event qi

, output the position of the leftmost parenthesis you need to flip in order to

get back to the balanced state.

Note that each input flipping event qi

is applied to the string after the previous flip qi−1 and its

fix.

Sample Input

6 3

((()))

4

3

1

Sample Output

2

2

1

Hint

题意

给你一个平衡的括号序列,然后每次询问是让一个括号掉转方向,让你找到一个最左边的,改变方向之后能够使得序列平衡的括号

强制在线(就是每次询问完之后,保持修改

题解:

把(想成1,)想成-1,平衡的显然就是前缀和为0

对于(改成)的,我们就找到最左边的)改成(就好了

对于)改成(的,我们就找到前缀和到结尾的最小值,大于等于2的第一个括号就好了

为什么呢?

因为我们维护的是前缀和,(改成)很显然是要-2的,所以我们只需要找到前缀和到结尾的最小值大于2的就好了,这样修改之后,也保持了平衡

我们用线段树+二分来解决

n(logn+logn)的和nlognlogn 的都比较好想

代码

#include<bits/stdc++.h>
using namespace std; typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType sum , lazy;
SgTreeDataType now;
void updata(SgTreeDataType v)
{
sum += v;
lazy += v;
}
}; treenode tree[300005*4]; void push_down(int o)
{
SgTreeDataType lazyval = tree[o].lazy;
tree[2*o].updata(lazyval) ; tree[2*o+1].updata(lazyval);
tree[o].lazy = 0;
} void push_up(int o)
{
tree[o].sum = min(tree[2*o].sum , tree[2*o+1].sum);
} void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R, tree[o].lazy = 0;
tree[o].now = 1e9;
tree[o].sum = 0;
if (R > L)
{
int mid = (L+R) >> 1;
build_tree(L,mid,o*2);
build_tree(mid+1,R,o*2+1);
}
} void updata_pre(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
push_down(o);
int mid = (L+R)>>1;
if (QL <= mid) updata_pre(QL,QR,v,o*2);
if (QR > mid) updata_pre(QL,QR,v,o*2+1);
push_up(o);
}
} void updata_idx(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR)tree[o].now = v;
else
{
int mid = (L+R)>>1;
if (QL <= mid) updata_idx(QL,QR,v,o*2);
if (QR > mid) updata_idx(QL,QR,v,o*2+1);
tree[o].now = min(tree[o*2].now , tree[o*2+1].now);
}
} SgTreeDataType query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].sum;
else
{
push_down(o);
int mid = (L+R)>>1;
SgTreeDataType res = 1e9;
if (QL <= mid) res = min(res,query(QL,QR,2*o));
if (QR > mid) res = min(res,query(QL,QR,2*o+1));
push_up(o);
return res;
}
}
SgTreeDataType query2(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return tree[o].now;
else
{
int mid = (L+R)>>1;
SgTreeDataType res = 1e9;
if (QL <= mid) res = min(res,query2(QL,QR,2*o));
if (QR > mid) res = min(res,query2(QL,QR,2*o+1));
return res;
}
}
char str[300005];
int sum[300005]; int main()
{
int n,q;
scanf("%d%d",&n,&q);
scanf("%s",str+1);
build_tree(1,n,1);
for(int i=1;i<=n;i++)
{
if(str[i]=='(')
{
updata_pre(i,n,1,1);
updata_idx(i,i,1,1);
}
else
{
updata_pre(i,n,-1,1);
updata_idx(i,i,-1,1);
}
} while(q--)
{
int x;
scanf("%d",&x);
if(str[x]=='(')
{
str[x]=')';
updata_idx(x,x,-1,1);
updata_pre(x,n,-2,1);
int l = 1,r = x;
while(l<=r)
{
int mid = (l+r)/2;
if(query2(1,mid,1)<0)r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
updata_idx(l,l,1,1);
updata_pre(l,n,2,1);
str[l]='(';
}
else if(str[x]==')')
{
str[x]='(';
updata_idx(x,x,1,1);
updata_pre(x,n,2,1);
int l = 1,r = x;
while(l<=r)
{
int mid = (l+r)/2;
if(query(mid,x,1)>=2)r=mid-1;
else l=mid+1;
}
printf("%d\n",l);
updata_idx(l,l,-1,1);
updata_pre(l,n,-2,1);
str[l]=')';
}
//printf("%s\n",str+1);
}
}

Codeforces Gym 100803G Flipping Parentheses 线段树+二分的更多相关文章

  1. Gym 100803G Flipping Parentheses

    题目链接:http://codeforces.com/gym/100803/attachments/download/3816/20142015-acmicpc-asia-tokyo-regional ...

  2. Codeforces GYM 100114 D&period; Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  3. Codeforces Gym 100733J Summer Wars 线段树,区间更新,区间求最大值,离散化,区间求并

    Summer WarsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.a ...

  4. CSU - 1542 Flipping Parentheses &lpar;线段树&rpar;

    CSU - 1542 Flipping Parentheses Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld ...

  5. Codeforces Gym 100231B Intervals 线段树&plus;二分&plus;贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  6. Educational Codeforces Round 64 &lpar;Rated for Div&period; 2&rpar; &lpar;线段树二分&rpar;

    题目:http://codeforces.com/contest/1156/problem/E 题意:给你1-n  n个数,然后求有多少个区间[l,r] 满足    a[l]+a[r]=max([l, ...

  7. Codeforces 1500E - Subset Trick(线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 一道线段树的套路题(似乎 ycx 会做这道题?orzorz!!11) 首先考虑什么样的 \(x\) 是"不合适"的,我 ...

  8. hdu4614 线段树&plus;二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  9. 洛谷P4344 脑洞治疗仪 &lbrack;SHOI2015&rsqb; 线段树&plus;二分答案&sol;分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

随机推荐

  1. 初识App安全性测试

    目前手机App测试还是以发现bug为主,主要测试流程就是服务器接口测试,客户端功能性覆盖,以及自动化配合的性能,适配,压测等,对于App安全性测试貌似没有系统全面统一的标准和流程,其实安全性bug也可 ...

  2. 小谈KVC中KeyPath的集合运算符

    由于知识点比较简单,这里不再陈述一大堆的原理,直入主题. KVC中的集合运算符有以下三类: 1.简单集合运算符:@avg.@sum.@max.@min.@count (只能用在集合对象中,对象属性必须 ...

  3. 设计模式 之 观察者(Observer)模式

    观察者(observer)模式定义了一对多的依赖关系,让多个观察者对象能够同时监听某一主题对象.这个主题对象中的状态发生改变时,就会通知所有的观察者对象. 观察者模式的结构图: 结构中各个部分的含义: ...

  4. Linux网络编程必看书籍推荐

    首先要说讲述计算机网络和TCP/IP的书很多. 先要学习网络知识才谈得上编程 讲述计算机网络的最经典的当属Andrew S.Tanenbaum的<计算机网络>第五版,这本书难易适中. &l ...

  5. werfault进程使用CPU率高

    werfault进程是Windows vista 错误报告进程,是用来向微软反馈报告.是安全的正常进程. 解决方法:1.打开控制面板”—“系统和维护”,点击“问题报告和解决方案”. 2.点击“更改设置 ...

  6. Python之路【第八篇】&colon;Python模块

    阅读目录 一.模块和包 模块(module)的概念: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到 ...

  7. BZOJ 1491&colon; &lbrack;NOI2007&rsqb;社交网络(Floyd&plus;暴力乱搞)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=1491 题解: 先看数据范围,n<=100..欸可以乱搞了 首先因为小学学过的乘法原理 ...

  8. &lbrack;UE4&rsqb;Static Mesh的碰撞体

    一.可以在3D建模的时候添加碰撞体,导入到UE4的时候,碰撞体也会跟着导入进来. 二.也可以在UE4中自行添加碰撞体 三.在UE4中添加编辑碰撞体 四.选择碰撞体可以移动.缩放.旋转碰撞体,如果模型比 ...

  9. 浅谈spring中AOP以及spring中AOP的注解方式

    AOP(Aspect Oriented Programming):AOP的专业术语是"面向切面编程" 什么是面向切面编程,我的理解就是:在不修改源代码的情况下增强功能.好了,下面在 ...

  10. php 按照中文字母名字排序,并把相应的头像显示出来

    //排序public function getFirstChar($s){ $s0 = mb_substr($s,0,3); //获取名字的姓 $s = iconv('UTF-8','gb2312', ...