Merger of Bzoj 2733 & splay

Source: Internet
Author: User

Test instructions

With the right to connect the block, Tim Edge and query the block K large.

SOL:

Splay merge + and check set.

I thought splay could use artifice to simply merge ... Tune an afternoon finally waking wake up ... So I had to slowly plug in a ... What heuristic merge ... Say so high-level actually so silk.

The implementation is very slag ... The middle sequence traverses 3060ms, and the queue does not get up quickly ... Is it my splay that's not right?...

%%%hzw Line Tree ... Discover the fact that line-tree merging is simpler and faster ... Blinded by the splay ... The idea is not open enough ... Alas...

Code:

  

/*==========================================================================# last modified:2016-03-20 10:23# filename:2733.cpp# Description: ==========================================================================*/# Define me Acrossthesky #include <cstdio> #include <cmath> #include <ctime> #include <string> #inc (in)  Lude <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define LOWBIT (x) (x) & (-X ) #define for (I,A,B) for ((i) = (a);(i) <= (b);(i) + +) #define FORP (I,A,B) for (int i= (a); i<= (b); i++) #define FORM (i,a,b ) for (int i= (a); i>= (b); i--) #define LS (A, a) (((a) + (b)) << 1) #define RS (A, B) (((a) + (c)) >> 1) #define GETLC (a) ch[(a)][0] #define GETRC (a) ch[(a)][1] #define MAXN 400000 #define MAXM 100000 #define PI 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using Namespace std; typedef long Long LL;  typedef unsigned long long ull;     Template<class t> inline void read (t& num) {bool start=false,neg=false;     char c;     num=0;         while ((C=getchar ())!=eof) {if (c== '-') start=neg=true;             else if (c>= ' 0 ' && c<= ' 9 ') {start=true;         num=num*10+c-' 0 ';     } else if (start) break; } if (neg) num=-num; }/*==================split line==================*/int ch[maxn][2],fa[maxn],father[maxn],sz[maxn],key[maxn];void pushup (int x) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;} int kth (int x,int k) {while (x) {if (k==sz[ch[x][0]]+1) return x;if (k>sz[ch[x][0]]+1) k-=sz[ch[x][0]],k--, x=ch[x][1]; else x=ch[x][0];}} int find (int x) {return father[x]==x?x:find (father[x]);} void rotate (int x) {int p=fa[x],q=fa[p],d=ch[p][1]==x;fa[ch[p][d]=ch[x][d^1]]=p; pushup (p); fa[ch[x][d^1]=p]=x; Pushup (x); fa[x]=q;if (q) {if (ch[q][1]==p) Ch[q][1]=x;else ch[q][0]=x;}} void splay (int x,int aim) {for (int y; (y=fa[x])!=aim;rotate (x) if (fa[y]!=aim) rotate ((GETLC (y) ==x) = = (GETLC (fa[y]) ==y)? y:x);} void Insert (int x,int root) {int Node=root;while (node) {sz[node]++; int &d=ch[node][key[node]<key[x]];if (d==0) { D=x; Fa[x]=node; ch[x][0]=ch[x][1]=0; Sz[x]=1; Node=x; break;} Node=d;} Splay (node,0);} int q[maxn];void join (int x,int root) {/*int Lc=ch[x][0],rc=ch[x][1];ch[x][0]=ch[x][1]=0;if (LC) {join (lc,root);}//ch[ x][1]=0; Fa[rc]=0;insert (X,root); if (RC) join (rc,root); */int head=1,tail=1;q[head]=x;while (Head<=tail) {X=q[head]; head++ ; int Lc=ch[x][0],rc=ch[x][1];ch[x][0]=ch[x][1]=0;insert (x,root); if (LC) Q[++TAIL]=LC; if (RC) Q[++TAIL]=RC;}} void Union (int x,int y) {int u=find (x), V=find (y), if (u==v) return;else {father[u]=v;splay (u,0); splay (v,0); if (sz[u]< SZ[V]) Join (U,V), else join (v,u),/*father[u]=v;int a=kth (u,1), b=kth (V,sz[v]); splay (a,0); Splay (b,0); if (Key[a]<key[b]) {ch[a][1]=b; fa[b]=a; Pushup (a);} else {ch[b][1]=a; fa[a]=b; pushup (b);} */}}void query (int x,int k) {splay (x,0); if (K>sz[x]) printf (" -1\n"), Else printf ("%d\n ", KTH (X,k));} int main () {int n,m; read (n); read (m); Memset (father,0,sizeof (father)); Forp (i,1,n) {read (key[i]); father[i]=i; sz[i]=1;} Forp (i,1,m) {int u,v; read (u); Read (v); Union (u,v);} int q; Read (q); Forp (i,1,q) {char ch[10]; scanf ("%s", ch), if (ch[0]== ' q ') {int x,k; read (x); read (k); query (x,k);} else {int u,v; read (u); Read (v); Union (U,V);}}}

Merger of Bzoj 2733 & 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.