Bracket Sequence
Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

There is a sequence of brackets, which supports two kinds of operations.
1. we can choose a interval [l,r], and set all the elements range in this interval to left bracket or right bracket. 
2. we can reverse a interval, which means that for all the elements range in [l,r], if it's left bracket at that time, we change it into right bracket, vice versa.
Fish is fond of "Regular Bracket Sequence", so he want to know whether a interval [l,r] of the sequence is regular or not after doing some opearations.

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence. 
2. If S is a regular sequence, then (S) is also a regular sequences. 
3. If A and B are regular sequences, then AB is a regular sequence.

Input

In the first line there is an integer T (T≤10), indicates the number of test cases. Each case begins with a line containing an integers N (N ≤ 100,000 and N is a even number), the size of the initial brackets sequence. The next line contains a string whose length is N consisting of '(' and ')'. In the third of each test case, there is an integer M(M ≤ 100,000) indicates the number of queries. Each of the following M lines contains one operation as mentioned below. The index of the bracket sequence is labeled from 0 to N - 1.

Three operation description:
set l r c: change all the elements range in [l,r] into '(' or ')'.(c is '(' or ')')
reverse l r: reverse the interval [l,r]
query l,r: you should answer that interval [l,r] is regular or not

Output

For each test case, print a line containing the test case number (beginning with 1) on its own line, then the answer for each "query" operation, if the interval is regular, print "YES", otherwise print "NO", one on each line.
Print a blank line after each test case.

Sample Input

1
6
((()))
8
query 0 5
set 0 5 (
query 0 5
reverse 3 5
query 0 5
query 1 4
query 2 3
query 0 4

Sample Output

Case 1:
YES
NO
YES
YES
YES
NO

Hint

Huge input, use "scanf" instead of "cin".

Source

Classic Problem
 
线段树,把左右括号标记成-1,1。。。合法的区间的总和为零,且从左向右的累加和 小于等于 0
维护每个结点的sum max,为方便reserv再多维护一个min reserv时交换max,min的绝对值再成-1
set与reserv并存时,要先reserv再set
 
 #include <iostream>
#include <cstring>
#include <cstdio> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; const int maxn=; int setv[maxn<<],rev[maxn<<],sum[maxn<<],mx[maxn<<],mi[maxn<<];
char str[maxn]; void reserve(int rt)
{
sum[rt]=-sum[rt];
swap(mx[rt],mi[rt]);
mx[rt]=-mx[rt];
mi[rt]=-mi[rt];
rev[rt]^=;
} void set(int op,int rt,int len)
{
sum[rt]=op*len;
mx[rt]=sum[rt]>?sum[rt]:;
mi[rt]=sum[rt]<?sum[rt]:;
setv[rt]=op;rev[rt]=;
} void push_down(int rt,int ll,int lr)
{
if(rev[rt])
{
if(setv[rt]) setv[rt]*=-;
else reserve(rt<<),reserve(rt<<|);
rev[rt]=;
}
if(setv[rt])
{
set(setv[rt],rt<<,ll),set(setv[rt],rt<<|,lr);
setv[rt]=;
}
} void push_up(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
mx[rt]=max(mx[rt<<],sum[rt<<]+mx[rt<<|]);
mi[rt]=min(mi[rt<<],sum[rt<<]+mi[rt<<|]);
} void build(int l,int r,int rt)
{
setv[rt]=rev[rt]=;
if(l==r)
{
sum[rt]=(str[l]=='(')?-:;
mx[rt]=(sum[rt]<)?:;
mi[rt]=(sum[rt]<)?-:;
return;
}
int m=(l+r)>>;
build(lson),build(rson);
push_up(rt);
} void update(int L,int R,int c,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
if(c) set(c,rt,r-l+);
else reserve(rt);
return ;
}
int m=(l+r)>>;
push_down(rt,m-l+,r-m);
if(L<=m) update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
push_up(rt);
} int query_sum(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>,ans=;
push_down(rt,m-l+,r-m);
if(L<=m) ans+=query_sum(L,R,lson);
if(R>m) ans+=query_sum(L,R,rson);
push_up(rt);
return ans;
} int query_max(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return mx[rt];
}
int m=(l+r)>>,ret;
push_down(rt,m-l+,r-m);
if(R<=m) ret=query_max(L,R,lson);
else if(L>m) ret=query_max(L,R,rson);
else ret=max(query_max(L,R,lson),query_sum(L,R,lson)+query_max(L,R,rson));
push_up(rt);
return ret;
} int main()
{
int t,cas=;
char cmd[];
scanf("%d",&t);
while(t--)
{
int n,m;
printf("Case %d:\n",cas++);
scanf("%d",&n);
scanf("%s",str);
build(,n-,);
scanf("%d",&m);
while(m--)
{
scanf("%s",cmd);
if(cmd[]=='s')
{
int a,b; char c[];
scanf("%d%d%s",&a,&b,c);
if(c[]=='(')
update(a,b,-,,n-,);
else if(c[]==')')
update(a,b,,,n-,);
}
else if(cmd[]=='r')
{
int a,b;
scanf("%d%d",&a,&b);
update(a,b,,,n-,);
}
else if(cmd[]=='q')
{
int a,b;
scanf("%d%d",&a,&b);
if(!query_sum(a,b,,n-,)&&!query_max(a,b,,n-,)) puts("YES");
else puts("NO");
}
}
putchar();
}
return ;
}

