Pagini recente » Cod sursa (job #2490806) | Cod sursa (job #368092) | Cod sursa (job #551941) | Cod sursa (job #2585332) | Cod sursa (job #2897294)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 200005;
int val[N], heap[N], pos[N], n, op, arg, nh, t; //nh - number of nodes in the heap, t - the time of the last added item
void moveUp(int poz) {
while(poz / 2 > 0 && val[heap[poz / 2]] > val[heap[poz]]) {
swap(heap[poz / 2], heap[poz]);
swap(pos[heap[poz]], pos[heap[poz / 2]]);
poz /= 2;
}
}
void moveDown(int poz) {
while(true) {
int child_pos1 = poz * 2, child_pos2 = child_pos1 + 1, min_child_pos = poz;
if(child_pos1 <= nh && val[heap[poz]] > val[heap[child_pos1]])
min_child_pos = child_pos1;
if(child_pos2 <= nh && val[heap[poz]] > val[heap[child_pos2]])
min_child_pos = child_pos2;
if(min_child_pos == poz)
break;
swap(heap[poz], heap[min_child_pos]);
swap(pos[heap[poz]], pos[heap[min_child_pos]]);
}
}
void push(int x) {
val[++ t] = x;
heap[++ nh] = t;
pos[t] = nh;
moveUp(nh);
}
void pop(int time) {
val[time] = -1;
moveUp(pos[time]);
pos[heap[1]] = 0;
heap[1] = heap[nh --];
pos[heap[1]] = 1;
if(nh > 1)
moveDown(1);
}
//void test(){
// ifstream in1("grader_test5.ok");
// ifstream in2("heapuri.out");
// int line = 0;
// int x, y;
// while(in1 >> x){
// in2 >> y;
// ++ line;
// if(x != y){
// cout << "NU " << line << endl;
// }
// }
// cout << "\nDA";
//}
int main() {
in >> n;
while(n --) {
in >> op;
if(op == 1) {
in >> arg;
push(arg);
}
else if(op == 2) {
in >> arg;
pop(arg);
}
else if(op == 3){
out << val[heap[1]] << '\n';
}
}
out.close();
// test();
return 0;
}