-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheap.cpp
76 lines (63 loc) · 1.34 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
/*
In the name of ALLAH
Author : Raashid Anwar
*/
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;
mt19937 rng((uint64_t)chrono::steady_clock::now().time_since_epoch().count());
class heap {
vector <int> a;
public:
heap() {
a.clear();
}
push(int x) {
a.push_back(x);
int i = (int)a.size() - 1;
while (i != 0 && a[(i - 1) >> 1] > a[i]) {
swap(a[(i - 1) >> 1], a[i]);
i = (i - 1) >> 1;
}
}
void heapify(int i) {
int min_index = i;
if (i + i + 1 < (int)a.size() && a[i + i + 1] < a[min_index])
min_index = i + i + 1;
if (i + i + 2 < (int)a.size() && a[i + i + 2] < a[min_index])
min_index = i + i + 2;
if (min_index != i) {
swap(a[i], a[min_index]);
heapify(min_index);
}
}
int top() {
assert(!a.empty());
return a[0];
}
pop() {
assert(!a.empty());
if ((int)a.size() == 1) {
a.pop_back();
} else {
a[0] = a.back();
a.pop_back();
heapify(0);
}
}
bool empty() {
return a.empty();
}
int size() {
return (int)a.size();
}
};
void solve() {
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}