Pagini recente » Cod sursa (job #1083942) | Cod sursa (job #1872107) | Cod sursa (job #2895927) | Cod sursa (job #839810) | Cod sursa (job #2543050)
#include <bits/stdc++.h>
#define nmax 400005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int indice_in_heap[nmax], heap[nmax], valori[nmax], heap_len;
void adaugare(int j)
{
heap_len++;
int pos1 = heap_len;
int pos2 = (pos1 >> 1);
heap[pos1] = j, indice_in_heap[j] = pos1;
while(pos2 > 0 && valori[heap[pos2]] > valori[heap[pos1]])
{
indice_in_heap[heap[pos2]] = pos1;
indice_in_heap[heap[pos1]] = pos2;
swap(heap[pos1], heap[pos2]);
pos1 = pos2;
pos2 = (pos2 >> 1);
}
}
void sterge(int pos)
{
int pos2 = (pos << 1);
heap[pos] = heap[heap_len];
heap[heap_len] = 0;
heap_len--;
indice_in_heap[heap[pos]] = pos;
while(pos2 <= heap_len)
{
if(valori[heap[pos2]] > valori[heap[pos2 + 1]] && pos2 < heap_len)
pos2++;
if(valori[heap[pos2]] < valori[heap[pos]])
indice_in_heap[heap[pos2]] = pos, indice_in_heap[heap[pos]] = pos2, swap(heap[pos], heap[pos2]), pos = pos2, pos2 = (pos << 1);
else
pos2 = heap_len + 2;
}
}
int main()
{
heap[0] = 2000000000;
int i, j = 1, x, op, n, b, r = 0;
fin >> n;
for(i = 0; i < n; i++)
{
fin >> op;
if(op < 3)
{
fin >> x;
if(op == 1)
{
valori[j] = x;
adaugare(j);
++j;
}
else
{
if (indice_in_heap[x] == heap_len)
heap[heap_len] = 0, heap_len--;
else sterge(indice_in_heap[x]);
}
}
else
r++, fout << valori[heap[1]] << '\n';
}
return 0;
}