Pagini recente » Cod sursa (job #2513068) | Cod sursa (job #2830082) | Cod sursa (job #2200353) | Cod sursa (job #1968277) | Cod sursa (job #2553181)
#include <bits/stdc++.h>
using namespace std;
/****************************************************/
const int nmax=200004;
int heap[nmax];
int where[nmax],cronological[nmax];
int orderOfNum;
int heapN;
int n;
/****************************************************/
ifstream f("heapuri.in");
ofstream g("heapuri.out");
/****************************************************/
///--------------------------------------------------------------
inline void readInput()
{
f>>n;
}
///--------------------------------------------------------------
void upcheck(int pos)
{
while(pos/2!=0 && heap[pos]<heap[pos/2])
{
swap(heap[pos], heap[pos/2]);
swap(where[cronological[pos]],where[cronological[pos/2]]);
swap(cronological[pos], cronological[pos/2]);
pos/=2;
}
}
///--------------------------------------------------------------
void downcheck(int pos)
{
while(1)
{
int minim=heap[pos];
int TheCase=0;
if(minim>heap[pos*2] && pos*2<=heapN)
{
minim=heap[pos*2];
TheCase=1;
}
if(minim>heap[pos*2+1] && pos*2+1<=heapN)
{
minim=heap[pos*2+1];
TheCase=2;
}
if(TheCase==0) break;
else
{
if(TheCase==1)
{
swap(heap[pos], heap[pos*2]);
swap(where[cronological[pos]],where[cronological[pos*2]]);
swap(cronological[pos], cronological[pos*2]);
pos*=2;
}
else
{
swap(heap[pos], heap[pos*2+1]);
swap(where[cronological[pos]],where[cronological[pos*2+1]]);
swap(cronological[pos], cronological[pos*2+1]);
pos=pos*2+1;
}
}
}
}
///--------------------------------------------------------------
void removeFromHeap(int pos)
{
pos=where[pos];
swap(heap[pos], heap[heapN]);
swap(where[cronological[pos]],where[cronological[heapN]]);
swap(cronological[pos], cronological[heapN]);
--heapN;
downcheck(heapN);
upcheck(heapN);
}
///--------------------------------------------------------------
void addToHeap(int val)
{
heap[++heapN] =val;
cronological[heapN] = ++orderOfNum;
where[orderOfNum] = heapN;
upcheck(heapN);
}
///--------------------------------------------------------------
inline void Solution()
{
int x,y;
for(;n>0;n--)
{
f>>x;
if(x==1)
{
f>>y;
addToHeap(y);
}
else
{
if(x==2)
{
f>>y;
removeFromHeap(y);
}
else g<<heap[1]<<"\n";
}
}
}
///--------------------------------------------------------------
int main()
{
readInput();
Solution();
return 0;
}