Zoj 3765 Lights (stretching tree)

Source: Internet
Author: User

Test instructions: A row of lights have light and dark, each light has a value;

Five types of operations:

Q L R SS Seek the GCD of the light in the [L,r] interval in which the state is SS;

I I value SS inserts a value after the first light for the value state for the SS Light;

D I delete the first light;

R I change the state of the first light;

M I x change the value of the first light to X;

Reference: Http://wenku.baidu.com/link?url=EEwhlUPp2_U05mJEcbskt0_e3scLHpZ5gfS2s4OEvuAObTB2CgB4_TUroeccorOEGMxzzKk9SA_ Qt2w3k06xbg6p1qlpr8plgymusfhfocg

Idea: involving the insertion and deletion of arrays generally think of stretching tree splay, typical application; a binary search tree;

In order to make the whole search time smaller, those items with high frequency are often placed near the root of the tree. This is the advantage of stretching the tree; averaging complexity O (logn)

The core operation of the stretching tree is rotation, and each operation requires the number of operations to rotate to the ch[ch[root][1]][0] position near the root node;

Super-awesome data structure.

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineKey_value Ch[ch[root][1]][0]Const intmaxn=500010;Const intinf=0x3f3f3f3f;intpre[maxn],ch[maxn][2],KEY[MAXN],SIZ[MAXN];//parent node, child node, number, subtree sizeintroot,tot1;intSUM0[MAXN],SUM1[MAXN];//current GCD for two statesintST[MAXN];//the on or off state of the nodeintS[maxn],tot2;//memory pool, capacityinta[maxn],status[maxn],n,q;Long LonggcdLong LongALong Longb) {  if(b==0)returnA; returnGCD (b,a%b);}voidNewNode (int&r,intFatherintKintSS) {//Establishing Nodes  if(Tot2) r=s[tot2--]; Elser=++tot1; PRE[R]=father; ch[r][0]=ch[r][1]=0; KEY[R]=k;//valueSt[r]=ss;//Light Status  if(ss==0) sum0[r]=k,sum1[r]=0; Elsesum0[r]=0, sum1[r]=K; SIZ[R]=1;}voidPushup (intR) {//Update parent node, update up    intlson=ch[r][0],rson=ch[r][1]; SIZ[R]=siz[lson]+siz[rson]+1; SUM0[R]=sum1[r]=0; SUM0[R]=gcd (Sum0[lson],sum0[rson]); SUM1[R]=gcd (Sum1[lson],sum1[rson]); if(St[r]) sum1[r]=gcd (Sum1[r],key[r]); Elsesum0[r]=gcd (Sum0[r],key[r]);}voidBuildint&x,intLintRintFather) {//two fork Tree achievements    if(L>r)return; intMid= (L+R)/2;    NewNode (X,father,a[mid],status[mid]); Build (ch[x][0],l,mid-1, x); Build (ch[x][1],mid+1, r,x); Pushup (x);}voidInit () {//InitializeRoot=tot1=tot2=0; ch[root][0]=ch[root][1]=siz[root]=pre[root]=0; Sum0[root]=sum1[root]=0; NewNode (Root,0,0,0); NewNode (ch[root][1],root,0,0);//establish the root node     for(intI=0; i<n;i++) scanf ("%d%d",&a[i],&Status[i]); Build (Key_value,0, N-1, ch[root][1]);//AchievementsPushup (ch[root][1]); Pushup (root);}voidRotate (intXintKind) {//Rotate    inty=Pre[x]; ch[y][!kind]=Ch[x][kind]; Pre[ch[x][kind]]=y; if(Pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x; PRE[X]=Pre[y]; Ch[x][kind]=y; Pre[y]=x; Pushup (y);}voidSplay (intRintGoal) {//stretching    while(pre[r]!=goal) {      if(pre[pre[r]]==goal) {Rotate (r,ch[pre[r]][0]==R); }      Else{        inty=Pre[r]; intkind=ch[pre[y]][0]==y; if(ch[y][kind]==R) {Rotate (R,!kind);        Rotate (R,kind); }        Else{Rotate (y,kind);        Rotate (R,kind);   }}} pushup (r); if(goal==0) root=R;}intGet_kth (intRintK) {//query in sequence traversal K large    intt=siz[ch[r][0]]+1; if(t==k)returnR; if(t>k)returnGet_kth (ch[r][0],k); Else returnGet_kth (ch[r][1],k-t);}voidInsert (intPosintValintSS) {splay (get_kth (Root,pos+1),0); Splay (get_kth (Root,pos+2), root); NewNode (key_value,ch[root][1],val,ss); Pushup (ch[root][1]); Pushup (root);}voidEraseintR) {   if(!R)return; s[++tot2]=R; Erase (ch[r][0]); Erase (ch[r][1]);}voidDelete (intPOS) {//Delete a nodeSplay (Get_kth (Root,pos),0); Splay (get_kth (Root,pos+2), root);   Erase (Key_value); Pre[key_value]=0; Key_value=0; Pushup (ch[root][1]); Pushup (root);}voidChangeintPOS) {//FlipSplay (Get_kth (Root,pos),0); Splay (get_kth (Root,pos+2), root); St[key_value]^=1;    Pushup (Key_value); Pushup (ch[root][1]); Pushup (root);}voidModifyintPosintVal) {//ModifySplay (Get_kth (Root,pos),0); Splay (get_kth (Root,pos+2), root); Key[key_value]=Val;    Pushup (Key_value); Pushup (ch[root][1]); Pushup (root);}intQueryintLintRintSS) {//Query GCD    intans; Splay (Get_kth (root,l),0); Splay (get_kth (Root,r+2), root); if(ss==0) ans=Sum0[key_value]; Elseans=Sum1[key_value]; if(ans==0) ans=-1; returnans;}intMain () {inti,j,k; Charch[ the];  while(SCANF ("%d%d", &n,&q)! =EOF)        {init ();  while(q--) {scanf ("%s", CH); if(ch[0]=='Q'){                intL,r,ss; scanf ("%d%d%d",&l,&r,&SS); printf ("%d\n", Query (L,R,SS)); }            Else if(ch[0]=='I'){                intPos,val,ss; scanf ("%d%d%d",&pos,&val,&SS);            Insert (POS,VAL,SS); }            Else if(ch[0]=='D'){                intPOS; scanf ("%d",&POS);            Delete (POS); }            Else if(ch[0]=='R'){                intPOS; scanf ("%d",&POS);            Change (POS); }            Else if(ch[0]=='M'){                intPos,val; scanf ("%d%d",&pos,&val);            Modify (Pos,val); }        }    }    return 0;}

Zoj 3765 Lights (stretching tree)

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.