Pagini recente » Cod sursa (job #2095131) | Cod sursa (job #482390) | Cod sursa (job #2076351) | Cod sursa (job #1537740) | Cod sursa (job #2839621)
#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 upheap(int p)
{
while (p > 1 and v[h[p]] < v[h[p / 2]])
{
swap(h[p],h[p / 2]);
swap(pos[h[p]],pos[h[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)
{
pos[h[p]] = fu;
pos[h[fu]] = p;
swap(h[p],h[fu]);
downheap(fu);
}
}
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 >> y;
v[++p] = y;
h[++cnt] = p;
pos[p] = cnt;
upheap(cnt);
}
else
{
in >> y;
swap(h[cnt],h[pos[y]]);
pos[y] = pos[h[pos[y]]];
cnt--;
downheap(pos[y]);
}
}
return 0;
}