Pagini recente » Cod sursa (job #534299) | Cod sursa (job #1860434) | Cod sursa (job #2815518) | Cod sursa (job #2717352) | Cod sursa (job #1323835)
#include <cstdio>
#include <algorithm>
using namespace std;
#define NMAX 200010
#define V(x) Heap[x].val
#define O(x) Heap[x].ord
int N;
int Ord[NMAX];
struct
{
int val;
int ord;
} Heap[NMAX];
inline bool CMP(int a, int b)
{
return V(a)<V(b);
}
inline int father(int n)
{
return n>>1;
}
inline int left_son(int n)
{
return n<<1;
}
inline int right_son(int n)
{
return (n<<1)+1;
}
inline void Swap(int a, int b)
{
swap(V(a),V(b));
swap(O(a),O(b));
swap(Ord[O(a)],Ord[O(b)]);
}
void Up(int n)
{
while(n > 1 && CMP(n,father(n)))
{
Swap(n,father(n));
n = father(n);
}
}
void Down(int n)
{
int son;
do
{
son = 0;
if(left_son(n) <= N)
{
son = left_son(n);
if(right_son(n) <= N && CMP(right_son(n),son))
son = right_son(n);
if(CMP(n,son))
son = 0;
else
Swap(n,son);
}
n = son;
}while(n);
}
void Insert (int x)
{
N++;
V(N)=x;
O(N)=N;
Ord[N]=N;
Up(N);
}
void RemoveAt (int x)
{
Swap(x,N);
N--;
if(x > 1 && CMP(x,father(x)))
Up(x);
else
Down(x);
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int M,x,op;
scanf("%d",&M);
while(M--)
{
scanf("%d",&op);
switch(op)
{
case 1:
{
scanf("%d",&x);
Insert(x);
break;
}
case 2:
{
scanf("%d",&x);
RemoveAt(Ord[x]);
break;
}
case 3:
{
printf("%d\n",Heap[1]);
break;
}
}
}
return 0;
}