Pagini recente » Cod sursa (job #713712) | Cod sursa (job #2068719) | Cod sursa (job #6613) | Cod sursa (job #2948858) | Cod sursa (job #1248915)
#include <iostream>
#include <fstream>
#include <vector>
#define log(...) cout<<__VA_ARGS__<<'\n';
#define NMax 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
class Heap {
public:
/// Public methods and properties
Heap () {
numberOfNodes = numberOfIndex = 0;
}
int fatherOf (int k) {return k/2;};
int leftSonOf (int k) {return 2*k;};
int rightSonOf (int k) {return 2*k+1;};
void insertOnHeap (int x) {
//log ("Inserting on heap");
numberOfNodes++, numberOfIndex++;
V[numberOfIndex] = x;
H[numberOfNodes] = numberOfIndex;
P[numberOfIndex] = numberOfNodes;
goUp (numberOfNodes);
}
void eraseFromHeap (int index) {
//log ("Erase from heap");
int position = P[index];
H[position] = H[numberOfNodes];
P[index] = -1;
numberOfNodes--;
if (position > 1 && V[H[position]] < V[H[fatherOf(position)]])
goUp(position);
else
goDown(position);
}
int minimumFromHeap () {
//log ("Returning the minimum");
return V[H[1]];
}
private:
/// Private methods and properties
int numberOfNodes, numberOfIndex;
int V[NMax];
int H[NMax];
int P[NMax];
void goUp (int x) {
int aux;
while (x/2 && V[H[x]]<V[H[x/2]])
{
aux = H[x];
H[x] = H[x/2];
H[x/2] = aux;
P[H[x]] = x;
P[H[x/2]] = x/2;
x /= 2;
}
}
void goDown (int x) {
int aux, y = 0;
while (x != y) {
y = x;
if (y*2<=numberOfNodes && V[H[x]]>V[H[y*2]]) x = y*2;
if (y*2+1<=numberOfNodes && V[H[x]]>V[H[y*2+1]]) x = y*2+1;
aux = H[x];
H[x] = H[y];
H[y] = aux;
P[H[x]] = x;
P[H[y]] = y;
}
}
};
int n;
Heap *customHeap;
int main() {
f>>n;
customHeap = new Heap();
for (int i=1;i<=n;i++) {
int type;
f>>type;
if (type == 1) {
int x;
f>>x;
customHeap->insertOnHeap(x);
} else if (type == 2) {
int x;
f>>x;
customHeap->eraseFromHeap(x);
} else if (type == 3) {
g<<customHeap->minimumFromHeap()<<'\n';
}
}
delete customHeap;
f.close(); g.close();
return 0;
}