码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】斜堆

时间:2018-12-03 13:54:15      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:class   string   color   nbsp   char   else   include   cstring   ++   

如题这是一个模板。。。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cctype>
 6 
 7 inline void read(int & x)
 8 {
 9     x = 0;
10     int k = 1;
11     char c = getchar();
12     while (!isdigit(c))
13         if (c == -) c = getchar(), k = -1;
14         else c = getchar();
15     while (isdigit(c))
16         x = (x << 1) + (x << 3) + (c ^ 48),
17         c = getchar();
18     x *= k;
19 }
20 
21 const int MAXN = 100010;
22 int n, m, opt, x, y, tot = 0;
23 int faz[MAXN], son[MAXN][2], val[MAXN];
24 
25 inline int Find(int u)
26 {
27     while (faz[u]) u = faz[u];
28     return u;
29 }
30 
31 int Merge(int a, int b)
32 {
33     if (!a || !b) { return a + b; }
34     if (val[a] > val[b] || (val[a] == val[b] && a > b))
35         std::swap(a, b);
36     son[a][1] = Merge(son[a][1], b);
37     faz[son[a][1]] = a;
38     std::swap(son[a][1], son[a][0]); 
39     return a;
40 }
41 
42 int qa, qb, cc;
43 
44 inline void Merge_opt(int aa, int bb)
45 {
46     if (val[aa] == -1 || val[bb] == -1) return;
47     qa = Find(aa), qb = Find(bb);
48     if (qa == qb) return;
49     Merge(qa, qb);
50 }
51 
52 signed main()
53 {
54     read(n), read(m);
55     for (int i = 1; i <= n; ++i)
56         read(val[i]);
57     for (int i = 1; i <= m; ++i)
58     {
59         read(opt), read(x);
60         if (opt == 1) read(y), Merge_opt(x, y);
61         else
62         {
63             if (val[x] == -1) {puts("-1"); continue;}
64             y = Find(x);
65             printf("%d\n", val[y]);
66             faz[son[y][0]] = faz[son[y][1]] = 0;
67             val[y] = -1;
68             Merge_opt(son[y][0], son[y][1]);
69         }
70     }
71     return 0;
72 }

 

【模板】斜堆

标签:class   string   color   nbsp   char   else   include   cstring   ++   

原文地址:https://www.cnblogs.com/yanyiming10243247/p/10057792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!