其他操作只需标记即可

但对于Query操作,非法序列一定是)))(这种形式,所以(=1,)=-1维护前缀和即可(即视感,然而我还是不会QAQ)

调了大半天。。药丸

  1 #include<bits/stdc++.h>
  2 #define inc(i,l,r) for(int i=l;i<=r;i++)
  3 #define dec(i,l,r) for(int i=l;i>=r;i--)
  4 #define link(x) for(edge *j=h[x];j;j=j->next)
  5 #define mem(a) memset(a,0,sizeof(a))
  6 #define inf 1e9
  7 #define succ(x) (1<<x)
  8 #define NM 100000+5
  9 using namespace std;
 10 int read(){
 11     int x=0,f=1;char ch=getchar();
 12     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
 13     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
 14     return x*f;
 15 }
 16 int c[NM][2],ll[NM],lr[NM],rr[NM],rl[NM],s[NM],b[NM],f[NM],cg[NM],tag[NM],z[NM],a[NM],tmp[NM];
 17 int n,_x,_y,_t,root,tot,t1,t2,m;
 18 char st[NM];
 19 void pushdown(int x){
 20     if(x==0)return;
 21     if(tag[x]){
 22         swap(ll[x],rl[x]);swap(rr[x],lr[x]);
 23         swap(c[x][0],c[x][1]);
 24         tag[c[x][0]]^=1;tag[c[x][1]]^=1;
 25         tag[x]=0;
 26     }
 27     if(cg[x]){
 28         s[x]=cg[x]*b[x];
 29         ll[x]=rl[x]=max(0,cg[x]*b[x]);
 30         lr[x]=rr[x]=min(0,cg[x]*b[x]);
 31         a[x]=cg[x];
 32         cg[c[x][0]]=cg[c[x][1]]=cg[x];
 33         cg[x]=z[x]=z[c[x][0]]=z[c[x][1]]=0;
 34     }
 35     if(z[x]){
 36         swap(ll[x],lr[x]);ll[x]=-ll[x];lr[x]=-lr[x];
 37         swap(rr[x],rl[x]);rl[x]=-rl[x];rr[x]=-rr[x];
 38         a[x]=-a[x];s[x]=-s[x];
 39         if(cg[c[x][0]])cg[c[x][0]]*=-1;else z[c[x][0]]^=1;
 40         if(cg[c[x][1]])cg[c[x][1]]*=-1;else z[c[x][1]]^=1;
 41         z[x]=0;
 42     }
 43 }
 44 void up(int i){
 45     a[0]=b[0]=ll[0]=lr[0]=rr[0]=rl[0]=s[0]=0;
 46     int x=c[i][0],y=c[i][1];
 47     pushdown(x);pushdown(y);
 48     b[i]=b[x]+b[y]+1;
 49     s[i]=s[x]+a[i]+s[y];
 50     ll[i]=max(ll[x],s[x]+a[i]+ll[y]);
 51     lr[i]=min(lr[x],s[x]+a[i]+lr[y]);
 52     rl[i]=max(rl[y],s[y]+a[i]+rl[x]);
 53     rr[i]=min(rr[y],s[y]+a[i]+rr[x]);
 54 }
 55 void newnode(int r,int fa,int k){
 56     f[r]=fa;a[r]=k;b[r]=1;s[r]=k;
 57     ll[r]=rl[r]=max(a[r],0);
 58     lr[r]=rr[r]=min(a[r],0);
 59 }
 60 int build(int fa,int x,int y){
 61     int t=x+y>>1;
 62     if(x>y)return 0;
 63     c[t][0]=build(t,x,t-1);
 64     newnode(t,fa,tmp[t]);
 65     c[t][1]=build(t,t+1,y);
 66     up(t);
 67     return t;
 68 }
 69 int _d(int x){
 70     return c[f[x]][1]==x;
 71 }
 72 void rot(int x){
 73     int y=f[x],kind=!_d(x);
 74     pushdown(y);pushdown(x);
 75     c[y][!kind]=c[x][kind];f[c[x][kind]]=y;
 76     c[f[y]][_d(y)]=x;f[x]=f[y];
 77     c[x][kind]=y;f[y]=x;
 78     up(y);up(x);
 79 }
 80 void splay(int x,int goal=0){
 81     for(int y=f[x];y!=goal;y=f[x]){
 82         if(f[y]!=goal)
 83             _d(x)==_d(y)?rot(y):rot(x);
 84         rot(x);
 85     }
 86     if(goal==0)root=x;
 87 }
 88 int find(int x){
 89     int r=root;
 90     while(x){
 91         pushdown(r);
 92         if(b[c[r][0]]>=x)r=c[r][0];
 93         else{
 94             x-=b[c[r][0]]+1;
 95             if(!x)return r;
 96             r=c[r][1];
 97         }
 98     }    
 99 }
100 int pick(int x,int y){
101     y+=2;x=find(x);y=find(y);
102     splay(x);splay(y,x);
103     return c[y][0];
104 }
105 void ch(int x,int y){
106     int r=pick(x,y);
107     cg[r]=_t;tag[r]=0;z[r]=0;
108     up(y);up(x);
109 }
110 void down(int x,int y){
111     int r=pick(x,y);
112     tag[r]^=t1;
113     if(cg[r])cg[r]-=cg[r];else z[r]^=t2;
114     up(f[r]);up(f[f[r]]);
115 }
116 void query(int x,int y){
117     int r=pick(x,y);
118     printf("%d\n",(-lr[r]+1)/2+(rl[r]+1)/2);
119 }
120 void out(int x){
121     pushdown(x);
122     if(c[x][0])out(c[x][0]);
123     printf("%d ",x);
124 //    printf("%d %d %d %d %d %d\n",x,ll[x],lr[x],rl[x],rr[x],s[x]);
125     if(c[x][1])out(c[x][1]);
126 }
127 int main(){
128 //    freopen("data.in","r",stdin);
129     n=read();m=read();
130     scanf("%s\n",st+2);
131     inc(i,2,n+1)tmp[i]=st[i]=='('?1:-1;
132     build(0,1,n+2);root=(n+3)/2;
133 //    out(root);printf("\n");
134     while(m--){
135         scanf("%s",st);
136         _x=read();_y=read();
137         if(st[0]=='R'){
138             scanf("%s",st);
139             _t=st[0]=='('?1:-1;
140             ch(_x,_y);
141         }else if(st[0]=='Q')query(_x,_y);
142         else{
143             t1=0,t2=0;
144             if(st[0]=='S')t1=1;else t2=1;
145             down(_x,_y);
146         }
147 //        out(root);printf("\n");
148     }
149     return 0;
150 }
View Code

 

posted on 2016-01-09 17:40  onlyRP  阅读(149)  评论(0编辑  收藏  举报