-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpriority-queue.cpp
More file actions
84 lines (76 loc) · 1.73 KB
/
priority-queue.cpp
File metadata and controls
84 lines (76 loc) · 1.73 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
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<pair<int, int> > a;
vector<int> pos;
void siftUp(int v){
while(v) {
if(a[v].first < a[(v - 1) / 2].first) {
swap(pos[ a[v].second ], pos[ a[(v - 1) / 2].second ]);
swap(a[v], a[(v - 1) / 2]);
v = (v - 1) / 2;
}
else
break;
}
}
void siftDown(int v, int n) {
while(2 * v + 1 < n) {
int l = 2 * v + 1;
int r = 2 * v + 2;
int j = l;
if(r < n && a[r].first < a[l].first) {
j = r;
}
if(a[v].first < a[j].first) {
break;
}
swap(pos[ a[v].second ], pos[ a[j].second ]);
swap(a[v], a[j]);
v = j;
}
}
void push(int x, int operation){
a.push_back(make_pair(x, operation));
pos[operation] = (int)a.size() - 1;
siftUp((int)a.size() - 1);
}
void extract() {
if(a.empty())
cout << "*" << endl;
else{
cout << a[0].first << endl;
swap(a[0], a[(int)a.size() - 1]);
pos[a[0].second] = 0;
a.pop_back();
siftDown(0, (int)a.size());
}
}
void key(int x, int y){
int i = pos[x];
a[i].first = y;
siftUp(i);
}
int main() {
freopen("priorityqueue.in", "r", stdin);
freopen("priorityqueue.out", "w", stdout);
string s;
int operation = 0;
pos.resize(1e6 + 100);
while (cin >> s) {
if (s == "push") {
int x;
cin >> x;
push(x, operation);
} else if (s == "extract-min") {
extract();
} else {
int x, y;
cin >> x >> y;
x--;
key(x, y);
}
operation++;
}
}