Cod sursa(job #2665044)

Utilizator OvidRata Ovidiu Ovid Data 29 octombrie 2020 22:21:24
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.62 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("heapuri.in"); ofstream fout("heapuri.out");
#define cin fin
#define cout fout



int t, n, m, k, a[300010], q, l, r;

int hp[800010];
int ind[200010];
int tmp[800010];
int lt=0;


void ins(int x, int i){
lt++;
hp[lt]=x;
int cr=lt;
ind[i]=cr;
tmp[cr]=i;
while(cr>1){
    if( hp[cr/2]>hp[cr] ){
        swap(hp[cr/2], hp[cr]);
        swap(tmp[cr/2], tmp[cr]);
        ind[tmp[cr/2] ]=cr/2; ind[tmp[cr] ]=cr;
        cr/=2;
    }
    else{
        break;
    }
}

}




void del(int i){
int pos=ind[i];
tmp[pos]=tmp[lt];
ind[tmp[pos] ]=pos;
int x=hp[pos];
hp[pos]=hp[lt]; hp[lt]=0; ; lt--;
//cout<<x<<" "<<hp[pos]<<"\n";
//up-heapify
if( hp[pos]<x ){
    while(pos>1){
        if(hp[pos/2]>hp[pos]){
            swap(hp[pos], hp[pos/2]);
            swap(tmp[pos], tmp[pos/2]);
            ind[tmp[pos] ]=pos; ind[tmp[pos/2] ]=pos/2;
            pos/=2;
        }
        else{
            break;
        }
    }
}

//down-heapify
if(hp[pos]>x){
    while(pos<=lt){
        int a1=-1e10, a2=-1e10;
        if( (pos*2)<=lt){
            a1=hp[pos*2];
        }
        if( (pos*2)<lt){
            a2=hp[pos*2+1];
        }
        //cout<<a1<<" "<<a2<<"a\n";
        if( ((a1<a2) && (a1>(-1e10))) || ((a1>(-1e10)) && (a2<=(-1e10))) ){
            if(a1<hp[pos]){
                swap(hp[pos*2], hp[pos]);
                swap(tmp[pos*2], tmp[pos] );
                ind[tmp[pos] ]=pos; ind[tmp[pos*2] ]=pos*2;
                pos*=2;
            }
            else{
                break;
            }
        }
        else{
            if((a2>(-1e10)) && (a2<hp[pos]) ){
                swap(hp[pos*2+1], hp[pos]);
                swap(tmp[pos*2+1], tmp[pos] );
                ind[tmp[pos] ]=pos; ind[tmp[pos*2+1] ]=pos*2+1;
                pos*=2; pos++;
            }
            else{
                break;
            }
        }
    }
}
//cout<<hp[]<<"ps\n";


}




int32_t main(){
INIT
cin>>n;
int cnt=1;
for(int i=1; i<=n; i++){
    int o, x; cin>>o;
    if(o==1){
        cin>>x;
        ins(x, cnt); cnt++;
    }
    if(o==2){
        int crd; cin>>crd;
        del(crd);
    }
    if(o==3){
        cout<<hp[1]<<"\n";
    }
}



return 0;
}