Bzoj 2733 Yong No Xiang

Source: Internet
Author: User

This topic splay heuristic merging ... That is, violent insertion.

But why write one and check the set?

Not to find the root of splay ... Just to determine if it is within a set, not break off.

5s is dead ....

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#defineMAXV 100500using namespacestd;intn,m,q,x,y,tree[maxv][3],SIZE[MAXV],FATH[MAXV],VAL[MAXV],FINDS[MAXV];Chars[5];BOOLIsRootintx) {    returntree[fath[x]][1]!=x&&tree[fath[x]][2]!=x;}voidPushup (intx) {    intls=tree[x][1],rs=tree[x][2]; SIZE[X]=size[ls]+size[rs]+1;}intGetfather (intx) {    if(x!=finds[x]) finds[x]=Getfather (finds[x]); returnfinds[x];}voidInsertintNodeintXintNowintfather) {    if(now==0) {Fath[node]=father; if(X>val[father]) tree[father][2]=node; Elsetree[father][1]=node; tree[node][1]=0; tree[node][2]=0; Size[node]=1; return; }    if(X>val[now]) insert (node,x,tree[now][2],now); ElseInsert (node,x,tree[now][1],now); Pushup (now);}voidRotateintx) {    inty=fath[x],z=Fath[y],l,r; if(tree[y][1]==X) l=1;ElseL=2; R=3-l; if(!isroot (y)) {        if(tree[z][1]==y) tree[z][1]=x; Elsetree[z][2]=x; } Fath[x]=z;fath[y]=x;fath[tree[x][r]]=y; TREE[Y][L]=tree[x][r];tree[x][r]=X; Pushup (y);p ushup (x);}voidSplay (intx) {     while(!isroot (x)) {        inty=fath[x],z=Fath[y]; if(!isroot (y)) {            if((tree[y][1]==x) ^ (tree[z][1]==y)) rotate (x); Elserotate (y);    } rotate (x); }}voidBFsintXinty) {Queue<int>Q;    Q.push (x);  while(!Q.empty ()) {        intHead=Q.front ();        Q.pop ();        Insert (head,val[head],y,y); if(tree[head][1]) Q.push (tree[head][1]); if(tree[head][2]) Q.push (tree[head][2]); }}voidUnionn (intXinty) {    intF1=getfather (x), f2=Getfather (y); if(F1==F2)return;    Splay (x); splay (y); if(size[x]>Size[y]) swap (x, y); BFS (x, y); Finds[x]=y;}intAskintNowintk) {    intls=tree[now][1],rs=tree[now][2]; if(size[ls]>=k)returnAsk (ls,k); Else if(size[ls]<k-1)returnAsk (rs,k-size[ls]-1); Else returnNow ;}voidWork1 () {scanf ("%d%d",&x,&y);    Splay (x); if(size[x]<y) {printf ("-1\n");return;} intans=Ask (x, y); printf ("%d\n", ans); Splay (ANS);}voidWork2 () {scanf ("%d%d",&x,&y); Unionn (x, y);}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%d",&Val[i]); Size[i]=1; Finds[i]=i; }     for(intI=1; i<=m;i++) {scanf ("%d%d",&x,&y);    Unionn (x, y); } scanf ("%d",&q);  for(intI=1; i<=q;i++) {scanf ("%s", s); if(s[0]=='Q') Work1 (); ElseWork2 (); }    return 0;}

Bzoj 2733 Yong No Xiang

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.