Pagini recente » Cod sursa (job #1968012) | Cod sursa (job #847988) | Cod sursa (job #2926120) | Cod sursa (job #1708793) | Cod sursa (job #2945055)
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
const int N=200000;
/// minheap
typedef int Heap[N+1];
Heap H;
inline int father(int nod)
{
return nod/2;
}
inline int leftson(int nod)
{
return nod*2;
}
inline int rightson(int nod)
{
return nod*2+1;
}
inline int top(Heap H)
{
return H[1];
}
void sift(Heap H, int N,int K)
{
int son;
do
{
son = 0;
if(leftson(K)<=N)
{
son = leftson(K);
if(rightson(K)<=N&&H[rightson(K)]<H[leftson(K)])
son = rightson(K);
if(H[son]>= H[K])
son = 0;
}
if(son)
swap(H[K],H[son]),K=son;
} while(son);
}
void percolate(Heap H,int N,int K)
{
int key = H[K];
while(K>1&&key < H[father(K)])
{
H[K] = H[father(K)];
K = father(K);
}
H[K] = key;
}
void build_heap(Heap H,int N)
{
for(int i = N/2;i>0;--i)
sift(H,N,i);
}
void cut(Heap H,int& N,int K)
{
H[K] = H[N];
--N;
if(K>1 && H[father(K)]>H[K])
percolate(H,N,K);
else sift(H,N,K);
}
void insert(Heap H,int& N,int K)
{
H[++N] = K;
percolate(H,N,N);
}
int main()
{
int v[200001],N=0,poz;
int cer,x;
vector<int> ans;
int n;
cin>>n;
while(n--)
{
cin>>cer;
if(cer==1)
{
cin>>x;
v[N+1] = x;
insert(H,N,x);
}
else if(cer==2)
{
cin>>x;
poz = v[x];
cut(H,N,poz);
}
else
{
ans.push_back(top(H));
cut(H,N,1);
}
}
for(auto i : ans)cout<<i<<'\n';
return 0;
}