Cod sursa(job #3243206)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 16 septembrie 2024 16:09:47
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.21 kb
//heap de mana coaie

#include <bits/stdc++.h>

using namespace std;

ifstream f ("heapuri.in");
ofstream g ("heapuri.out");

const int NMAX = 2e5;

struct elem{
    
  int val, poz;  
  
};

elem heap[NMAX + 1];

bool marked[NMAX+1];

int n, cnt;

void rearanj(int idx){
    
    while(idx != 0 and heap[idx/2].val > heap[idx].val)
        swap(heap[idx/2], heap[idx]), idx /= 2;
    
}

int heap_sz;

void pop(){
    
    heap[1] = heap[heap_sz];
    
    heap_sz -= 1;
    
    int idx = 1;
    
    while(idx <= heap_sz){
        
        if(heap[idx].val > heap[idx * 2].val)
            swap(heap[idx], heap[2 * idx]), idx = idx * 2;
            
        else if(heap[idx].val > heap[idx * 2 + 1].val)
            swap(heap[idx], heap[2 * idx + 1]), idx = idx * 2 + 1;
            
        else idx = 10000000;
        
    }
    
    
}

int main()
{
    int n;
    f >> n;
    
    for(int i=1; i<=NMAX; i++)
        heap[i] = {2000000000, 2000000000};
        
    for(int i=1; i<=NMAX; i++)
        marked[i] = false;
        
    //int heap_sz = 0;
    
    for(int i=1; i<=n; i++){
        
        int t;
        f >> t;
        
        if(t == 1){ //inseram
            
            int k;
            f >> k;
            
            cnt ++;
            
            heap_sz ++;
            
            heap[heap_sz] = {k, cnt};
            
            //cout << cnt << ' ' << heap[cnt].val << ' ';
            
            rearanj(heap_sz);
            
            //cout << heap[1].val << endl;
            
            
        }
        
        if(t == 2){ // stergem bro
            
            int k;
            f >> k;
            
            marked[k] = true;
            
            
            
        }
        
        if(t == 3){ // afisam bro dar avem grija sa nu fie sters ca daca da il inlocuim cu ult element dam swap si apoi mna
            
            while(marked[heap[1].poz]){
                
                //cout << heap[1].poz << " DAS" << endl;
                pop();
                
                rearanj(heap_sz);
                
                
            }
            
            g << heap[1].val << "\n";
            
        }
        
    }
    
    //cout << "PULA";
    

    return 0;
}