Recently learned the left side of the tree, learning when the online no detailed tutorial of the suffering, so I write an article ( because it is konjac konjac so may not write very good )
What is a left-leaning tree?
Left side, as the name implies, is to the left tilt, left-leaning tree to meet the nature of the heap, but also to meet the nature of the left bias
Because it tilts to the left, it can effectively reduce the time complexity of the query.
Let's take a look at a left-leaning tree.
This is a left-leaning tree ( though a little ugly )
The left-leaning tree has two important values: The key value, the distance
The key value is the weight of the point, and the distance value of each point is the distance of its right son plus 1.
The nature of maintaining the left bias is the distance value , and the maintenance heap is the property of the weight
The left-leaning tree satisfies at least one of several actions
Merge, query, delete
Merge:
First of all, merging is the combination of two left-leaning trees to make a left-leaning tree.
Basic Flow:
1. Determine which left-leaning tree is smaller and will fit into small (depending on the topic)
2. If the inserted tree is empty, insert it directly, otherwise find the right subtree of the inserted tree and continue merging
3. After inserting, if the dis value of the right subtree is greater than the left subtree, then the tree is exchanged around
So, merging is a recursive process
Inquire:
Queries are generally the minimum value of the query tree, which is the top element of the heap (depending on the situation), and no more
Delete:
Deleting a point is actually merging the left and right sub-trees of this point and merging with the original tree.
Talk so much, then we will practice practiced hand bar: Rokua left-leaning tree template: https://www.luogu.org/problemnew/show/P3377
Obviously, this problem is a veritable template problem, as long as the master of the left-leaning tree I said before the basic operation can be a
So, directly on the code
#include <bits/stdc++.h>#defineN 100001using namespacestd;intn,m,fa[n],val[n],dis[n],son[n][2];intGfintx) { while(Fa[x]) x=Fa[x]; returnx;}intMergeintXinty) { if(x==0|| y==0)returnx+y; if(val[x]>val[y]| | (val[x]==val[y]&&x>y)) Swap (x, y); son[x][1]=merge (son[x][1],y); fa[son[x][1]]=x; if(dis[son[x][1]]>dis[son[x][0]]) Swap (son[x][1],son[x][0]); DIS[X]=dis[son[x][1]]+1; returnx;} MergingvoidPopintx) {Val[x]=-1; fa[son[x][0]]=fa[son[x][1]]=0; Merge (son[x][0],son[x][1]);} DeleteintMain () {scanf ("%d%d",&n,&m); dis[0]=-1; for(intI=1; i<=n;i++) scanf ("%d",&Val[i]); for(intI=1; i<=m;i++){ intOPT;SCANF ("%d",&opt); Switch(opt) { Case 1:{ intX,Y;SCANF ("%d%d",&x,&y); X=GF (x), y=GF (y); if(val[x]==-1|| val[y]==-1)Continue; if(x==y)Continue; Merge (x, y); Break; } Case 2:{ intX;SCANF ("%d", &x); x=GF (x); if(val[x]==-1) {printf ("%d\n",-1); Continue; } printf ("%d\n", val[x]); Pop (x); Break; } } } return 0;}
A This problem, it means that you have mastered the left-leaning tree this cow b data structure, so say,
Data structures are actually not that difficult (!) Mistaken
Left-leaning Tree tutorial