http://www.lydsy.com/JudgeOnline/problem.php?id=2209

splay又犯逗。。。。。。。。upd1那里的sum忘记赋值反。。。。。。。。。。。。。

本题好神。。首先发现将所有能匹配的消掉后一定是类似这样的))))((((,当然也有((((((这种情况

还有本题将查询的区间长度都看做偶数。。。。。

如果能知道有多少个)和多少个(,那么答案就是

)的个数除以2取上界+(的个数除以2取上界

很简单吧。。。。。。

所以我们只需要在splay维护从左边剩余的)最多有多少个,右边(最多有多少个即可。

但是由于操作2和操作3的存在,我们还需要维护左边剩余的(最多有多少个,右边)最多有多少个,这样就能支持了。。

然后请自己yy

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } const int oo=~0u>>1, N=1e5+10;
struct node *null;
struct node {
node *f, *c[2];
int s, k, lmin, lmax, rmin, rmax, sum;
bool tag, rev;
node(int _k=0) { s=1; lmin=lmax=rmin=rmax=k=sum=_k; f=c[0]=c[1]=null; tag=rev=0; }
void setc(node *x, bool d) { c[d]=x; x->f=this; }
bool d() { return f->c[1]==this; }
void pushup() {
s=c[0]->s+c[1]->s+1;
sum=c[0]->sum+c[1]->sum+k;
lmin=c[0]->lmin;
lmax=c[0]->lmax;
rmin=c[1]->rmin;
rmax=c[1]->rmax;
int mnl=min(0, c[1]->lmin), mnr=min(0, c[0]->rmin);
int mxl=max(0, c[1]->lmax), mxr=max(0, c[0]->rmax);
lmin=min(lmin, c[0]->sum+k+mnl);
lmax=max(lmax, c[0]->sum+k+mxl);
rmin=min(rmin, c[1]->sum+k+mnr);
rmax=max(rmax, c[1]->sum+k+mxr);
}
void upd1() {
if(this==null) return;
tag=!tag;
int t=lmin;
lmin=-lmax;
lmax=-t;
t=rmin;
rmin=-rmax;
rmax=-t;
k=-k;
sum=-sum;
}
void upd2() {
if(this==null) return;
rev=!rev;
swap(c[0], c[1]);
swap(lmin, rmin);
swap(lmax, rmax);
}
void pushdown() {
if(tag) {
tag=0;
c[0]->upd1();
c[1]->upd1();
}
if(rev) {
rev=0;
c[0]->upd2();
c[1]->upd2();
}
}
}*root;
void PN(node *x) {
printf("key:%c \t lmin:%d \t lmax:%d \t rmin:%d \t rmax:%d \t sum:%d \t \n", x->k==1?'(':(x->k==-1?')':'N'), x->lmin, x->lmax, x->rmin, x->rmax, x->sum);
}
void Pr(node *x) {
if(x==null) return;
Pr(x->c[0]);
if(x->k) printf("%c", x->k==1?'(':')');
Pr(x->c[1]);
}
void P(node *x=root) {
Pr(x); puts("");
}
void rot(node *x) {
node *f=x->f;
f->pushdown(); x->pushdown(); bool d=x->d();
f->f->setc(x, f->d());
f->setc(x->c[!d], d);
x->setc(f, !d);
f->pushup();
if(f==root) root=x;
}
void splay(node *x, node *f=null) {
x->pushdown();
while(x->f!=f)
if(x->f->f==f) rot(x);
else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x));
x->pushup();
}
node *sel(node *x, int k) {
if(x==null) return x;
x->pushdown();
int s=x->c[0]->s;
if(s==k) return x;
if(s<k) return sel(x->c[1], k-s-1);
return sel(x->c[0], k);
}
node *getrange(int l, int r) {
splay(sel(root, l-1));
splay(sel(root, r+1), root);
return root->c[1]->c[0];
}
void fix1() {
int l=getint(), r=getint();
node *x=getrange(l, r);
x->pushdown();
x->upd1();
}
void fix2() {
int l=getint(), r=getint();
node *x=getrange(l, r);
x->pushdown();
x->upd2();
}
void ask() {
int l=getint(), r=getint();
node *x=getrange(l, r);
l=(-x->lmin+1)>>1;
r=(x->rmax+1)>>1;
printf("%d\n", l+r);
} char s[N];
int n, m, q;
void build(int l, int r, node *&x) {
if(l>r) return;
int mid=(l+r)>>1;
x=new node(s[mid]==')'?-1:1);
if(l==r) return;
build(l, mid-1, x->c[0]);
build(mid+1, r, x->c[1]);
if(l<=mid-1) x->c[0]->f=x;
if(mid+1<=r) x->c[1]->f=x;
x->pushup();
}
void in(node *x) {
x->lmax=x->rmax=-oo;
x->lmin=x->rmin=oo;
x->sum=0;
x->k=0;
}
void init() {
null=new node(); null->s=0; in(null);
null->f=null->c[0]=null->c[1]=null;
root=new node(); in(root);
root->setc(new node(), 1); in(root->c[1]);
node *x;
read(n); read(m);
scanf("%s", s+1);
build(1, n, x);
root->c[1]->setc(x, 0);
root->c[1]->pushup();
root->pushup();
} int main() {
init();
while(m--) {
int c=getint();
if(c==0) ask();
else if(c==1) fix1();
else if(c==2) fix2();
}
return 0;
}

  


Description

Input

输入数据的第一行包含两个整数N和Q,分别表示括号序列的长度,以及操作的个数。 第二行包含一个长度为N的括号序列。 接下来Q行,每行三个整数t、x和y,分别表示操作的类型、操作的开始位置和操作的结 束位置,输入数据保证x不小于y。其中t=0表示询问操作、t=1表示反转操作、t=2表示翻转操 作。

Output

对于每一个询问操作,输出一行,表示将括号序列的该子序列修改为配对,所需的最少改动 个数。

Sample Input

6 3
)(())(
0 1 6
0 1 4
0 3 4

Sample Output

2
2
0

HINT

100%的数据满足N,Q不超过10^5

Source

【BZOJ】2209: [Jsoi2011]括号序列(splay)的更多相关文章

  1. bzoj 2209: [Jsoi2011]括号序列 splay

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 833  Solved: 392[Submit][Status ...

  2. BZOJ 2209: [Jsoi2011]括号序列 [splay 括号]

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1111  Solved: 541[Submit][Statu ...

  3. bzoj 2209 [Jsoi2011]括号序列 平衡树

    2209: [Jsoi2011]括号序列 Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 1404  Solved: 699[Submit][Statu ...

  4. ●BZOJ 2209 [Jsoi2011]括号序列

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2209 题解: Splay 很好的题,但是把智障的我给恶心到了...   首先不难发现,最后没 ...

  5. 【BZOJ2329/2209】[HNOI2011]括号修复/[Jsoi2011]括号序列 Splay

    [BZOJ2329/2209][HNOI2011]括号修复/[Jsoi2011]括号序列 题解:我们的Splay每个节点维护如下东西:左边有多少多余的右括号,右边有多少多余的左括号,同时为了反转操作, ...

  6. BZOJ2209 [Jsoi2011]括号序列 splay

    原文链接http://www.cnblogs.com/zhouzhendong/p/8093556.html 题目传送门 - BZOJ2209 题解 我太弱了,调出这题感觉都要吐了. 题解懒得写了. ...

  7. 【BZOJ-2329&2209】括号修复&括号序列 Splay

    2329: [HNOI2011]括号修复 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 1007  Solved: 476[Submit][Statu ...

  8. BZOJ 2329/2209 [HNOI2011]括号修复 (splay)

    题目大意: 让你维护一个括号序列,支持 1.区间修改为同一种括号 2.区间内所有括号都反转 3.翻转整个区间,括号的方向不变 4.查询把某段区间变为合法的括号序列,至少需要修改多少次括号 给跪了,足足 ...

  9. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

随机推荐

  1. Linux 千万不要执行的10个命令

    1. rm -rf 命令 rm -rf命令是删除文件夹及其内容最快的方式之一.仅仅一丁点的敲错或无知都可能导致不可恢复的系统崩坏.下列是一些rm 命令的选项. rm 命令在Linux下通常用来删除文件 ...

  2. 原创:分享asp.net伪静态成目录形式iis如何设置

    服务器租用详解asp.net伪静态成目录形式iis如何设置: 一.首先介绍一下asp.net伪静态成html后缀iis如何设置的 iis6 伪静态 iis配置方法 图解 1.右键点击 要设置网站的网站 ...

  3. Java for LeetCode 189 Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  4. mybatis整合spring 之 基于接口映射的多对一关系

    转载自:http://my.oschina.net/huangcongmin12/blog/83731 mybatis整合spring 之  基于接口映射的多对一关系. 项目用到俩个表,即studen ...

  5. icon font字体图标字库汇总

    最近在研究icon font图标字库,找了一些比较好的在线字库.大都是开源的,而且各有特色! 阿里icon font字库 http://www.iconfont.cn/ 这个是阿里妈妈M2UX的一个i ...

  6. Google map测量工具

    启用Google map的测量工具,测量图上的距离. 打开Google map,左下角看到Google 地图实验室,点开,然后enable相应功能即可. 通过Google map lab还能使我们能够 ...

  7. Step deep into GLSL

    1 Lighting computation is handled in eye space(需要根据眼睛的位置来计算镜面发射值有多少进入眼睛), hence, when using GLSL (GP ...

  8. pycharm上运行django服务器端、以及创建app方法

     快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 安装Django  下载Django包,解压缩. CMD 进入解压路径下. 执行:python setup.py in ...

  9. C# 如何判断系统是否是静音

    推荐的方法,使用CoreAudioApi.dll,仅在win7上测试过: private MMDevice defaultDevice = null; //判断当前系统扬声器状态 private bo ...

  10. Java Hour 13 集合基础

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...