BZOJ2209 [Jsoi2011] bracket sequence splay

Source: Internet
Author: User

Original link http://www.cnblogs.com/zhouzhendong/p/8093556.html

Topic Portal-BZOJ2209 Solving

I was too weak to pull up the problem and feel nauseous.

The puzzle is too lazy to write.

to a link:

http://blog.csdn.net/lych_cys/article/details/50700277

Code
#include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath >using namespace Std;const int N=100005;int fa[n],son[n][2],rev1[n],rev2[n],root;int Lmin[n],lmax[n],rmin[n],rmax [N],sum[n],val[n],size[n];void pushup (int x) {int ls=son[x][0],rs=son[x][1];sum[x]=sum[ls]+val[x]+sum[rs];size[x]= size[ls]+size[rs]+1; Lmin[x]=min (Lmin[ls],sum[ls]+val[x]+lmin[rs]); Lmax[x]=max (Lmax[ls],sum[ls]+val[x]+lmax[rs]); Rmin[x]=min (Rmin[rs],sum[rs]+val[x]+rmin[ls]); Rmax[x]=max (Rmax[rs],sum[rs]+val[x]+rmax[ls]);} int build (int pre,int l,int R) {if (l>r) return 0;int mid= (l+r) >>1;fa[mid]=pre;if (l==r) {lmin[mid]=rmin[mid]= Lmax[mid]=rmax[mid]=0;sum[mid]=val[mid],size[mid]=1;if (val[mid]<0) lmin[mid]=rmin[mid]=-1;if (val[mid]>0) Lmax[mid]=rmax[mid]=1;return Mid;} Son[mid][0]=build (mid,l,mid-1); Son[mid][1]=build (mid,mid+1,r);p ushup (mid); return mid;} void Pushson (int x,int r1,int R2) {if (!x) return;if (R1) {Rev1[x]^=1;swap (lmin[x],lmax[x]), lmin[x]=-lmin[x],lmax[x]=-lmax[x];swap (Rmin[x],rmax[x]), rmin[x]=-rmin[x],rmax[x]=-rmax[x];sum[x]=-sum[x];val[x]=-val[x];} if (R2) {Rev2[x]^=1;swap (lmin[x],rmin[x]), swap (lmax[x],rmax[x]), swap (son[x][0],son[x][1]);}} void pushdown (int x) {int &ls=son[x][0],&rs=son[x][1],&r1=rev1[x],&r2=rev2[x];p Ushson (LS,R1,R2); Pushson (RS,R1,R2); r1=r2=0;} void Pushadd (int x) {if (fa[x]) Pushadd (fa[x]);p ushdown (x);} int Wson (int x) {return son[fa[x]][1]==x;} void rotate (int x) {if (!fa[x]) Return;int Y=fa[x],z=fa[y],l=wson (x), r=l^1;if (z) son[z][wson (y)]=x;fa[x]=z,fa[y]=x,fa [Son[x][r]]=y;son[y][l]=son[x][r],son[x][r]=y;pushup (Y), pushup (x);} void splay (int x,int RT) {if (!x) return;if (!RT) root=x;pushadd (x); for (int y=fa[x];fa[x]!=rt;rotate (x), y=fa[x]) if (fa[y ]!=RT) Rotate (Wson (x) ==wson (y)? y:x);} int findkth (int x,int k) {pushdown (x), if (size[son[x][0]]+1==k) return x;if (K<=size[son[x][0]]) return findkth (son[x ][0],K); Elsereturn findkth (son[x][1],k-size[son[x][0]]-1);} int N,m;char Str[n];int Main () {scanf ("%d%d%s", &n,&m,str+2), memset (val,0,sizeof val), for (int i=2;i<=n+1;i++) val[i]=str[i]== ' ('? 1:-1;root=build (0,1,n+2); int i=1;i<=m;i++) {int op,x,y;scanf ("%d%d%d", &op,&x,&y); x=findkth (root,x), y=findkth (root,y+2); Splay (x,0); splay (y,x); int z=son[y][0];if (op==0) printf ("%d\n", (rmax[z]+1)/2-(lmin[z]-1)/2), if (op==1) Pushson (Z, 1,0); if (op==2) Pushson (z,0,1);} return 0;}

  

BZOJ2209 [Jsoi2011] bracket sequence splay

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.