I

Title Description

We define a sequence of brackets is happy, if and only if the number of the number of right parenthesis it does not exceed the opening parenthesis.

Now you want to maintain a length $ n $ bracketed sequence, numbered $ 1-n $, supports the following three operations:

1 $ p $ denote the first $ p $ parentheses reverse, both $ ($ becomes $) $, $) $ becomes $ ($.

2 $ l $ $ r $ presents to ask: if you want each prefix of L $ $ $ R & lt parentheses to the first $ sequence consisting parentheses are happy, the minimum number necessary to remove the brackets in position.

3 $ l $ $ r $ presents to ask: if For each prefix of L $ $ $ R & lt parentheses to the first $ parentheses each consisting of a sequence of suffixes are happy, at least by deleting the number of positions the brackets.

data range

$ N \ 2 \ Times 10 ^ 5, q \ 3 \ Times 10 ^ 5 $

answer

Consider $ [l, r] $ sequence brackets pulled out and make a prefix, and then represented as a function of image

If it is 2, then the operation, apparently $ max (-min, 0) $

For the third operation, consider the greedy, the prefix inflicted first method, the image will be shifted at this time, each point $ I $ x_i-min finally becomes $ (x_j
) (J <I) $

To make the suffix becomes legal, the right point to the highest point of the image, so the max after a prefix to make legitimate right down to the point after the legal

So poorly maintained can use segment tree

Code

#include <bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,a[N],q;char ch;
struct T{int in,ax,s,df;}t[N<<2],V;
#define Ls k<<1
#define Rs k<<1|1
#define mid ((l+r)>>1)
T hb(T x,T y){
    V.in=min(x.in,y.in+x.s);
    V.ax=max(x.ax,y.ax+x.s);
    V.df=max(max(x.df,y.df),y.ax+x.s-x.in);
    V.s=x.s+y.s; return V;
}
void build(int k,int l,int r){
    if (l==r){
        if (~a[l]) t[k]=(T){0,1,1,1};
        else t[k]=(T){-1,0,-1,0};
        return;
    }
    build(Ls,l,mid);build(Rs,mid+1,r);
    t[k]=hb(t[Ls],t[Rs]);
}
void upd(int k,int l,int r,int x){
    if (l==r){
        if (~a[l]) t[k]=(T){0,1,1,1};
        else t[k]=(T){-1,0,-1,0};
        return;
    }
    if (mid>=x) upd(Ls,l,mid,x);
    else upd(Rs,mid+1,r,x);
    t[k]=hb(t[Ls],t[Rs]);
}
T qry(int k,int l,int r,int L,int R){
    if (L<=l && r<=R) return t[k];
    if (mid>=R) return qry(Ls,l,mid,L,R);
    if (mid<L) return qry(Rs,mid+1,r,L,R);
    return hb(qry(Ls,l,mid,L,R),qry(Rs,mid+1,r,L,R));
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf(" %c",&ch),
        a[i]=((ch=='(')?1:-1);
    build(1,1,n);scanf("%d",&q);
    for (int op,x,y;q--;){
        scanf("%d%d",&op,&x);
        if (op<2) a[x]=-a[x],upd(1,1,n,x);
        else if (op<3){
            scanf("%d",&y);
            T v=qry(1,1,n,x,y);
            printf("%d\n",max(0,-v.in));
        }
        else{
            scanf("%d",&y);
            T v=qry(1,1,n,x,y);
            printf("%d\n",max(0,-v.in)+v.df-(v.s-v.in));
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xjqxjq/p/11766818.html
I
"I"
I: