Pagini recente » Cod sursa (job #1908059) | Cod sursa (job #2337257) | Cod sursa (job #2811691) | Cod sursa (job #2892628) | Cod sursa (job #2189512)
#include <bits/stdc++.h>
#define NM 200002
using namespace std;
int n, h[NM], m, o[NM];
long long v[NM];
void fix2 (int k)
{
//cout << k << " ";
if(k < 1)
return;
if(k / 2 >= 1 && v[h[k]] < v[h[k / 2]])
{
swap(o[h[k]], o[h[k / 2]]);
swap(h[k], h[k / 2]);
fix2(k / 2);
}
}
void fixinsert ()
{
int k = m;
fix2(k);
}
void fix1 (int k)
{
int x = k * 2;
if(x > m)
return;
if(x + 1 <= m && v[h[x + 1]] < v[h[x]])
x++;
if(v[h[k]] > v[h[x]])
{
swap(o[h[k]], o[h[x]]);
swap(h[k], h[x]);
fix1(x);
}
}
void fixdelete (int k)
{
o[h[m]] = k;
swap(h[k], h[m]);
m--;
fix1(k);
//cout << "\n";
}
int main()
{
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
fin >> n;
for(int i = 1, y = 0; i <= n; i++)
{
int op;
fin >> op;
if(op == 3)
fout << v[h[1]] << "\n";
else if(op == 1)
{
y++;
fin >> v[y];
m++;
o[y] = m;
h[m] = y;
fixinsert();
}
else if(op == 2)
{
int x;
fin >> x;
fixdelete(o[x]);
}
/*
for(int i = 1; i <= m; i++)
fout << v[h[i]] << " ";
fout << "\n";*/
}
return 0;
}