-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleftisttree.cpp
More file actions
66 lines (58 loc) · 983 Bytes
/
Copy pathleftisttree.cpp
File metadata and controls
66 lines (58 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
const int N=200001;
int n;
int we[N],di[N],le[N],ri[N],idx;
int pa[N];
bool cmp(int x,int y){
if(we[x]!=we[y])return we[x]<we[y];
else return x<y;
}
int find(int x){
if(pa[x]==x)return x;
else return pa[x]=find(pa[x]);
}
int merge(int x, int y){
if(!x||!y)return x+y;
if(cmp(y,x))swap(x,y);
ri[x]=merge(ri[x],y);
if(di[ri[x]]>di[le[x]])swap(le[x],ri[x]);
di[x]=di[ri[x]]+1;
return x;
}
int main(){
scanf("%d",&n);
we[0]=2e9;
while(n--){
int t,x,y;
scanf("%d%d",&t,&x);
if(t==1){
we[++idx]=x;
di[idx]=1;
pa[idx]=idx;
}
else if(t==2){
scanf("%d",&y);
x=find(x),y=find(y);
if(x!=y){
if(cmp(y,x))swap(x,y);
pa[y]=x;
merge(x,y);
}
}
else if(t==3){
printf("%d\n",we[find(x)]);
}
else{
x=find(x);
if(cmp(ri[x],le[x])){
swap(le[x],ri[x]);
}
pa[x]=le[x];
pa[le[x]]=le[x];
merge(le[x],ri[x]);
}
}
}