Pagini recente » Cod sursa (job #2575696) | Cod sursa (job #1165709) | Cod sursa (job #1087575) | Cod sursa (job #2508541) | Cod sursa (job #2225907)
#include <iostream>
#include <fstream>
using namespace std;
const int Maxx=2e5+100;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int querry;
int cod,elem,poz,pz,i;
int len;
bool ok;
int heap[Maxx];
int pos[Maxx];
void percolate(int node);
void shift(int node);
int cauta (int node,int poz);
int main() {
fin>>querry;++querry;
for(;--querry;){
fin>>cod;
if (cod==1){
fin>>elem;
heap[++len]=elem;
pos[len]=len;
percolate(len);
continue;
}
if (cod==2) {
fin>>poz;
for (i=1;i<=len;++i) if (pos[i]==poz) break;
heap[i]=0;
swap(heap[len],heap[i]);
shift(i);
continue;
}
fout<<heap[1]<<"\n";
}
return 0;
}
void percolate(int node){
while (node>1 && heap[node]<heap[node/2]){
swap(heap[node],heap[node/2]);
swap(pos[node],pos[node/2]);
node/=2;
}
}
void shift(int node){
--len;
while (node<=len){
if (heap[2*node]==0 && heap[2*node+1]==0) break;
if (heap[2*node]>heap[2*node+1]){
swap(heap[node],heap[2*node]);
swap(pos[node],pos[2*node]);
node=2*node;
} else {
swap(heap[node],heap[2*node+1]);
swap(pos[node],pos[2*node+1]);
node=2*node+1;
}
}
}