Pagini recente » Cod sursa (job #1173435) | Cod sursa (job #1692483) | Cod sursa (job #1346537) | Cod sursa (job #1346530) | Cod sursa (job #2887525)
#include<fstream>
using namespace std;
int heap[200005], cronologic[200005], pozitii[200005];
int backHeap=1;
int n;
void swapCuPozitii(int poz1, int poz2){
pozitii[heap[poz1]]=poz2;
pozitii[heap[poz2]]=poz1;
swap(heap[poz1], heap[poz2]);
}
void inserare(int x){
heap[backHeap]=x;
int poz=backHeap;
pozitii[x]=backHeap;
backHeap++;
while(poz!=1 && cronologic[heap[poz]]<cronologic[heap[poz/2]]){
swapCuPozitii(poz, poz/2);
poz/=2;
}
}
void stergeRoot(){
backHeap--;
swapCuPozitii(1, backHeap);
int poz=1;
while(1){
if(2*poz>=backHeap)
break;
if(2*poz+1>=backHeap){
if(cronologic[heap[poz]]>cronologic[heap[2*poz]]){
swapCuPozitii(poz, 2*poz);
}
break;
}
if(cronologic[heap[2*poz]]<cronologic[heap[2*poz+1]]){
if(cronologic[heap[poz]]<cronologic[heap[2*poz]])
break;
swapCuPozitii(poz, 2*poz);
poz*=2;
}
else{
if(cronologic[heap[poz]]<cronologic[heap[2*poz+1]])
break;
swapCuPozitii(poz, 2*poz+1);
poz=2*poz+1;
}
}
}
void stergere(int x){
int poz=pozitii[x];
while(poz!=1){
swapCuPozitii(poz, poz/2);
poz/=2;
}
stergeRoot();
}
int main() {
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
fin>>n;
int optiune;
int contor=1;
for(int i=0;i<n;i++){
fin>>optiune;
//inserare
if(optiune==1){
int x;
fin>>x;
cronologic[contor]=x;
inserare(contor);
contor++;
}
//stergere
else if(optiune==2){
int poz;
fin>>poz;
stergere(poz);
}
//minim
else{
fout<<cronologic[heap[1]]<<"\n";
}
}
fout.close();
fin.close();
return 0;
}