Pagini recente » Cod sursa (job #1725792) | Cod sursa (job #2980814) | Cod sursa (job #2775749) | Cod sursa (job #2983196) | Cod sursa (job #386711)
Cod sursa(job #386711)
/*
* File: main.cpp
* Author: virtualdemon
*
* Created on January 25, 2010, 6:05 PM
*/
#include <vector>
#include <fstream>
/*
* Create a min-heap
*/
using namespace std;
typedef unsigned int u;
vector< u > Position;
vector< int > Heap;
inline u father( u k )
{
return k/2;
}
inline u left_son( u k )
{
return 2*k;
}
inline u right_son( u k )
{
return 2*k+1;
}
inline void swap( int& x, int& y )
{
x+=y;
y=x-y;
x-=y;
}
u sift( u k )
{
u N=Heap.size(), son;
while( true )
{
son=left_son(k);
if( son >= N )
break;
if( right_son(k) < N && Heap[right_son(k)] < Heap[left_son(k)] )
son=right_son(k);
if( Heap[k] >= Heap[son] ) //it has min-heap property
break;
swap( Heap[k], Heap[son] );
k=son;
}
return k;
}
u percolate( u k )
{
int key=Heap[k];
while( k > 0 && key < Heap[father(k)] )
{
Heap[k]=Heap[father(k)];
k=father(k);
}
Heap[k]=key;
return k;
}
/*
* void make_heap( u N )
* {
* for( u k=N/2; u > 0; --k )
* sift( N, k );
* sift( N, 0 );
* }
*/
void cut( int k )
{
Heap[ Position[k] ]=Heap.back();
Heap.pop_back();
Position[k]=sift( k );
}
void insert( int x )
{
Heap.push_back(x);
Position.push_back( percolate( Heap.size()-1 ) );
}
int main()
{int y;
u n, i, x;
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;
}