-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-1.cpp
More file actions
241 lines (219 loc) · 5.23 KB
/
3-1.cpp
File metadata and controls
241 lines (219 loc) · 5.23 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
Вычислить разницу глубин наивного дерева поиска и декартового дерева. Разница может быть отрицательна.
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
template<class T>
struct BSTNode
{
BSTNode *left;
BSTNode *right;
T data;
BSTNode(const T& data):
left(NULL),
right(NULL),
data(data){};
};
template <class T>
class Tree
{
public:
Tree():
root(NULL){};
~Tree(){};
void Add(const T& data);
bool Has(const T& data) const;
void List(const BSTNode<T> *node) const;
void Destroy(BSTNode<T> *node);
BSTNode<T> *Root() const{return root;};
int Height(BSTNode<T> *node) const;
private:
BSTNode<T> *root;
};
template <class T>
void Tree<T>::Destroy(BSTNode<T> *node)
{
if(node==NULL)
return;
Destroy(node->left);
Destroy(node->right);
delete node;
}
template <class T>
void Tree<T>::Add(const T& data)
{
BSTNode<T> **node=&root;
for(;*node;)
{
if((*node)->data<=data)
node=&(*node)->right;
else
node=&(*node)->left;
}
*node=new BSTNode<T>(data);
}
template <class T>
void Tree<T>::List(const BSTNode<T> *node) const
{
if(node==NULL)
return;
cout<<node->data<<" ";
List(node->left);
List(node->right);
}
template <class T>
int Tree<T>::Height(BSTNode<T> *node) const
{
if(node==NULL)
return 1;
int tmp1=Height(node->left);
int tmp2=Height(node->right);
return tmp1>tmp2?tmp1+1:tmp2+1;
}
template<class T>
struct DecartNode
{
DecartNode *left;
DecartNode *right;
T data;
long long int priority;
DecartNode(const T& data, const long long int priority):
left(NULL),
right(NULL),
data(data),
priority(priority){};
};
template <class T>
class DecartTree
{
public:
DecartTree():
root(NULL){};
~DecartTree(){};
void Add(const T& data, const long long int priority, DecartNode<T> *&node);
DecartNode<T> *Search(const long long int priority) const;
void List(const DecartNode<T> *node) const;
void Destroy(DecartNode<T> *node);
DecartNode<T> *Root() const{return root;};
int Height(DecartNode<T> *node) const;
private:
DecartNode<T> *root;
void Split( DecartNode<T>* currentNode, const T& key, DecartNode<T>*& left,DecartNode<T>*& right );
DecartNode<T>* Merge( DecartNode<T>* left, DecartNode<T>* right );
};
template <class T>
void DecartTree<T>::Split( DecartNode<T>* currentNode, const T& key, DecartNode<T>*& left,DecartNode<T>*& right )
{
if( currentNode == 0 )
{
left = 0;
right = 0;
}
else if( currentNode->data <= key )
{
Split( currentNode->right, key, currentNode->right, right );
left = currentNode;
}
else
{
Split( currentNode->left, key, left, currentNode->left );
right = currentNode;
}
}
template <class T>
DecartNode<T>* DecartTree<T>::Merge( DecartNode<T>* left, DecartNode<T>* right )
{
if( left == 0 || right == 0 )
return left == 0 ? right : left;
if( left->priority > right->priority )
{
left->right = Merge( left->right, right );
return left;
}
right->left = Merge( left, right->left );
return right;
}
template <class T>
void DecartTree<T>::Destroy(DecartNode<T> *node)
{
if(node==NULL)
return;
Destroy(node->left);
Destroy(node->right);
delete node;
}
template <class T>
void DecartTree<T>::Add(const T& data, const long long int priority, DecartNode<T>*& node)
{
if(node == NULL)
{
node = new DecartNode<T>(data, priority);
return;
}
if(priority <= node->priority)
{
if(node->data > data)
Add(data, priority, node->left);
else
Add(data, priority, node->right);
}
else
{
DecartNode<T>* left = NULL;
DecartNode<T>* right = NULL;
Split(node, data, left, right);
DecartNode<T>* curr = new DecartNode<T>(data, priority);
curr->left = left;
curr->right = right;
node = curr;
}
}
template <class T>
DecartNode<T> *DecartTree<T>::Search(const long long int priority) const
{
const DecartNode<T> *node=root;
for(;;)
{
if (node == NULL) return 0;
else if (priority < node->priority) node = node->left;
else return node;
}
}
template <class T>
void DecartTree<T>::List(const DecartNode<T> *node) const
{
if(node==NULL)
return;
cout<<node->data<<" ";
List(node->left);
List(node->right);
}
template <class T>
int DecartTree<T>::Height(DecartNode<T> *node) const
{
if(node==NULL)
return 1;
int tmp1=Height(node->left);
int tmp2=Height(node->right);
return tmp1>tmp2?tmp1+1:tmp2+1;
}
int main()
{
DecartTree<long long int> a;
DecartNode<long long int> *root=a.Root();
Tree<long long int> b;
int n;
cin>>n;
for(int i=0,tmp_k,tmp_p;i<n;i++)
{
cin>>tmp_k>>tmp_p;
a.Add(tmp_k,tmp_p,root);
b.Add(tmp_k);
}
cout<<b.Height(b.Root())-a.Height(root);
a.Destroy(root);
b.Destroy(b.Root());
// system("pause");
return 0;
}