Cod sursa(job #2952894)

Utilizator andu2006Alexandru Gheorghies andu2006 Data 10 decembrie 2022 10:32:45
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.06 kb
#include <bits/stdc++.h>
using namespace std;

const int NMAX=2e5+5,buffsize=1<<13;
FILE* fin;
char buff[buffsize];
int buffpos=buffsize;
int read(){
    if(buffpos==buffsize) fread(buff,1,buffsize,fin),buffpos=0;
    int n=0;
    while(buff[buffpos]<'0' || buff[buffpos]>'9'){
        ++buffpos;
        if(buffpos==buffsize) fread(buff,1,buffsize,fin),buffpos=0;
    }
    while(buff[buffpos]>='0' && buff[buffpos]<='9'){
        n=(n<<1)+(n<<3)+(buff[buffpos]^48);
        ++buffpos;
        if(buffpos==buffsize) fread(buff,1,buffsize,fin),buffpos=0;
    }
    return n;
}
template<typename T, size_t capacity, class _cmp=less<T> >
class heap{
    private:
    T data[capacity];
    size_t N=0;
    _cmp cmp;
    public:
    size_t size(){
        return N;
    }
    void push(T x){
        data[N]=x;
        unsigned u=N++;
        while(u){
            if(cmp(x,data[(u-1)/2])){
                data[u]=data[(u-1)/2];
                u=(u-1)/2;
            }
            else break;
        }
        data[u]=x;
    }
    void pop(){
        T aux=data[--N];
        unsigned u=0;
        while(u*2+1<N){
            if(u*2+2>=N || cmp(data[u*2+1],data[u*2+2])){
                if(cmp(data[u*2+1],aux)){
                    data[u]=data[u*2+1];
                    u=u*2+1;
                }
                else break;
            }
            else{
                if(cmp(data[u*2+2],aux)){
                    data[u]=data[u*2+2];
                    u=u*2+2;
                }
                else break;
            }
        }
        data[u]=aux;
    }
    T top(){
        return data[0];
    }
};
int v[NMAX],n,pos[NMAX];
set<int> s;
int main()
{
    fin=fopen("heapuri.in","r");
    ofstream fout("heapuri.out");
    int q=read();
    while(q--){
        int c=read();
        if(c==1){
            v[n]=read();
            s.insert(v[n++]);
        }
        else if(c==2){
            int x=read();
            s.erase(v[x-1]);
        }
        else fout<<*s.begin()<<'\n';
    }
    return 0;
}