Mai intai trebuie sa te autentifici.
Cod sursa(job #1367393)
Utilizator | Data | 1 martie 2015 20:37:26 | |
---|---|---|---|
Problema | Heapuri | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 2.51 kb |
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define Nmax 200005
class Heap_tree{
private:
int H[Nmax];
int poz[Nmax];
int timp[Nmax];
int Size,dad,rs,ls;
public:
Heap_tree(){
Size = dad = rs = ls = 0;
memset(H,0,sizeof(H));
memset(poz,0,sizeof(poz));
memset(timp,0,sizeof(timp));
}
void Upheap(int nodc){
dad = nodc / 2;
int oldH = H[nodc];
int oldT = timp[nodc];
while(dad && H[dad] > oldH){
H[nodc] = H[dad];
timp[nodc] = timp[dad];
poz[timp[nodc]] = nodc;
nodc = dad;
dad = nodc / 2;
}
H[nodc] = oldH;
timp[nodc] = oldT;
poz[timp[nodc]] = nodc;
}
void Downheap(int nodc){
int oldH = H[nodc];
int oldT = timp[nodc];
int where;
while(1){
ls = nodc << 1;
rs = (nodc << 1)|1;
where = nodc;
if(ls <= Size && H[ls] < oldH)
where = ls;
if(rs <= Size && H[rs] < oldH && H[rs] < H[ls])
where = rs;
if(where == nodc)
break;
H[nodc] = H[where];
timp[nodc] = timp[where];
poz[timp[nodc]] = nodc;
nodc = where;
}
H[nodc] = oldH;
timp[nodc] = oldT;
poz[timp[nodc]] = nodc;
}
void Insert(int nodc,int moment){
++Size;
H[Size] = nodc;
poz[moment] = Size;
timp[Size] = moment;
Upheap(Size);
}
void Delete(int time_moment){
int pos = poz[time_moment];
dad = pos / 2;
H[pos] = H[Size];
timp[pos] = timp[Size];
poz[timp[pos]] = pos;
--Size;
if(H[dad] < H[pos])
Downheap(pos);
else
Upheap(pos);
}
int Top(){
return H[1];
}
};
Heap_tree Heap;
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int T,mom = 0,op,val,x;
scanf("%d",&T);
for(int i = 1; i <= T; ++i)
{
scanf("%d",&op);
if(op == 1){
scanf("%d",&val);
Heap.Insert(val,++mom);
continue;
}
if(op == 2){
scanf("%d",&x);
Heap.Delete(x);
continue;
}
printf("%d\n",Heap.Top());
}
return 0;
}