Pagini recente » Cod sursa (job #2063531) | Cod sursa (job #921279) | Cod sursa (job #2297066) | Cod sursa (job #2045550) | Cod sursa (job #1336752)
#include <fstream>
#include <vector>
using namespace std;
ifstream is("heapuri.in");
ofstream os("heapuri.out");
using VI = vector<int>;
class Heap{
public:
Heap() : n(0), cnt(0)
{
h.push_back(0);
nr.push_back(0);
poz.push_back(0);
}
int top()
{
return nr[h[1]];
}
void push(int nod)
{
h.push_back(++cnt);
nr.push_back(nod);
poz.push_back(++n);
up(n);
}
void pop(int pos)
{
pos = poz[pos];
h[pos] = h[n--];
h.pop_back();
poz[h[pos]] = pos;
down(pos);
}
void down(int up)
{
int down = 2 * up;
while ( down <= n )
{
if ( down < n && nr[h[down + 1]] < nr[h[down]] )
++down;
if ( nr[h[up]] < nr[h[down]] )
return;
swap(poz[h[up]], poz[h[down]]);
swap(h[up], h[down]);
up = down;
down *= 2;
}
}
void up(int down)
{
int up = down / 2;
while ( up && nr[h[down]] < nr[h[up]] )
{
swap(poz[h[up]], poz[h[down]]);
swap(h[up], h[down]);
down = up;
up /= 2;
}
}
private:
int n, cnt;
VI h, nr, poz;
};
int main()
{
int t, tip, x;
Heap h;
is >> t;
while ( t-- )
{
is >> tip;
if ( tip == 3 )
os << h.top() << "\n";
else
{
is >> x;
if ( tip == 1 )
h.push(x);
else
h.pop(x);
}
}
is.close();
os.close();
return 0;
}