Pagini recente » Cod sursa (job #1338308) | Cod sursa (job #198327) | Cod sursa (job #31323) | Cod sursa (job #381300) | Cod sursa (job #2924586)
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int HEAP_SIZE = 200005;
typedef int Heap[HEAP_SIZE];
int n, heapCnt = 0, ins = 0;
Heap heap;
int pos[HEAP_SIZE], inserted[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 cnt, int node) {
int son;
do {
son = 0;
if (leftSon(node) <= cnt) {
son = leftSon(node);
if (rightSon(node) <= cnt && h[rightSon(node)] < h[son])
son = rightSon(node);
if (h[son] >= h[node])
son = 0;
}
if (son != 0) {
swap(h[node], h[son]);
swap(pos[inserted[node]], pos[inserted[son]]);
swap(inserted[node], inserted[son]);
node = son;
}
} while (son != 0);
}
void percolate(Heap h, int cnt, int node) {
int key = h[node];
while (node > 1 && key < h[father(node)]) {
h[node] = h[father(node)];
pos[inserted[father(node)]] = node;
inserted[node] = inserted[father(node)];
node = father(node);
}
h[node] = key;
inserted[node] = ins;
pos[ins] = node;
}
void insert(Heap h, int& cnt, int key) {
h[++cnt] = key;
inserted[cnt] = ins;
pos[ins] = cnt;
percolate(h, cnt, cnt);
}
void cut(Heap h, int& cnt, int node) {
h[node] = h[cnt--];
if (node > 1 && h[node] < h[father(node)])
percolate(h, cnt, node);
else
sift(h, cnt, node);
}
int main() {
fin >> n;
for (int i = 1; i <= n; i++) {
int cmd;
fin >> cmd;
if (cmd == 1) {
int x;
fin >> x;
ins++;
insert(heap, heapCnt, x);
} else if (cmd == 2) {
int x;
fin >> x;
cut(heap, heapCnt, pos[x]);
} else {
fout << heap[1] << '\n';
}
}
return 0;
}