Pagini recente » Cod sursa (job #1972974) | Cod sursa (job #1800901) | Cod sursa (job #1021682) | Cod sursa (job #547280) | Cod sursa (job #528933)
Cod sursa(job #528933)
#include <fstream>
using namespace std;
#define MAXN 200000
int N, heap_size, insCount, poz[MAXN+1];
pair<int,int> heap[MAXN+1];
ifstream f("heapuri.in");
ofstream g("heapuri.out");
void min_heapify(int i) {
int smallest, left;
do {
smallest = i; left = i<<1;
for (int j = left;j<left+2;j++) {
if (j <= heap_size && heap[j].first < heap[smallest].first) {
smallest = j;
}
}
if (smallest == i) break;
swap(heap[i],heap[smallest]);
swap(poz[heap[i].second],poz[heap[smallest].second]);
i = smallest;
}while (true);
}
void insert_heap(int val) {
int parent, i;
insCount++;
heap[++heap_size] = make_pair(val,insCount);
poz[insCount] = heap_size;
parent = heap_size>>1;
i = heap_size;
while (parent > 0 && heap[parent].first > heap[i].first) {
swap(heap[parent], heap[i]);
swap(poz[heap[parent].second], poz[heap[i].second]);
i = parent;
parent = i>>1;
}
}
int main() {
int tip, aux;
f >> N;
for (int i=1;i<=N;i++) {
f>>tip;
switch(tip) {
case 1:
f>>aux;
insert_heap(aux);
break;
case 2:
f >> aux;
swap(heap[poz[aux]],heap[heap_size]);
heap_size--;
min_heapify(poz[aux]);
break;
case 3:
g << heap[1].first <<"\n";
break;
}
}
f.close(); g.close();
return 0;
}