您的位置:首页 > 其它

【BZOJ】2329: [HNOI2011]括号修复(splay+特殊的技巧)

2014-12-07 11:06 399 查看
http://www.lydsy.com/JudgeOnline/problem.php?id=2329

和前一题一样,不就多了个replace操作吗。好,就打一下。

然后交上去wa了。。。。。。。。。。。。。。。。。。。。

看了题解,好神奇!

记住:以后pushdown的tag要考虑先后顺序!

因为invert和swap操作谁先谁后没有关系,那么考虑invert和replace这两个有冲突的关系

为什么有冲突呢?因为假如你replace的标记在先,invert标记在后,但是invert在pushdown里先执行QAQ,比如序列(()),本来replace了[2, 3] (,变成 (((),然后invert[2, 3], 应该变成 ()))。

可是!你pushdown里执行的是invert先,即本来(()),然后变成了()(),然后再replace,变成((()啦!!!所以错了!!!

解决方法就是,在执行invert后,改变replace的tag。

即在变成()()后,replace[2, 3] ( 变成 replace[2, 3] ),那么就是()))。!!!

#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, fix;
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; fix=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;
if(fix) fix=-fix;
tag=!tag; int t;
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 upd3(int _k) {
if(this==null) return;
fix=_k;
k=_k;
sum=s*k;
lmin=rmin=k<0?sum:k;
lmax=rmax=k<0?k:sum;
}
void pushdown() {
if(tag) {
c[0]->upd1();
c[1]->upd1();
tag=0;
}
if(rev) {
c[0]->upd2();
c[1]->upd2();
rev=0;
}
if(fix) {
c[0]->upd3(fix);
c[1]->upd3(fix);
fix=0;
}
}
}*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;
x->pushdown();
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 fix3() {
int l=getint(), r=getint();
node *x=getrange(l, r);
char c=getchar(); while(c!=')'&&c!='(') c=getchar();
int fix=c==')'?-1:1;
x->pushdown();
x->upd3(fix);
}
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
;
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();
}

char o[10];
int main() {
init();
while(m--) {
scanf("%s", o);
if(o[0]=='Q') ask();
else if(o[0]=='I') fix1();
else if(o[0]=='S') fix2();
else fix3();
//P();
}
return 0;
}


  

Description



Input

Output

Sample Input

Sample Output

HINT

Source

Splay
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: