-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7dde65
commit 0d9e197
Showing
1 changed file
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
template<typename T> | ||
class pq { | ||
private: | ||
int size, height; | ||
struct Node { | ||
T value; | ||
Node *l, *r, *parent; | ||
Node(T val) : value(val), l(nullptr), r(nullptr), parent(nullptr) {} // Node constructor | ||
}; | ||
Node* root; | ||
|
||
public: | ||
pq() : root(nullptr), size(0), height(0) {} | ||
|
||
void insert(T value) { | ||
size++; | ||
if ((size & (size - 1)) == 0) height++; | ||
if (size == 1) { | ||
root = new Node(value); | ||
return; | ||
} | ||
|
||
Node* now = root; | ||
for (int i = height - 2; i >= 0; i--) { | ||
if (((1 << i) & size) == 0) { | ||
if (!now->l) now->l = new Node(0); | ||
now->l->parent = now; | ||
now = now->l; | ||
} else { | ||
if (!now->r) now->r = new Node(0); | ||
now->r->parent = now; | ||
now = now->r; | ||
} | ||
} | ||
now->value = value; | ||
|
||
while (now->parent && now->value < now->parent->value) { | ||
swap(now->value, now->parent->value); | ||
now = now->parent; | ||
} | ||
} | ||
|
||
void pop() { | ||
if (size == 0) return; | ||
Node* now = root; | ||
|
||
for (int i = height - 2; i >= 0; i--) { | ||
if (((1 << i) & size) == 0) { | ||
now = now->l; | ||
} else { | ||
now = now->r; | ||
} | ||
} | ||
|
||
if (size > 1) { | ||
swap(root->value, now->value); | ||
} | ||
|
||
if (now->parent) { | ||
if (now == now->parent->l) now->parent->l = nullptr; | ||
else now->parent->r = nullptr; | ||
} | ||
delete now; | ||
|
||
if ((size & (size - 1)) == 0) height--; | ||
size--; | ||
|
||
now = root; | ||
while (now && (now->l || now->r)) { | ||
Node* smallest = now; | ||
if (now->l && now->l->value < smallest->value) smallest = now->l; | ||
if (now->r && now->r->value < smallest->value) smallest = now->r; | ||
if (smallest == now) break; | ||
swap(now->value, smallest->value); | ||
now = smallest; | ||
} | ||
} | ||
|
||
T top() { | ||
return root->value; | ||
} | ||
|
||
bool empty() const{ | ||
return size == 0; | ||
} | ||
}; | ||
|
||
int main() { | ||
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); | ||
pq<int> test; | ||
int n; cin >> n; | ||
while (n--){ | ||
int x; cin >> x; | ||
if (x) test.insert(x); | ||
else{ | ||
if (test.empty()) cout << 0 << "\n"; | ||
else { | ||
cout << test.top() << "\n"; test.pop(); | ||
} | ||
} | ||
} | ||
return 0; | ||
} |