-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreap.cc
More file actions
205 lines (179 loc) · 4.48 KB
/
Treap.cc
File metadata and controls
205 lines (179 loc) · 4.48 KB
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
#include <bits/stdc++.h>
using namespace std;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
default_random_engine generator(seed);
uniform_int_distribution<int> distribution(0,1000000);
string ans = "";
struct Node {
Node *left, *right, *parent;
string word;
int order, priority, count;
int chars;
//constructor
Node (int x, string s) : word(s), order(x), priority(distribution(generator)), count(1) {
left = right = parent = nullptr;
chars = s.size();
}
};
//returns the cardinality of a Node
inline int numWords(Node* N) {
return (N ? N->count : 0);
}
inline int numChars(Node* N) {
return (N ? N->chars : 0);
}
//Makes P the parent of N
inline void SetParent (Node* N, Node* P) {
if(N) N->parent = P;
}
//Updates a node which has a modified son
//it re-sets the cardinality and adjusts the parents
void Update(Node* N) {
if (!N) return;
N->count = 1 + numWords(N->left) + numWords(N->right);
N->chars = N->word.size() + numChars(N->left) + numChars(N->right);
SetParent(N->left, N);
SetParent(N->right, N);
}
//make the higher priority node on top, Merge recursively
//N1 seems to go to the left by default
Node* Merge(Node* N1, Node* N2) {
if (!N1) return N2;
if (!N2) return N1;
if (N1->priority >= N2->priority) {
N1->right = Merge(N1->right, N2);
Update(N1);
return N1;
} else {
N2->left = Merge(N1, N2->left);
Update(N2);
return N2;
}
}
// Splits the Tree into trees with order > x and order <= x
// first is x <= x, second > x
// You go Splitting recursively and reattaching edges to the
// First node you encounter of the same type.
pair<Node*, Node*> Split_by_Order(Node* N, int x) {
if (!N) return {nullptr, nullptr};
if (N->order < x) {
auto p = Split_by_Order(N->right, x);
N->right = p.first;
Update(N);
SetParent(N, nullptr);
return {N, p.second};
} else {
auto p = Split_by_Order(N->left, x);
N->left = p.second;
Update(N);
SetParent(N, nullptr);
return {p.first, N};
}
}
//Same as above, it simply gets the first x elements.
pair<Node*, Node*> Split_by_Cardinality(Node* N, int x) {
if (!N) return {nullptr, nullptr};
if (numWords(N->left) + 1 <= x) {
auto p = Split_by_Cardinality(N->right, x - numWords(N->left) - 1);
N->right = p.first;
Update(N);
SetParent(N, nullptr);
return {N, p.second};
} else {
auto p = Split_by_Cardinality(N->left, x);
N->left = p.second;
Update(N);
SetParent(N, nullptr);
return {p.first, N};
}
}
//Inserts N2 into N1
//It divides N1 into 2 Treaps, and merges with the Treap of
//lesser order, then reattaches the other Treap
Node * Insert(Node* N1, Node* N2) {
auto p = Split_by_Cardinality(N1, N2->order);
N1 = Merge(p.first, N2);
N1 = Merge(N1, p.second);
return N1;
}
/*
//Returns the number of nodes such that order < x
int Count(Node* N, int x) {
if(!N) return 0;
if(N->order <= x) return 1 + Cardinality(N->left) + Count(N->right, x);
return Count(N->left,x);
}
//Updates a Node
//Strips the 2 sons of N and merges them into one treap
//saves the parent of N and completely resets N(except for priority)
//Reattaches the splitted sons into the parent, updates until we reach root
//if N was the root we make the merged sons the Root
//Now we insert the updated N
Node* UpdateNode(Node* &R, Node* N, int nx) {
SetParent(N->left, nullptr);
SetParent(N->right, nullptr);
auto m = Merge(N->left, N->right);
auto p = N->parent;
N->left = N->right = N->parent = nullptr;
N->order = nx;
Update(N);
if(p) {
p->left == N ? p->left = m : p->right = m;
if(m) m->parent = p;
while(p) {
Update(p);
p = p->parent;
}
} else {
R = m;
}
return Insert(R,N);
}
//Deletes a node
//Same as above but we don't reinsert
Node* Delete(Node* R, Node* N) {
SetParent(N->left, nullptr);
SetParent(N->right, nullptr);
auto m = Merge(N->left, N->right);
auto p = N->parent;
if(p) {
p->left == N ? p->left = m : p->right = m;
if(m) m->parent = p;
while(p) {
Update(p);
p = p->parent;
}
} else {
R = m;
}
}
*/
char ith(Node* N, int x) {
if (numChars(N->left) - 1 >= x) {
return ith(N->left, x);
}
if((int)(numChars(N->left) - 1 + N->word.size()) >= x) {
return N->word[x - numChars(N->left)];
}
return ith(N->right, x - numChars(N->left) - N->word.size());
}
int main() {
Node* Root = nullptr;
char c;
while(cin >> c) {
if(c == 'I') {
string s;
int p;
cin >> s >> p;
Node* n = new Node(p, s);
Root = Insert(Root, n);
}
if(c == 'C') {
int x;
cin >> x;
cout << ith(Root, x);
}
if(c == 'E') return cout << endl, 0;
//
}
}