2209: [Jsoi2011]括号序列
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 894 Solved: 423
[ Submit][ Status]
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
)(())(
0 1 6
0 1 4
0 3 4
Sample Output
2
2
0
2
0
HINT
100%的数据满足N,Q不超过10^5。
思路:对于一段括号匹配结束之后肯定会成为))))((((,或者只剩一种情况,如果最后剩下的未匹配的左括号有x个,右括号有y个,那么答案就是ans=(x+1)/2+(y+1)/2,也就是将右括号的前半部分变成左括号,左括号的后半部分变成右括号,当两者是奇数时都需要修改。
所以伸展树维护四个值,la表示左边连续最小值(也就是从左边连续匹配剩下的有括号的数量),lb表示左边连续最大值,ra,rb类似
那么答案就是ans=(abs(la)+1)/2+(rb+1)/2=(-la+1)/2+(rb+1)/2;
注意,这两种形式看起来是等价的,但是后一种才是真正正确的,即包含了所有情况。因为在这里我们会觉得在大多数时候LMin是个负数。但是有时候可能是正的,比如剩余括号序列为((((,此时la=1,rb=4,答案为2,即将后两个修改为右括号。
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define Key_value ch[ch[root][1]][0]
const int maxn=100010;
int ch[maxn][2],pre[maxn],rev[maxn],over[maxn],size[maxn],key[maxn];
int la[maxn],lb[maxn],sum[maxn],ra[maxn],rb[maxn];
int root,tot1;
int N,M;
char str[maxn];
void NewNode(int &r,int f,int val)
{
r=++tot1;
pre[r]=f;
ch[r][0]=ch[r][1]=0;
size[r]=1;
key[r]=la[r]=lb[r]=ra[r]=rb[r]=val;
rev[r]=over[r]=0;
sum[r]=val;
}
void pushup(int r)
{
if(!r)return;
int lson=ch[r][0],rson=ch[r][1];
size[r]=size[lson]+size[rson]+1;
la[r]=min(la[lson],sum[lson]+min(key[r],key[r]+la[rson]));
lb[r]=max(lb[lson],sum[lson]+max(key[r],key[r]+lb[rson]));
ra[r]=min(ra[rson],sum[rson]+min(key[r],key[r]+ra[lson]));
rb[r]=max(rb[rson],sum[rson]+max(key[r],key[r]+rb[lson]));
sum[r]=sum[lson]+sum[rson]+key[r];
}
void update_rev(int r)
{
if(!r)return;
swap(ch[r][0],ch[r][1]);
swap(la[r],ra[r]);
swap(lb[r],rb[r]);
rev[r]^=1;
}
void update_over(int r)
{
if(!r)return;
key[r]=-key[r];
sum[r]=-sum[r];
swap(la[r],lb[r]);
la[r]=-la[r],lb[r]=-lb[r];
swap(ra[r],rb[r]);
ra[r]=-ra[r],rb[r]=-rb[r];
over[r]^=1;
}
void pushdown(int r)
{
if(rev[r])
{
update_rev(ch[r][0]);
update_rev(ch[r][1]);
rev[r]=0;
}
if(over[r])
{
update_over(ch[r][0]);
update_over(ch[r][1]);
over[r]=0;
}
}
void build(int &x,int l,int r,int f)
{
if(l>r)return;
int mid=(l+r)>>1;
NewNode(x,f,str[mid]=='('?1:-1);
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
pushup(x);
}
void init()
{
root=tot1=0;
ch[root][0]=ch[root][1]=size[root]=rev[root]=over[root]=pre[root]=0;
la[root]=lb[root]=ra[root]=rb[root]=0;
NewNode(root,0,0);
NewNode(ch[root][1],root,0);
build(Key_value,0,strlen(str)-1,ch[root][1]);
pushup(ch[root][1]);
pushup(root);
}
void Rotate(int x,int kind)
{
int y=pre[x];
pushdown(y);
pushdown(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
}
void Splay(int r,int goal)
{
pushdown(r);
while(pre[r]!=goal)
{
if(pre[pre[r]]==goal)
{
pushdown(pre[r]);
pushdown(r);
Rotate(r,ch[pre[r]][0]==r);
}
else
{
pushdown(pre[pre[r]]);
pushdown(pre[r]);
pushdown(r);
int y=pre[r];
int kind=(ch[pre[y]][0]==y);
if(ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
pushup(r);
if(goal==0)root=r;
}
int get_kth(int r,int k)
{
pushdown(r);
int t=size[ch[r][0]]+1;
if(t==k)return r;
if(t>k)return get_kth(ch[r][0],k);
return get_kth(ch[r][1],k-t);
}
void debug(int r)
{
if(!r)return;
debug(ch[r][0]);
printf("%c",key[r]==1?'(':')');
debug(ch[r][1]);
}
void Reverse(int l,int r)
{
Splay(get_kth(root,l),0);
Splay(get_kth(root,r+2),root);
update_rev(Key_value);
pushup(ch[root][1]);
pushup(root);
}
void Over(int l,int r)
{
Splay(get_kth(root,l),0);
Splay(get_kth(root,r+2),root);
update_over(Key_value);
pushup(ch[root][1]);
pushup(root);
}
void Query(int l,int r)
{
Splay(get_kth(root,l),0);
Splay(get_kth(root,r+2),root);
int ans=(-la[Key_value]+1)/2+(rb[Key_value]+1)/2;
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
scanf("%s",str);
init();
int op,l,r;
while(M--)
{
scanf("%d%d%d",&op,&l,&r);
//cout<<op<<endl;
if(op==0)Query(l,r);
else if(op==1)Over(l,r);
else Reverse(l,r);
}
}
return 0;
}