Pagini recente » Cod sursa (job #2277231) | Cod sursa (job #1322585) | Cod sursa (job #2775896) | Cod sursa (job #1994946) | Cod sursa (job #3181514)
#include <bits/stdc++.h>
#define NM 200005
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
int t, c, x;
int h[NM], pos[NM], whatPos[NM];
int hSize = 0, nrAdded = 0;
void schimba(int a, int b)
{
swap(h[a], h[b]);
swap(pos[whatPos[a]], pos[whatPos[b]]);
swap(whatPos[a], whatPos[b]);
}
void add(int k)
{
h[++hSize] = k;
whatPos[hSize] = nrAdded;
pos[nrAdded] = hSize;
int a = hSize;
while(a > 0 && h[a / 2] > h[a])
{
schimba(a, a / 2);
a /= 2;
}
}
int top() { return h[1]; }
void del(int k)
{
k = pos[k];
schimba(k, hSize);
hSize--;
int a;
while(k * 2 <= hSize && (h[k] > h[k * 2] || h[k] > h[k * 2 + 1]))
{
if(k * 2 + 1 > hSize)
a = k * 2;
else
a = (h[k * 2] < h[k * 2 + 1] ? k * 2 : k * 2 + 1);
if(h[k] < h[a])
break;
schimba(k, a);
k = a;
}
}
int main()
{
fin >> t;
for(; t; t--)
{
fin >> c;
switch(c)
{
case 1:
fin >> x;
nrAdded++;
add(x);
break;
case 2:
fin >> x;
del(x);
break;
case 3:
fout << top() << '\n';
break;
}
}
return 0;
}