Cod sursa(job #3243226)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 16 septembrie 2024 16:48:20
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.17 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[2 * NMAX + 100];

bool marked[NMAX + 100];

int n, cnt;

void rearanj(int idx){
    
    while(idx > 1 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 i = 1;
    
     while(heap[i].val > heap[i * 2].val or heap[i].val > heap[i * 2 + 1].val)
        {
            if(heap[i * 2].val < heap[i * 2 + 1].val)
            {
                swap(heap[i] , heap[i * 2]);
                i *= 2; 
            }
            else
            {
                swap(heap[i] , heap[i * 2 + 1]);
                i =i * 2 + 1;
            }
        }
    
    
}

int main()
{
    int n;
    f >> n;
    
    for(int i=1; i<=2 * NMAX + 1; 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(heap_sz and marked[heap[1].poz]){
                
                pop();
                
            }
            
            //if(heap[1].val != 0)
            g << heap[1].val << "\n";
            
        }
        
    }
    
    //cout << "PULA";
    

    return 0;
}