Pagini recente » Cod sursa (job #190465) | Cod sursa (job #1627548) | Cod sursa (job #60706) | Cod sursa (job #818238) | Cod sursa (job #2177720)
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int MAXN = 200005;
int n;
int v[MAXN], p[MAXN], o[MAXN];
inline int parent(int nod) {
return nod / 2;
}
inline int leftChild(int nod) {
return nod * 2;
}
inline int rightChild(int nod) {
return nod * 2 + 1;
}
inline void swapNode(int a, int b) {
swap(v[a], v[b]);
swap(p[a], p[b]);
o[p[a]] = a;
o[p[b]] = b;
}
void upHeap(int nod) {
while (nod > 1 && v[parent(nod)] > v[nod]) {
swapNode(parent(nod), nod);
nod = parent(nod);
}
}
void downHeap(int nod) {
int child = 1;
while (child > 0) {
child = 0;
if (leftChild(nod) <= n) {
if (v[leftChild(nod)] < v[nod]) {
child = leftChild(nod);
}
}
if (rightChild(nod) <= n) {
if (v[rightChild(nod)] < v[nod]) {
if (child == 0 || v[leftChild(nod)] < v[rightChild(nod)]) {
child = rightChild(nod);
}
}
}
if (child != 0) {
swapNode(child, nod);
nod = child;
}
}
}
void add(int val, int ord) {
n++;
v[n] = val;
p[n] = ord;
o[ord] = n;
upHeap(n);
}
void del(int nod) {
swapNode(nod, n);
n--;
if (parent(nod) >= 1 && v[nod] < v[parent(nod)]) {
upHeap(nod);
}
else if (leftChild(nod) <= n) {
downHeap(nod);
}
}
int main() {
int q;
fin >> q;
int tr = 0;
for (int var = 1; var <= q; ++var) {
int t;
fin >> t;
if (t == 3) {
fout << v[1] << '\n';
}
else {
int k;
fin >> k;
if (t == 1) {
add(k, ++tr);
}
else {
del(o[k]);
}
}
}
fout.close();
return 0;
}