Cod sursa(job #1467533)

Utilizator SilviuIIon Silviu SilviuI Data 3 august 2015 15:53:34
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#include <string>
#include <vector>
#include <math.h>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <limits.h>
#include <algorithm>
#include <deque>
#define nmax 200010
#define mod 1999999973
using namespace std;
int n,i,heap[nmax],tip,nr,t[nmax],pos[nmax],x,nrx;
void Swap(int a,int b)
{
    swap(heap[a],heap[b]); pos[heap[a]]=a; pos[heap[b]]=b;
}
void heapup(int v)
{
    int k=heap[v];
    while (v>1 && t[heap[v]]<t[heap[v/2]]) {
        Swap(v,v/2);
        v=v/2;
    }
    heap[v]=k;
}
void heapdown(int v)
{
    int w=v*2;
    while (w<=nrx) {
        if (w+1<=nrx && t[heap[w]]>t[heap[w+1]]) w++;
        if (t[heap[v]]>t[heap[w]]) Swap(v,w); else break;
        v=w; w=w*2;
    }
}
void add_heap(int x)
{
    nrx++; nr++; heap[nrx]=nr; t[nr]=x; pos[nr]=nrx;
    heapup(nrx);
}
void delete_heap(int x)
{
    t[x]=-1; heapup(pos[x]);
    Swap(1,nrx); nrx--;
    heapdown(1);
}
int main() {
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
scanf("%d",&n);
for (i=1;i<=n;i++) {
    scanf("%d",&tip);
    switch (tip) {
        case 1:{ scanf("%d",&x); add_heap(x); break; }
        case 2:{ scanf("%d",&x); delete_heap(x); break; }
        case 3:{ printf("%d\n",t[heap[1]]); break; }
    }
}
return 0;
}