forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Treap.cpp
206 lines (181 loc) · 3.11 KB
/
Treap.cpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
struct Treap
{
struct data
{
int priority, val, cnt;
data *l, *r;
data()
{
val = 0, cnt = 0, l = NULL, r = NULL;
}
data (int _val)
{
val = _val, cnt = 1;
l = NULL, r = NULL;
priority = getRand(1, 2e9);
}
};
typedef struct data* node;
node head;
Treap(): head(0) {}
int cnt(node cur)
{
return cur ? cur->cnt : 0;
}
void updateCnt(node cur)
{
if(cur)
cur->cnt = cnt(cur->l) + cnt(cur->r) + 1;
}
void push(node cur) //Lazy Propagation
{
;
}
void combine(node &cur, node l, node r)
{
if(!l)
{
cur = r;
return;
}
if(!r)
{
cur = l;
return;
}
//Merge Operations like in segment tree
}
void reset(node &cur) //To reset other fields of cur except value and cnt
{
if(!cur)
return;
}
void operation(node &cur)
{
if(!cur)
return;
reset(cur);
combine(cur, cur->l, cur);
combine(cur, cur, cur->r);
}
void splitPos(node cur, node &l, node &r, int k, int add = 0)
{
if(!cur)
return void(l = r = 0);
push(cur);
int idx = add + cnt(cur->l);
if(idx <= k)
splitPos(cur->r, cur->r, r, k, idx + 1), l = cur;
else
splitPos(cur->l, l, cur->l, k, add), r = cur;
updateCnt(cur);
operation(cur);
}
void split(node cur, node &l, node &r, int k)
{
if(!cur)
return void(l = r = 0);
push(cur);
int idx = cur -> val;
if(idx <= k)
split(cur->r, cur->r, r, k), l = cur;
else
split(cur->l, l, cur->l, k), r = cur;
updateCnt(cur);
operation(cur);
}
void merge(node &cur, node l, node r)
{
push(l);
push(r);
if(!l || !r)
cur = l ? l : r;
else if(l->priority > r->priority)
merge(l->r, l->r, r), cur = l;
else
merge(r->l, l, r->l), cur = r;
updateCnt(cur);
operation(cur);
}
void insert(int val)
{
if(!head)
{
head = new data(val);
return;
}
node l, r, mid, mid2, rr;
mid = new data(val);
split(head, l, r, val - 1);
merge(l, l, mid);
split(r, mid2, rr, val);
merge(head, l, rr);
}
void erase(int val)
{
node l, r, mid;
split(head, l, r, val - 1);
split(r, mid, r, val);
merge(head, l, r);
}
void inorder(node cur)
{
if(!cur)
return;
push(cur);
inorder(cur->l);
cout<<cur->val<<" ";
inorder(cur->r);
}
void inorder()
{
inorder(head);
cout<<endl;
}
void clear(node cur)
{
if(!cur)
return;
clear(cur->l), clear(cur->r);
delete cur;
}
void clear()
{
clear(head);
}
int find_by_order(int x) //1 indexed
{
if(!x)
return -1;
x--;
node l, r, mid;
splitPos(head, l, r, x - 1);
splitPos(r, mid, r, 0);
int ans = -1;
if(cnt(mid) == 1)
ans = mid->val;
merge(r, mid, r);
merge(head, l, r);
return ans;
}
int order_of_key(int val) //1 indexed
{
node l, r, mid;
split(head, l, r, val - 1);
split(r, mid, r, val);
int ans = -1;
if(cnt(mid) == 1)
ans = 1 + cnt(l);
merge(r, mid, r);
merge(head, l, r);
return ans;
}
};
//Problem 1: https://www.spoj.com/problems/ALLIN1/
//Solution 1: http://p.ip.fi/DOgq