-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
81 lines (71 loc) · 1.38 KB
/
heap.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
#include <cstdio>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <list>
#include <set>
#include <map>
using namespace std;
// min heap.
const int N = 100010;
int x[N], sz;
// go up if cur should go according to heap property.
inline void pushUp(int cur) {
while (cur > 0) {
int par = ((cur - 1) >> 1);
if (x[par] > x[cur]) {
swap(x[par], x[cur]);
cur = par;
} else break;
}
}
// go down if cur should go according to heap property.
inline void pushDown(int cur) {
while (true) {
int minchild = cur + cur + 1;
if (minchild >= sz) break;
if (minchild + 1 < sz && x[minchild + 1] < x[minchild]) {
minchild++;
}
if (x[minchild] < x[cur]) {
swap(x[minchild], x[cur]);
cur = minchild;
} else break;
}
}
// add new element same as push in priority queue.
inline void add(int a) {
x[sz] = a;
pushUp(sz++);
}
// get min element, here min heap.
inline int getMin() {
if (sz > 0) {
int ans = x[0];
swap(x[0], x[sz - 1]);
sz--;
pushDown(0);
return ans;
}
return -1;
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int cmd, a;
scanf("%d", &cmd);
if (cmd == 0) {
scanf("%d", &a);
add(a);
} else {
printf("%d\n", getMin());
}
}
}