Cod sursa(job #1443864)

Utilizator ButnaruButnaru George Butnaru Data 28 mai 2015 19:31:30
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <stdio.h>
#include <cstring>
#include <vector>
#include <bitset>
#include <set>
#include <math.h>
#include <deque>
#include <queue>
#include <string>
#include <algorithm>
#define nmax 200010
using namespace std;
int n,i,x,tip,nr=0,t[nmax],heap[nmax],pos[nmax],b=0;
void Swap(int x,int y)
{
    swap(heap[x],heap[y]);
    pos[heap[x]]=x;
    pos[heap[y]]=y;
}
int 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;
}
int heapdown(int v)
{
    int w=v*2;
    while (w<=nr){
        if (w+1<=nr && t[heap[w+1]]<t[heap[w]]) w++;
        if (t[heap[v]]>t[heap[w]]) Swap(v,w); else break;
        v=w; w*=2;
    }
}
inline void insert_heap(int x)
{
    nr++; b++; t[b]=x; heap[nr]=b; pos[b]=nr;
    heapup(nr);
}
inline void delete_heap(int x)
{
    t[x]=-1; heapup(pos[x]);
    heap[1]=heap[nr]; pos[heap[1]]=1; nr--;
    heapdown(pos[x]);
}
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); insert_heap(x); break; }
        case 2:{ scanf("%d",&x); delete_heap(x); break; }
        case 3:{ printf("%d\n",t[heap[1]]); break; }
    }
}
return 0;
}