UESTC 1546 Bracket Sequence的更多相关文章

  1. (中等) UESTC 94 Bracket Sequence,线段树+括号。

    There is a sequence of brackets, which supports two kinds of operations. we can choose a interval [l ...

  2. CF#138 div 1 A. Bracket Sequence

    [#138 div 1 A. Bracket Sequence] [原题] A. Bracket Sequence time limit per test 2 seconds memory limit ...

  3. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

  4. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  5. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  6. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

  7. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  8. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  9. CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈

    C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...

随机推荐

  1. 通过XShell链接虚拟机的CentOS

    今天在Win7环境通过XShell链接VirtualBox的CentOS;始终链接不上,原来是因为虚拟机选择网络链接方式不对[推荐连接方式:Host-only Adapter(主机模式). 知识提要: ...

  2. pcl计算样点法向并显示

    利用最小二乘法估计样点表面法向,并显示 #include <pcl/point_types.h> #include <pcl/io/pcd_io.h> #include < ...

  3. Linux 信号量详解一

    信号量主要用于进程间(不是线程)的互斥,通过sem_p()函数加锁使用资源,sem_v函数解锁释放资源,在加锁期间,CPU从硬件级别关闭中断,防止pv操作被打断. semget函数 int semge ...

  4. codevs 1245 最小的N个和

    1245 最小的N个和 http://codevs.cn/problem/1245/ 题目描述 Description 有两个长度为 N 的序列 A 和 B,在 A 和 B 中各任取一个数可以得到 N ...

  5. Android开发之带你轻松集成友盟统计

    友盟统计是什么呢?为什么要集成他呢? 当我们需要获取自己写的软件的装机量和用户使用信息时,这时我们可以集成友盟统计. 首先到友盟统计中注册账号什么的就不废话了,直接看创建项目: 在个人中心中的管理里面 ...

  6. Alpha阶段总结

    Alpha阶段的验收已经完成,8个小组都展现了他们经过连夜奋战后的成果.相比过往几届,这是第一次8个小组全部顺利演示操作完成,没有个别小组因为任务未完成而延宕演示的情况发生.Alpha演示,各组都实现 ...

  7. Java读取xml配置文件

    package test.com; import java.io.FileInputStream; import javax.xml.parsers.DocumentBuilder;import ja ...

  8. 滤镜 filter:gray 变灰色

    .gray { -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); ...

  9. Android 解析聊天表情的笔记

    应用需要用到聊天功能,考虑到开始需求不大,暂时先用第三方的. 一研究发现界面风格有点不符合整体的风格,加上需要一些自己的特定的需求和界面显示,于是就决定调用第三方数据接口,界面自己写.功能只需要文字, ...

  10. 从点云到网格(二)VRIP介绍

    VRIP(Volumetric Range Image Processing),顾名思义,是从深度图重建网格的一种方法.VRIP是Brian Curless和Marc Levoy在1996年提出来的方 ...