Pagini recente » Cod sursa (job #1840109) | Cod sursa (job #1486437) | Cod sursa (job #664863) | Cod sursa (job #974341) | Cod sursa (job #2190213)
// Heap.cpp : Defines the entry point for the console application.
//
#include <algorithm>
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <stdio.h>
#include <vector>
using namespace std;
const int MAX_HEAP_SIZE = 200002;
int H[MAX_HEAP_SIZE], nH, History[MAX_HEAP_SIZE];
//template <class T> void swap(T& a, T& b)
//{
// T c(a); a = b; b = c;
//}
inline int father(int x) {
return x / 2;
}
inline int left_son(int x) {
return x * 2;
}
inline int right_son(int x) {
return x * 2 + 1;
}
//sift = jos
void jos(int k) {
int son = 0;
do {
son = 0;
if (left_son(k) <= nH) {
son = left_son(k);
if (right_son(k) <= nH && H[right_son(k)] > H[son]) {
son = right_son(k);
}
if (H[son] <= H[k]) {
son = 0;
}
}
if (son) {
swap(H[k], H[son]);
k = son;
}
} while (son);
}
void sus(int k) {
while (k > 1 && H[k] > H[father(k)]) {
swap(H[k], H[father(k)]);
k = father(k);
}
}
void cut(int key) {
H[key] = H[nH];
nH--;
if (key > 1 && H[key] > H[father(key)]) {
sus(key);
}
else {
jos(key);
}
}
int verif(int nod, int key) {
if (nod > nH) {
return -1;
}else {
if (H[nod] == key) return nod;
if (H[nod] < key) return -1;
int valStg = verif(left_son(nod), key);
int valDr = verif (right_son(nod), key);
if (valDr != -1) return valDr;
if (valStg != -1) return valStg;
}
}
void insert(int value) {
H[++nH] = value;
sus(nH);
}
int getMax() {
return H[1];
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
FILE * pFile;
pFile = fopen("heapuri.in", "r");
int m = 0;
int counter = 0;
fscanf(pFile, "%d", &m);
while (m--) {
int optiune = 0;
fscanf(pFile, "%d", &optiune);
if (optiune == 3) {
g << -getMax() << "\n";
}
else {
int val = 0;
fscanf(pFile, "%d", &val);
if (optiune == 1) {
counter++;
History[counter] = -val;
insert(-val);
}
else {
int valCautata = History[val];
cut(verif(1, valCautata));
}
}
}
return 0;
}