Pagini recente » Cod sursa (job #2383684) | Cod sursa (job #3288530) | Cod sursa (job #532757) | Cod sursa (job #2262860) | Cod sursa (job #347643)
Cod sursa(job #347643)
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
struct nod{
int val;
int pos;
nod(int v,int p):val(v),pos(p){}
nod(){}
};
int main(){
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n;in>>n;
vector<nod> heap;heap.push_back(nod(0,0));heap.reserve(200000);
vector<int> pos;pos.push_back(int());pos.reserve(200000);
for(int i=0;i<n;i++){
int tip;in>>tip;
switch(tip){
case 1: {
int t;in>>t;
pos.push_back(heap.size());
heap.push_back(nod(t,pos.size()-1));
int j=heap.size()-1;
while(j>1&&heap[j].val<heap[j/2].val){
swap(heap[j],heap[j/2]);
pos[heap[j].pos]=j;
pos[heap[j/2].pos]=j/2;
j=j/2;
}
break;
}
case 2:{
int t;in>>t;
int j=pos[t];
if(j==heap.size()-1){
heap.resize(heap.size()-1);
}else{
swap(heap[j],heap[heap.size()-1]);
pos[heap[j].pos]=j;
heap.resize(heap.size()-1);
while(j>1&&heap[j].val<heap[j/2].val){
swap(heap[j],heap[j/2]);
pos[heap[j].pos]=j;
pos[heap[j/2].pos]=j/2;
j=j/2;
}
bool go=true;
while(2*j+1<heap.size()){
if(heap[2*j].val<heap[j].val){
if(heap[2*j+1].val<heap[2*j].val){
swap(heap[j],heap[2*j+1]);
pos[heap[j].pos]=j;
pos[heap[2*j+1].pos]=2*j+1;
j=2*j+1;
}else{
swap(heap[j],heap[2*j]);
pos[heap[j].pos]=j;
pos[heap[2*j].pos]=2*j;
j=2*j;
}
}else if(heap[2*j+1].val<heap[j].val){
swap(heap[j],heap[2*j+1]);
pos[heap[j].pos]=j;
pos[heap[2*j+1].pos]=2*j+1;
j=2*j+1;
}else{
go=false;
}
}
if(go&&2*j<heap.size()){
if(heap[2*j].val<heap[j].val){
swap(heap[j],heap[2*j]);
pos[heap[j].pos]=j;
pos[heap[2*j].pos]=2*j;
j=2*j;
}
}
}
break;
}
case 3:{
out<<heap[1].val<<"\n";
}
}
}
}