-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdatable.cpp
More file actions
75 lines (67 loc) · 1.61 KB
/
Updatable.cpp
File metadata and controls
75 lines (67 loc) · 1.61 KB
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
#include <iostream>
#include <queue>
#include <utility>
template <typename T>
class UpdatablePriorityQueue {
std::vector<T> heap;
std::vector<int> pos;
public:
UpdatablePriorityQueue(std::vector<T> v) {
heap = v;
pos.resize(heap.size());
for (int i = 0; i < heap.size(); i++) {
pos[i] = i;
}
for (int i = heap.size() / 2 - 1; i >= 0; i--) {
siftDown(i);
}
}
void siftDown(int i) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int smallest = i;
if (left < heap.size() && heap[left] < heap[smallest]) {
smallest = left;
}
if (right < heap.size() && heap[right] < heap[smallest]) {
smallest = right;
}
if (smallest != i) {
std::swap(heap[i], heap[smallest]);
std::swap(pos[heap[i]], pos[heap[smallest]]);
siftDown(smallest);
}
}
void siftUp(int i) {
while (i > 0 && heap[i] < heap[(i - 1) / 2]) {
std::swap(heap[i], heap[(i - 1) / 2]);
std::swap(pos[heap[i]], pos[heap[(i - 1) / 2]]);
i = (i - 1) / 2;
}
}
void update(int i, T value) {
heap[pos[i]] = value;
siftUp(pos[i]);
siftDown(pos[i]);
}
void insert(T value) {
heap.push_back(value);
pos.push_back(heap.size() - 1);
siftUp(heap.size() - 1);
}
T extractMin() {
T result = heap[0];
std::swap(heap[0], heap[heap.size() - 1]);
std::swap(pos[heap[0]], pos[heap[heap.size() - 1]]);
heap.pop_back();
pos.pop_back();
siftDown(0);
return result;
}
void print() {
for (int i = 0; i < heap.size(); i++) {
std::cout << heap[i] << " ";
}
std::cout << std::endl;
}
};