Pagini recente » Cod sursa (job #2794992) | Cod sursa (job #2279983) | Cod sursa (job #3126957) | Cod sursa (job #1718604) | Cod sursa (job #386725)
Cod sursa(job #386725)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on January 25, 2010, 6:05 PM
*/
#include <vector>
#include <fstream>
#define Nmax 200000
/*
* Create a min-heap
*/
using namespace std;
int N=-1;
int Heap[Nmax], Position[Nmax];
inline int father( int k )
{
return k/2;
}
inline int left_son( int k )
{
return 2*k;
}
inline int right_son( int k )
{
return 2*k+1;
}
inline void swap( int& x, int& y )
{
x+=y;
y=x-y;
x-=y;
}
int sift( int k )
{
int son;
while( true )
{
son=left_son(k);
if( son > N )
break;
if( right_son(k) && Heap[right_son(k)] < Heap[son] )
son=right_son(k);
if( Heap[son] >= Heap[k] )
break;
swap( Heap[son], Heap[k] );
k=son;
}
return k;
}
int percolate( int k )
{
int key=Heap[k], f=father(k);
while( k > 0 && key < Heap[f] )
{
Heap[k]=Heap[f];
k=f;
f=father(k);
}
Heap[k]=key;
return k;
}
void cut( int k )
{
k=Position[k];
Heap[k]=Heap[N];
--N;
if( k > 0 && Heap[k] < Heap[father(k)] )
Position[k]=percolate( k );
else Position[k]=sift( k );
}
void insert( int k )
{
Heap[++N]=k;
Position[N]=percolate( N );
}
int main()
{int n, i, x, y;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
in>>n;
for( i=0; i < n; ++i )
{
in>>x;
if( x < 3 )
{
in>>y;
if( 1 == x )
insert( y );
else cut( y );
}
else out<<Heap[0]<<'\n';
}
return 0;
}