Cod sursa(job #2225907)

Utilizator AlexandruPaulSirbu Alex AlexandruPaul Data 28 iulie 2018 16:51:02
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#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;
        }
    }
}