Pagini recente » Cod sursa (job #700769) | Cod sursa (job #1796917) | Cod sursa (job #2416985) | Cod sursa (job #1322291) | Cod sursa (job #1490872)
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
using namespace std;
ifstream is("heapuri.in");
ofstream os("heapuri.out");
int n, el;
int h[200001], nr[200001], p[200001];
void Read();
void Insert(int x);
void Delete(int poz);
int main()
{
Read();
is.close();
os.close();
return 0;
}
void Read()
{
int m, tip, x;
is >> m;
for ( int i = 1; i <= m; ++i )
{
is >> tip;
if ( tip != 3 )
{
is >> x;
if ( tip == 1 )
Insert(x);
else
Delete(p[x]);
}
else
os << nr[h[1]] << "\n";
}
/* for ( int i = 1; i <= el; ++i )
os << p[h[i]] << " " << h[i] << " " << nr[h[i]] << "\n";
os << "\n";*/
}
void Delete(int poz)
{
if ( poz == n )
{
--n;
return;
}
h[poz] = h[n--];
p[h[poz]] = poz;
int up = poz, down = 2 * poz;
while ( ( down <= n && nr[h[up]] > nr[h[down]] ) || ( down < n && nr[h[up]] > nr[h[down + 1]] ) )
{
if ( down < n && nr[h[down]] > nr[h[down + 1]] )
++down;
swap(h[up], h[down]);
swap(p[h[up]], p[h[down]]);
up = down;
down *= 2;
}
}
void Insert(int x)
{
h[++n] = ++el;
p[el] = n;
nr[h[n]] = x;
int down = n, up = n / 2;
while ( up && nr[h[down]] < nr[h[up]] )
{
swap(h[down], h[up]);
swap(p[h[down]], p[h[up]]);
down = up;
up /= 2;
}
}