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

无旋treap

时间:2019-03-16 16:49:46      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:div   type   names   als   The   mat   cin   class   show   

技术图片
  1 #ifndef FHQTREAP_H_INCLUDED
  2 #define FHQTREAP_H_INCLUDED
  3 
  4 //author Eterna
  5 #define Hello The_Cruel_World!
  6 #pragma GCC optimize(2)
  7 #include<iostream>
  8 #include<algorithm>
  9 #include<cstdio>
 10 #include<string>
 11 #include<cstring>
 12 #include<vector>
 13 #include<map>
 14 #include<set>
 15 #include<queue>
 16 #include<stack>
 17 #include<utility>
 18 #include<cmath>
 19 #include<climits>
 20 #include<deque>
 21 #include<functional>
 22 #include<complex>
 23 #include<numeric>
 24 #define max(x,y) ((x)>(y)?(x):(y))
 25 #define min(x,y) ((x)<(y)?(x):(y))
 26 #define Pi acos(-1.0)
 27 #define ABS(x) ((x) >= 0 ? (x) : (-(x)))
 28 #define pb(x) push_back(x)
 29 #define lowbit(x) (x & -x)
 30 #define FRIN freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\in.txt", "r", stdin)
 31 #define FROUT freopen("C:\\Users\\Administrator.MACHENI-KA32LTP\\Desktop\\out.txt", "w", stdout)
 32 #define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
 33 using namespace std;
 34 typedef long long ll;
 35 typedef unsigned long long ull;
 36 typedef pair<int, int> pii;
 37 const int maxn = 1e5;
 38 const int INF = 0x7fffffff;
 39 const int mod = 1e9 + 7;
 40 const double eps = 1e-7;
 41 inline int read_int() {
 42     char c;
 43     int ret = 0, sgn = 1;
 44     do { c = getchar(); } while ((c < 0 || c > 9) && c != -);
 45     if (c == -) sgn = -1; else ret = c - 0;
 46     while ((c = getchar()) >= 0 && c <= 9) ret = ret * 10 + (c - 0);
 47     return sgn * ret;
 48 }
 49 inline ll read_ll() {
 50     char c;
 51     ll ret = 0, sgn = 1;
 52     do { c = getchar(); } while ((c < 0 || c > 9) && c != -);
 53     if (c == -) sgn = -1; else ret = c - 0;
 54     while ((c = getchar()) >= 0 && c <= 9) ret = ret * 10 + (c - 0);
 55     return sgn * ret;
 56 }
 57 struct node {
 58     int value, key, Size, child[2];
 59 }arr[maxn + 5];
 60 int tot;
 61 inline void Push_Up(int index) {
 62     arr[index].Size = arr[arr[index].child[0]].Size + arr[arr[index].child[1]].Size + 1;
 63 }
 64 void Split_By_Size(int root, int& a, int& b, int k) {
 65     if (!root) {
 66         a = b = 0;
 67         return;
 68     }
 69     if (arr[arr[root].child[0]].Size < k) a = root, Split_By_Size(arr[root].child[1], arr[a].child[1], b, k - arr[arr[root].child[0]].Size - 1);
 70     else b = root, Split_By_Size(arr[root].child[0], a, arr[b].child[0], k);
 71     Push_Up(root);
 72 }
 73 void Split_By_Value(int root, int& a, int& b, int value) {
 74     if (!root) {
 75         a = b = 0;
 76         return;
 77     }
 78     if (arr[root].value <= value)a = root, Split_By_Value(arr[root].child[1], arr[a].child[1], b, value);
 79     else b = root, Split_By_Value(arr[root].child[0], a, arr[b].child[0], value);
 80     Push_Up(root);
 81 }
 82 void Merge(int& root, int a, int b) {
 83     if (!a || !b) {
 84         root = a + b;
 85         return;
 86     }
 87     if (arr[a].key < arr[b].key)root = a, Merge(arr[root].child[1], arr[a].child[1], b);
 88     else root = b, Merge(arr[root].child[0], a, arr[b].child[0]);
 89     Push_Up(root);
 90 }
 91 inline void Insert(int& root, int value) {
 92     int x = 0, y = 0, z = ++tot;
 93     arr[z].value = value, arr[z].key = rand(), arr[z].Size = 1;
 94     Split_By_Value(root, x, y, value);
 95     Merge(x, x, z);
 96     Merge(root, x, y);
 97 }
 98 inline void Erase(int& root, int value) {
 99     int x = 0, y = 0, z = 0;
100     Split_By_Value(root, x, y, value);
101     Split_By_Value(x, x, z, value - 1);
102     Merge(z, arr[z].child[0], arr[z].child[1]);
103     Merge(x, x, z);
104     Merge(root, x, y);
105 }
106 int Kth_number(int root, int k) {
107     while (arr[arr[root].child[0]].Size + 1 != k) {
108         if (arr[arr[root].child[0]].Size >= k)root = arr[root].child[0];
109         else k -= (arr[arr[root].child[0]].Size + 1), root = arr[root].child[1];
110     }
111     return arr[root].value;
112 }
113 int Get_Rank(int& root, int value) {
114     int x = 0, y = 0;
115     Split_By_Value(root, x, y, value - 1);
116     int res = arr[x].Size + 1;
117     Merge(root, x, y);
118     return res;
119 }
120 int Predecessor(int& root, int value) {
121     int x = 0, y = 0;
122     Split_By_Value(root, x, y, value - 1);
123     int res = Kth_number(x, arr[x].Size);
124     Merge(root, x, y);
125     return res;
126 }
127 int Successor(int& root, int value) {
128     int x = 0, y = 0;
129     Split_By_Value(root, x, y, value);
130     int res = Kth_number(y, 1);
131     Merge(root, x, y);
132     return res;
133 }
134 int n, op, u, root;
135 int main()
136 {
137     srand(19260817);
138     scanf("%d", &n);
139     while (n--) {
140         scanf("%d %d", &op, &u);
141         if (op == 1)Insert(root, u);
142         if (op == 2)Erase(root, u);
143         if (op == 3)printf("%d\n", Get_Rank(root, u));
144         if (op == 4)printf("%d\n", Kth_number(root, u));
145         if (op == 5)printf("%d\n", Predecessor(root, u));
146         if (op == 6)printf("%d\n", Successor(root, u));
147     }
148     return 0;
149 }
150 
151 
152 
153 
154 #endif // FHQTREAP_H_INCLUDED
View Code

 

无旋treap

标签:div   type   names   als   The   mat   cin   class   show   

原文地址:https://www.cnblogs.com/Eterna-King/p/10542876.html

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