Pagini recente » Cod sursa (job #1414483) | Cod sursa (job #1405370) | Cod sursa (job #456096) | Cod sursa (job #1042303) | Cod sursa (job #1136210)
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iterator>
#include <random>
#include <assert.h>
using namespace std;
const string file = "heapuri";
const string infile = file + ".in";
const string outfile = file + ".out";
const int INF = 0x3f3f3f3f;
//#define ONLINE_JUDGE
vector<int> heap;
vector<int> poz;
vector<int> elem;
int heapSize;
void initHeap()
{
heap.push_back(0);
heapSize = 0;
}
int lSon(int i)
{
return i * 2;
}
int rSon(int i)
{
return i * 2 + 1;
}
int father(int i)
{
return i / 2;
}
void heapSwap(int a, int b)
{
int aux = heap[a];
heap[a] = heap[b];
heap[b] = aux;
poz[heap[a]] = a;
poz[heap[b]] = b;
}
void heapUp(int i)
{
while(i != 1 && elem[heap[i]] < elem[heap[father(i)]])
{
heapSwap(i, father(i));
i = father(i);
}
}
void heapDown(int i)
{
while(true)
{
int mini = i;
if(lSon(i) <= heapSize && elem[heap[lSon(i)]] < elem[heap[mini]])
{
mini = lSon(i);
}
if(rSon(i) <= heapSize && elem[heap[rSon(i)]] < elem[heap[mini]])
{
mini = rSon(i);
}
if(mini == i)
{
break;
}
heapSwap(mini, i);
}
}
int heapTop()
{
return heap[1];
}
void heapPop()
{
heapSwap(1, heapSize);
heapSize--;
heapDown(1);
}
void heapInsert(int index)
{
heap.push_back(index);
heapSize++;
heapUp(heapSize);
}
void heapDelete(int index)
{
elem[index] = -INF;
heapUp(poz[index]);
heapPop();
}
int main()
{
#ifdef ONLINE_JUDGE
ostream &fout = cout;
istream &fin = cin;
#else
fstream fin(infile.c_str(), ios::in);
fstream fout(outfile.c_str(), ios::out);
#endif
int N;
fin >> N;
heap.reserve(N);
poz.reserve(N);
elem.reserve(N);
initHeap();
int inserted = 0;
for(int i = 0; i < N; i++)
{
int op, x;
fin >> op;
if(op == 1)
{
fin >> x;
elem[inserted] = x;
heapInsert(inserted++);
}
else if(op == 2)
{
fin >> x;
heapDelete(x - 1);
}
else if(op == 3)
{
fout << elem[heapTop()] << "\n";
}
}
#ifdef ONLINE_JUDGE
#else
fout.close();
fin.close();
#endif
}