Pagini recente » Cod sursa (job #2594589) | Cod sursa (job #1681181) | Cod sursa (job #1680961) | Cod sursa (job #1016175) | Cod sursa (job #3290084)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct cel
{
int val, poz;
}heap[200001];
int chrono[200001];
int n, nr;
void aux ( cel &a, cel &b )
{
cel auxi = a;
a = b;
b = auxi;
}
void push( int nod )
{
if ( nod > 1 && heap[nod].val < heap[nod / 2].val )
{
chrono[heap[nod].poz] = nod / 2;
chrono[heap[nod / 2].poz] = nod;
aux ( heap[nod], heap[nod / 2] );
push ( nod / 2 );
}
}
/* void pull ( int nod )
{
if ( nod * 2 <= n && ( heap[nod].val > heap[nod * 2].val || heap[nod].val > heap[nod * 2 + 1].val ) )
{
int cnt = 1;
if ( nod * 2 + 1 > n || heap[nod * 2].val < heap[nod * 2 + 1].val )
cnt = 0;
chrono[heap[nod].poz] = nod * 2 + cnt;
chrono[heap[nod * 2 + cnt].poz] = nod;
aux ( heap[nod], heap[nod * 2 + cnt] );
pull ( nod * 2 + cnt );
}
} */
void pull ( int nod )
{
int p = nod;
if ( nod * 2 <= n && heap[p].val > heap[2 * nod].val )
p = 2 * nod;
if ( nod * 2 + 1 <= n && heap[p].val > heap[2 * nod + 1].val )
p = 2 * nod + 1;
if ( p != nod )
{
chrono[heap[nod].poz] = p;
chrono[heap[p].poz] = nod;
aux ( heap[nod], heap[p] );
pull (p);
}
}
void afisare()
{
int k = 1;
for ( int i = 1; i <= n; i ++ )
{
cout << heap[i].val << " ";
if ( ( i + 1 ) % k == 0 )
{
cout << endl;
k *= 2;
}
}
cout << endl << endl;
}
int main()
{
int q, nr_3 = 0;
f >> q;
while ( q -- )
{
int c, x;
if ( q == 912 )
int var = 1;
f >> c;
if ( c == 1 )
{
f >> x;
if ( x == 10268 )
int var = 1;
nr ++;
n ++;
heap[n].val = x;
heap[n].poz = nr;
chrono[nr] = n;
push(n);
}
else if ( c == 2 )
{
f >> x;
if ( x == 25 )
int var = 1;
int crx = chrono[x];
if ( crx == n )
{
heap[n].val = 0;
n --;
}
else
{
chrono[heap[n].poz] = chrono[heap[crx].poz];
chrono[x] = n;
aux ( heap[crx], heap[n] );
heap[n].val = 0;
n --;
pull ( crx );
push ( crx );
}
}
if ( c == 3 )
{
nr_3 ++;
if ( nr_3 == 41 )
int var = 1;
g << heap[1].val << '\n';
}
}
return 0;
}