Pagini recente » Borderou de evaluare (job #1835269) | Borderou de evaluare (job #2085295) | Cod sursa (job #556777) | Cod sursa (job #640994) | Cod sursa (job #2295903)
#include <stdio.h>
#include <algorithm>
using namespace std;
int v[200010], heap[200010], pos[200010];
int n,l,nr;
void push(int x){
while(x/2 && v[heap[x]] < v[heap[x/2]])
{
swap(heap[x], heap[x/2]);
pos[heap[x]] = x;
pos[heap[x/2]] = x/2;
x /= 2;
}
}
void pop(int x){
int y = 0;
while(x != y)
{
y = x;
if(y*2 <= l && v[heap[x]] > v[heap[y*2]])
x = y*2;
if(y*2+1 <= l && v[heap[x]] > v[heap[y*2+1]])
x=y*2+1;
swap(heap[y], heap[x]);
pos[heap[x]] = x;
pos[heap[y]] = y;
}
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int c, x, i;
scanf("%d",&n);
for(i = 1;i <=n ; i++)
{
scanf("%d", &c);
if(c < 3)
scanf("%d" ,&x);
if (c == 1)
{
l++, nr++;
v[nr] = x;
heap[l] = nr;
pos[nr] = l;
push(l);
}
if (c == 2)
{
v[x] = -1;
push(pos[x]);
pos[heap[1]] = 0;
heap[1] = heap[l--];
pos[heap[1]] = 1;
if (l > 1)
pop(1);
}
if (c == 3) printf("%d\n", v[heap[1]]);
}
return 0;
}