Cod sursa(job #2810217)

Utilizator LuciBBadea Lucian LuciB Data 28 noiembrie 2021 21:29:30
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.62 kb
#include <bits/stdc++.h>
using namespace std;

FILE *fin, *fout;
const int NMAX = 2e5;
pair<int, int> heapp[NMAX + 1];
int heapSize;
bool erased[NMAX];
int poz;
void addHeap(int val) {
    int i;
    heapp[heapSize++] = {val, poz};
    poz++;
    i = heapSize - 1;
    while(i && heapp[i].first < heapp[(i - 1) / 2].first)  {
        swap(heapp[i], heapp[(i - 1) / 2]);
        i = (i - 1) / 2;
    }
}

void downHeap(int i) {
    int l, r, minn;

    l = 2 * i + 1;
    r = 2 * i + 2;
    minn = i;
    if(l < heapSize && heapp[l].first < heapp[minn].first)
        minn = l;
    if(r < heapSize && heapp[r].first < heapp[minn].first)
        minn = r;
    if(i != minn) {
        swap(heapp[i], heapp[minn]);
        downHeap(minn);
    }
}
void remove() {
    // int minn = heapp[0].first;
    heapp[0] = heapp[--heapSize];
    downHeap(0);
    //return minn;
}

static inline int minimum() {
    return heapp[0].first;
}

int main() {
    int n, op, x, i;

    fin = fopen("heapuri.in", "r");
    fout = fopen("heapuri.out", "w");

    fscanf(fin, "%d", &n);
    poz = 0;
    for(i = 0; i < n; i++) {
        fscanf(fin, "%d", &op, &x);
        switch (op) {
        case 1:
            fscanf(fin, "%d", &x);
            addHeap(x);
            break;
        case 2:
            fscanf(fin, "%d", &x);
            erased[x - 1] = true;
            break;
        default:
            while(erased[heapp[0].second])
                remove();
            fprintf(fout, "%d\n", minimum());
        }
    }

    //cout << (-1) / 2;
    fclose(fin);
    fclose(fout);

    return 0;
}