Pagini recente » Cod sursa (job #800640) | Cod sursa (job #2377649) | Cod sursa (job #1858316) | Cod sursa (job #826955) | Cod sursa (job #1168086)
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int NMAX = 200000+5;
const int INF = (1LL<<31)-1;
void Read(),Solve();
class Heap
{
int Dim,Tot;
int H[NMAX];
int P[NMAX];
int V[NMAX];
public:
Heap();
int top();
void insert(int);
void erase(int);
private:
void Heap_Down(int);
void Heap_Up(int);
};
Heap::Heap()
{
Dim = Tot = 0;
memset(H,0,sizeof(H));
memset(P,0,sizeof(P));
memset(V,0,sizeof(V));
}
int Heap::top()
{
return V[H[1]];
}
void Heap::insert(int value)
{
Dim++;
Tot++;
H[Dim] = Tot;
P[Tot] = Dim;
V[Tot] = value;
Heap_Up(Dim);
}
void Heap::erase(int position)
{
int x = P[position];
V[position] = INF;
Heap_Down(x);
Dim--;
}
void Heap::Heap_Down(int p)
{
if(2*p > Dim) return;
int f = 2 * p;
if(V[H[f]] > V[H[f+1]] && f+1 <= Dim) f++;
if(V[H[f]] < V[H[p]])
{
swap(H[f],H[p]);
swap(P[H[f]],P[H[p]]);
Heap_Down(f);
}
}
void Heap::Heap_Up(int f)
{
if(f == 1) return;
int p = f / 2;
if(V[H[f]] < V[H[p]])
{
swap(H[f],H[p]);
swap(P[H[f]],P[H[p]]);
Heap_Up(p);
}
}
int N;
Heap H;
int main()
{
Read();
Solve();
return 0;
}
void Read()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&N);
}
void Solve()
{
int t,x;
for(; N; --N)
{
scanf("%d",&t);
if(t == 1)
{
scanf("%d",&x);
H.insert(x);
}
else if(t == 2)
{
scanf("%d",&x);
H.erase(x);
}
else printf("%d\n",H.top());
}
}