Pagini recente » Cod sursa (job #351581) | Cod sursa (job #1426717) | Cod sursa (job #403378) | Cod sursa (job #1441012) | Cod sursa (job #2027654)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int v[200004],total,curent;
pair<int,int> heap[200004];
void fixpoz(int x)
{
int ok=1;
while (x/2!=0&&heap[x].first<heap[x/2].first)
{
v[heap[x].second]=x/2;
v[heap[x/2].second]=x;
swap(heap[x],heap[x/2]);
ok=0;
x/=2;
}
while (2*x<=curent&&ok==1)
{
if (2*x==curent)
{
ok=0;
if (heap[x].first>heap[2*x].first)
{
v[heap[x].second]=2*x;
v[heap[2*x].second]=x;
swap(heap[x],heap[2*x]);
x*=2;
}
}
else if (2*x+1==curent)
{
ok=0;
if (heap[2*x+1].first<heap[2*x].first)
{
if (heap[x].first>heap[2*x+1].first)
{
v[heap[x].second]=2*x+1;
v[heap[2*x+1].second]=x;
swap(heap[x],heap[2*x+1]);
x=2*x+1;
}
}
else if (heap[x].first>heap[2*x].first)
{
v[heap[x].second]=2*x;
v[heap[2*x].second]=x;
swap(heap[x],heap[2*x]);
x=2*x;
}
}
else
{
if (heap[2*x+1].first<heap[2*x].first)
{
if (heap[x].first>heap[2*x+1].first)
{
ok=1;
v[heap[x].second]=2*x+1;
v[heap[2*x+1].second]=x;
swap(heap[x],heap[2*x+1]);
x=2*x+1;
}
}
else if (heap[x].first>heap[2*x].first)
{
ok=1;
v[heap[x].second]=2*x;
v[heap[2*x].second]=x;
swap(heap[x],heap[2*x]);
x=2*x;
}
}
}
}
void ins(int x)
{
total++;
curent++;
v[total]=curent;
heap[curent].first=x;
heap[curent].second=total;
fixpoz(curent);
}
void del(int x)
{
int t=v[x];
v[heap[curent].second]=t;
swap(heap[curent],heap[t]);
curent--;
v[x]=0;
fixpoz(t);
}
int main()
{
int tip,n,i,t;
fin>>n;
for (i=1;i<=n;i++)
{
fin>>tip;
if (tip==1)
{
fin>>t;
ins(t);
tip=0;
}
else if (tip==2)
{
fin>>t;
del(t);
tip=0;
}
else if (tip==3)
{fout<<heap[1].first<<'\n';
tip=0;
}
}
return 0;
}