Pagini recente » Cod sursa (job #2336525) | Cod sursa (job #2431274) | Cod sursa (job #2148351) | Cod sursa (job #1812226) | Cod sursa (job #2811474)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<int>heap;
vector<int>ordine;
void up(int poz){
while(heap[poz]<heap[poz/2] && poz!=1){
swap(heap[poz],heap[poz/2]);
poz/=2;
}
}
void down(int poz){
int l = heap.size();
while(poz*2 < l){
int fiu = 2*poz;
fiu+=(heap[fiu]>heap[fiu+1] && l>fiu+1);
if(heap[fiu]>heap[poz])
return;
swap(heap[poz],heap[fiu]);
poz=fiu;
}
}
void ins(int x){
heap.push_back(x);
int poz=heap.size()-1;
if(heap[poz]>heap[poz/2])
up(poz);
}
void del(int poz){
swap(heap[heap.size()-1],heap[poz]);
heap.pop_back();
up(poz);
down(poz);
}
int main()
{
ifstream in("heapuri.in");
ofstream out("heapuri.out");
heap.push_back(0);
int n,cerinta,x;
in>>n;
for(int i=0;i<n;i++){
in>>cerinta;
if(cerinta == 1){
in>>x;
ins(x);
ordine.push_back(x);
}
else if(cerinta == 2){
in>>x;
int pozitie;
for(int i=0;i<ordine.size();i++){
if(ordine[x-1] == heap[i]){
pozitie = i;
break;
}
}
del(pozitie);
}
else{
out<<heap[1]<<'\n';
}
}
return 0;
}