Cod sursa(job #2682508)

Utilizator andrei.florea0405Florea Andrei-Bogdan andrei.florea0405 Data 8 decembrie 2020 20:13:24
Problema Heapuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int n;
int v[200010], p[200010];
vi a;

void add(int x) {
    n++;
    v[n] = x;
    p[x] = n;

    int pos = n;
    while (pos > 1 && v[pos] < v[pos / 2]) {
        p[v[pos]] = pos / 2;
        p[v[pos / 2]] = pos;
        swap(v[pos], v[pos / 2]);
        pos /= 2;
    }
}

void remove(int pos) {
    int aux = v[pos];
    v[pos] = v[n];
    n--;
    
    while (pos < n) {
        int fiu = 2 * pos;
        if (fiu + 1 <= n && v[fiu] > v[fiu + 1])
            fiu++;

        if (v[pos] < v[fiu])
            break;
        
        p[v[pos]] = fiu;
        p[v[fiu]] = pos;
        swap(v[pos], v[fiu]);
        pos = fiu;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    a.pb(0);
    int t;
    fin >> t;
    
    n = 0;
    while (t--) {
        int op, x;
        fin >> op;
        if (op == 1) {
            fin >> x;
            a.pb(x);
            add(x);
        } else if (op == 2) {
            fin >> x;
            remove(p[a[x]]);
        } else {
            fout << v[1] << "\n";
        }

        // for (int i = 1; i <= n; i++) {
        //     fout << v[i] << " ";
        // }
        // fout << "\n";
    }

    return 0;
}