Cod sursa(job #1290468)

Utilizator mirceadinoMircea Popoveniuc mirceadino Data 11 decembrie 2014 11:57:50
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.32 kb
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "heapuri";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;

const int NMAX = 200000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

int M, dim, cnt;
int V[NMAX];
int H[NMAX];
int P[NMAX];

void heapUp(int f) {
    int p = f / 2;

    if(p && V[H[p]] > V[H[f]]) {
        swap(H[p], H[f]);
        swap(P[H[p]], P[H[f]]);
        heapUp(p);
    }
}

void heapDown(int p) {
    int f = 2 * p;

    if(V[H[f]] > V[H[f + 1]] && f + 1 <= dim)
        f++;

    if(f <= dim && V[H[p]] > V[H[f]]) {
        swap(H[p], H[f]);
        swap(P[H[p]], P[H[f]]);
        heapDown(f);
    }
}

void insert(int x) {
    V[++cnt] = x;
    H[++dim] = cnt;
    P[cnt] = dim;
    heapUp(dim);
}

void erase(int x) {
    V[x] = INF;
    heapDown(P[x]);
    dim--;
}

int top() {
    return V[H[1]];
}

int main() {
    int t, x;

#ifndef ONLINE_JUDGE
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    scanf("%d", &M);

    while(M--) {
        scanf("%d", &t);

        if(t == 1) {
            scanf("%d", &x);
            insert(x);
        } else if(t == 2) {
            scanf("%d", &x);
            erase(x);
        } else {
            printf("%d\n", top());
        }
    }

    return 0;
}