Pagini recente » Cod sursa (job #442157) | Cod sursa (job #2227105) | Cod sursa (job #2953390) | Cod sursa (job #2150649) | Cod sursa (job #2839697)
#include <bits/stdc++.h>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int V = 200005;
int h[V],v[V],pos[V],cnt;
void swapp(int a,int b)
{
swap(h[a],h[b]);
pos[h[a]] = a;
pos[h[b]] = b;
}
void upheap(int p)
{
while (v[h[p]] < v[h[p / 2]])
{
swapp(p,p / 2);
p /= 2;
}
}
void downheap(int p)
{
int fs = 2 * p,fd = 2 * p + 1,fu = p;
if (fs <= cnt and v[h[fs]] < v[h[fu]])
fu = fs;
if (fd <= cnt and v[h[fd]] < v[h[fu]])
fu = fd;
if (fu != p)
{
swapp(p,fu);
downheap(fu);
}
}
void sterge(int p)
{
if (p == cnt)
cnt--;
else
{
swapp(p,cnt--);
upheap(p);
downheap(p);
}
}
int main()
{
int n,x,y,p = 0;
in >> n;
for (int i = 1; i <= n; i++)
{
in >> x;
if (x == 3)
out << v[h[1]] << '\n';
else if (x == 1)
{
in >> v[++p];
pos[p] = p;
h[++cnt] = p;
upheap(cnt);
}
else
{
in >> y;
sterge(pos[y]);
}
}
return 0;
}