Cod sursa(job #3163312)

Utilizator KarinaDKarina Dumitrescu KarinaD Data 31 octombrie 2023 11:31:58
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.8 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin ( "heapuri.in" );
ofstream fout ( "heapuri.out" );

const int N = 2e5 + 20;
int heap_size, n;

int poz[N];

struct heapStruct {
    int val;
    int poz;
} heap[N];

void heapUp (int p) {
    
    while ( heap[p].val < heap[p/2].val && p > 1 ) {
        
        swap ( poz[heap[p].poz], poz[heap[p/2].poz] );
        swap ( heap[p], heap[p/2] );
        
        p = p / 2;
    }
}

int heapAdd (int x) {
    
    heap[heap_size].val = x;
    heap[heap_size].poz = heap_size;
    
    heapUp(heap_size);
    
    return 0;
    
}

int heapErase (int x) {

    heap[x] = heap[heap_size];
    poz[heap[x].poz] = x;
    
    heap_size--;
    
    while ( x * 2 <= heap_size ) {
        
        int child = x * 2;
        if ( child < heap_size && heap[child].val > heap[child + 1].val )
            child++;
        
        if ( heap[x].val > heap[child].val ) {
            swap ( poz[heap[x].poz], poz[heap[child].poz] );
            swap ( heap[x], heap[child] );
            x = child;
        }
        
        else
            break;
    }
    
    heapUp(x);
    
    return 0;
}

int main() {
    
    int t, cer, x, n1 = 0;
    
    fin >> t;
    
    for ( int i = 0; i < t; i++ ) {
        
        fin >> cer;
        
        if ( cer == 1 ) {
            
            fin >> x;
            
            heap_size++;
            n++;
            
            poz[n] = heap_size;
            
            heapAdd(x);
        }
        
        else if ( cer == 2 ) {
            
            fin >> x;
    
            heapErase(poz[x]);
    
        }
        
        else
            fout << heap[1].val << "\n";
        
        /*for ( int k = 1; k <= n1; k++ )
            fout << poz[k] << " ";
        fout << "\n";*/
    }
    
    return 0;
}