-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQHEAP1.cpp
69 lines (62 loc) · 1.52 KB
/
QHEAP1.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
#include <iostream>
#include <algorithm>
#define ll long long int
using namespace std;
void minHeapify(ll * heap, int index, int size){
int minIndex = index;
int left = 2*index + 1;
int right = 2*index + 2;
if(left < size && heap[index] > heap[left]) minIndex = left;
if(right < size && heap[index] > heap[left]) minIndex = right;
if(minIndex != index){
swap(heap[index], heap[minIndex]);
minHeapify(heap, minIndex, size);
}
}
void shiftUp(ll * heap, int index){
if(index == 0) return;
int parentIndex = (index - 1) / 2;
if(heap[index] < heap[parentIndex]){
swap(heap[index], heap[parentIndex]);
shiftUp(heap, parentIndex);
}
}
void insertValue(ll * heap, ll v, int size){
if(size == 0){
heap[0] = v;
}
else{
heap[size] = v;
shiftUp(heap, size);
}
}
void deleteValue(ll * heap, ll v, int size){
swap(*find(heap, heap + size, v), heap[size - 1]);
--size;
for(int i = size/2 - 1; i >= 0; --i){
minHeapify(heap, i, size);
}
}
int main() {
int Q, choice, size = 0;
ll v;
cin>>Q;
ll heap[Q];
while(Q--){
cin>>choice;
switch(choice){
case 1:
cin>>v;
insertValue(heap, v, size++);
break;
case 2:
cin>>v;
deleteValue(heap, v, size--);
break;
case 3:
cout<<heap[0]<<endl;
break;
}
}
return 0;
}