Pagini recente » Cod sursa (job #2899323) | Cod sursa (job #1275654) | Cod sursa (job #1997156) | Cod sursa (job #1418433) | Cod sursa (job #3163259)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ( "heapuri.in" );
ofstream fout ( "heapuri.out" );
const int N = 3e5;
int n;
int poz[N];
struct heapStruct {
int val;
int poz;
} heap[N];
int heapAdd ( int x, int k ) {
heap[k].val = x;
heap[k].poz = k;
poz[k] = k;
while ( heap[k].val < heap[k/2].val && k > 1 ) {
swap ( poz[heap[k].poz], poz[heap[k/2].poz] );
swap ( heap[k], heap[k/2] );
k = k / 2;
}
return 0;
}
int heapErase ( int x, int k ) {
swap ( heap[x], heap[k] );
poz[heap[x].poz] = x;
poz[heap[x].poz] = 0;
while ( x * 2 < k ) {
if ( heap[x].val > heap[x*2].val ) {
swap ( poz[heap[x].poz], poz[heap[x*2].poz] );
swap ( heap[x], heap[x*2] );
}
else if ( heap[x].val > heap[x*2 + 1].val ) {
swap ( poz[heap[x].poz], poz[heap[x*2+1].poz] );
swap ( heap[x], heap[x*2+1] );
}
x = x * 2;
}
return 0;
}
int main() {
int t, cer, x;
fin >> t;
for ( int i = 0; i < t; i++ ) {
fin >> cer;
if ( cer == 1 ) {
fin >> x;
n++;
heapAdd (x, n);
}
else if ( cer == 2 ) {
fin >> x;
heapErase (poz[x], n);
n--;
}
else
fout << heap[1].val << "\n";
}
return 0;
}