-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.cpp
195 lines (163 loc) · 4.88 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
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
/*
author : vbhv-unyl
Title : Modular Heap Data Structure
Description : This a modular class, designed to implement min and max heap
at the same time. The constructor of the class has two parameters :-
1. An array a - Heap will be built initially on this array.
2. A flag variable - If true you will get a min heap and max heap otherwise.
By default the value of the flag is false and you'll create a max heap.
*/
#include <bits/stdc++.h>
#include<chrono>
#include<random>
using namespace std;
using namespace chrono;
#define ll long long
#define vi vector<ll>
#define pi pair<ll, ll>
#define vii vector<pi>
#define vvi vector<vi>
#define mpi map<ll, ll>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcount(x)
#define eb(x) emplace_back(x)
#define ff first
#define ss second
#define speed cin.tie(NULL)->sync_with_stdio(false)
#define rep(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define reb(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--)
mt19937 generator (std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1e5 + 2, MOD = 1e9 + 7;
const ll INF = 9e18;
ll getNumber(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(generator);
}
template<typename T> class Heap {
private:
ll sz;
bool flag;
vector<T> heap;
void heapify_up(ll i) {
ll idx = i;
while(idx > 0) {
ll parent = (idx - 1) >> 1;
if(!flag) {
if(heap[parent] < heap[idx]) {
swap(heap[parent], heap[idx]);
idx = parent;
}
else break;
}
else {
if(heap[parent] > heap[idx]) {
swap(heap[parent], heap[idx]);
idx = parent;
}
else break;
}
}
}
void heapify_down() {
ll i = 0;
swap(heap[0], heap.back());
heap.pop_back();
sz--;
ll prev = i;
while(1) {
ll left = -1, right = -1;
if((i << 1) + 1 < sz) left = (i << 1) + 1;
if((i << 1) + 2 < sz) right = (i << 1) + 2;
if(left == -1 && right == -1) break;
if(left != -1 && right != -1) {
ll val = heap[i], idx = i;
if(!flag) {
if(heap[left] > val) val = heap[left], idx = left;
if(heap[right] > val) val = heap[right], idx = right;
}
else {
if(heap[left] < val) val = heap[left], idx = left;
if(heap[right] < val) val = heap[right], idx = right;
}
swap(heap[i], heap[idx]);
i = idx;
}
else if(left != -1) {
ll idx = i;
if(!flag) {
if(heap[left] > heap[idx]) idx = left;
}
else {
if(heap[left] < heap[idx]) idx = left;
}
swap(heap[i], heap[idx]);
i = idx;
}
else if(right != -1){
ll idx = i;
if(!flag) {
if(heap[right] > heap[idx]) idx = right;
}
else {
if(heap[right] < heap[idx]) idx = right;
}
swap(heap[i], heap[idx]);
i = idx;
}
if(i == prev) break;
prev = i;
}
}
void build() {
for(ll i = (sz >> 1); i < sz; i++) heapify_up(i);
}
public:
Heap(vector<T> &a, bool flag = false) {
this->flag = flag;
this->heap = a;
this->sz = heap.size();
build();
}
void push(ll x) {
if(heap.empty()) {
heap.push_back(x);
}
else {
heap.push_back(x);
ll idx = sz;
++sz;
heapify_up(idx);
}
}
void pop() { heapify_down(); }
ll get() {
if(heap.empty()) return ((flag) ? INT_MIN : INT_MAX);
return heap[0];
}
};
void tests()
{
ll n;
cin >> n;
vi a(n);
for(auto &x: a) cin >> x;
/*
Array a = {4, 9, 3, 10, 5}
You will get 10 as the top of heap and 3, when you create min heap
*/
/*
maxheap by default
For a minheap, do this : Heap<ll> heap(a, 1);
*/
Heap<ll> heap(a);
heap.push(1);
heap.pop();
cout << heap.get();
}
signed main()
{
int tt = 1;
while(tt--)
tests();
return 0;
}