1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include<bits/stdc++.h> using namespace std; void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; } void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');} #define lf double #define pii pair<int,int > #define vec vector<int > #define pb push_back #define mp make_pair #define fr first #define sc second #define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) const int maxn = 1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7; struct data {int ab,bc,mx,mn,res;}t[maxn]; int tag[maxn],a[maxn],n,q; char s[maxn]; #define ls p<<1 #define rs p<<1|1 #define mid ((l+r)>>1) void update(int p) { t[p].mx=max(t[ls].mx,t[rs].mx); t[p].mn=min(t[ls].mn,t[rs].mn); t[p].ab=max(max(t[ls].ab,t[rs].ab),t[ls].mx-t[rs].mn*2); t[p].bc=max(max(t[ls].bc,t[rs].bc),t[rs].mx-t[ls].mn*2); t[p].res=max(max(t[ls].res,t[rs].res),max(t[ls].mx+t[rs].bc,t[ls].ab+t[rs].mx)); } void push(int p,int x) { t[p].mx+=x,t[p].mn+=x; t[p].ab-=x,t[p].bc-=x;tag[p]+=x; } void pushdown(int p) { if(tag[p]) push(ls,tag[p]),push(rs,tag[p]),tag[p]=0; } void modify(int p,int l,int r,int x,int y,int v) { if(x<=l&&r<=y) return push(p,v),void(); pushdown(p); if(x<=mid) modify(ls,l,mid,x,y,v); if(y>mid) modify(rs,mid+1,r,x,y,v); update(p); } void build(int p,int l,int r) { if(l==r) return t[p].mn=t[p].mx=a[l],t[p].ab=t[p].bc=-a[l],void(); build(ls,l,mid),build(rs,mid+1,r);update(p); } void debug(int p,int l,int r) { if(l==r) { printf("%d ",t[p].mn); }else pushdown(p),debug(ls,l,mid),debug(rs,mid+1,r); } int main() { read(n),read(q);n=(n-1)*2; scanf("%s",s+1); for(int i=1;i<=n;i++) a[i]=a[i-1]+(s[i]=='('?1:-1); build(1,1,n);write(t[1].res); for(int i=1;i<=q;i++) { int a,b;read(a),read(b); if(s[a]!=s[b]) { modify(1,1,n,a,n,s[a]=='('?-2:2); modify(1,1,n,b,n,s[b]=='('?-2:2);swap(s[a],s[b]); }write(t[1].res); } return 0; }
|