Pagini recente » Cod sursa (job #41315) | Borderou de evaluare (job #1058815) | Cod sursa (job #3257474) | Cod sursa (job #2805127) | Cod sursa (job #2925338)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int HEAP_SIZE = 200005;
typedef int Heap[HEAP_SIZE];
int insCnt = 0;
int pos[HEAP_SIZE], ins[HEAP_SIZE];
inline int father(int node) {
return node / 2;
}
inline int leftSon(int node) {
return node * 2;
}
inline int rightSon(int node) {
return node * 2 + 1;
}
void sift(Heap h, int n, int node) {
int son;
do {
son = 0;
if (leftSon(node) <= n) {
son = leftSon(node);
if (rightSon(node) <= n && h[son] > h[rightSon(node)])
son = rightSon(node);
if (h[son] >= h[node])
son = 0;
}
if (son != 0) {
swap(h[node], h[son]);
swap(pos[ins[node]], pos[ins[son]]);
swap(ins[node], ins[son]);
node = son;
}
} while (son != 0);
}
void percolate(Heap h, int n, int node) {
int value = h[node];
int insertedPosition = ins[node];
while (node > 1 && value < h[father(node)]) {
h[node] = h[father(node)];
pos[ins[father(node)]] = node;
ins[node] = ins[father(node)];
node = father(node);
}
h[node] = value;
pos[insertedPosition] = node;
ins[node] = insertedPosition;
}
void insert(Heap h, int& n, int value) {
h[++n] = value;
ins[n] = ++insCnt;
pos[insCnt] = n;
percolate(h, n, n);
}
void cut(Heap h, int& n, int node) {
h[node] = h[n];
pos[ins[n]] = node;
ins[node] = ins[n];
n--;
if (node > 1 && h[node] < h[father(node)])
percolate(h, n, node);
else
sift(h, n, node);
}
int n, heapCnt = 0;
Heap heap;
int main() {
fin >> n;
for (int i = 1; i <= n; i++) {
int cmd;
fin >> cmd;
if (cmd == 1) {
int x;
fin >> x;
insert(heap, heapCnt, x);
} else if (cmd == 2) {
int x;
fin >> x;
cut(heap, heapCnt, pos[x]);
} else {
fout << heap[1] << '\n';
}
}
return 0;
}