Pagini recente » Cod sursa (job #1015981) | Cod sursa (job #1267559) | Cod sursa (job #2365081) | Cod sursa (job #156478) | Cod sursa (job #2424237)
#include <bits/stdc++.h>
#define NM 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int k,poz,heap[NM][3],pozitii[NM];
void UpHeap(int poz)
{ int Father=poz/2;
if(Father>1 && heap[poz][1]<heap[Father][1])
{ swap(heap[poz],heap[Father]);
UpHeap(Father);
}
}
void DownHeap(int poz)
{ int son=poz*2;
if(son+1<=k && heap[son+1][1]<heap[son][1]) son++;
if(son<=k && heap[son][1]<heap[poz][1])
{ swap(heap[son],heap[poz]);
DownHeap(son);
}
}
void InsertHeap(int x)
{ k++;
heap[k][1]=x;
heap[k][2]=poz;
pozitii[poz]=k;
UpHeap(k);
}
void DeleteHeap(int x)
{ int poz=pozitii[x];
heap[poz][1]=heap[k][1];
heap[poz][2]=heap[k][2];
pozitii[heap[k][2]]=poz;
k--;
int Father=poz/2;
if(poz>1 && heap[poz][1]<heap[Father][1]) UpHeap(poz);
else DownHeap(poz);
}
int main()
{ int n;
f>>n;
while(n--)
{ int t;
f>>t;
if(t==1)
{ poz++;
int x;
f>>x;
InsertHeap(x);
}
else
if(t==2)
{ int x;
f>>x;
DeleteHeap(x);
}
else g<<heap[1][1]<<'\n';
}
return 0;
}