Pagini recente » Cod sursa (job #1828644) | Cod sursa (job #1457646) | Cod sursa (job #665724) | Cod sursa (job #2380676) | Cod sursa (job #2089445)
#include <stdio.h>
#define NMAX 200000
#define VALMAX 1000000000
int v [ NMAX ] ;
int heap [ NMAX ] ; /// min heap
int poz [ NMAX ] ;
int n, aux ;
int lson (int tata ) {
return tata * 2 ;
}
int rson (int tata ) {
return tata * 2 + 1 ;
}
int daddy (int nod ) {
return nod / 2 ;
}
void sift (int nod ) {
int son, aux ;
if (heap[rson(nod)] < heap[lson(nod)] && rson(nod) <= n )
son = rson(nod) ;
else
son = lson(nod) ;
while ( heap[son] < heap[nod] && 2 * nod <= n ) {
aux = heap[son] ;
heap[son] = heap[nod] ;
heap[nod] = aux ;
aux = poz[son] ;
poz[son] = poz[nod] ;
poz[nod] = aux ;
nod = son ;
if (v[rson(nod)] < v[lson(nod)] )
son = rson(nod) ;
else
son = lson(nod) ;
}
}
void delet (int pozitie ) {
aux = heap[n] ;
heap[n] = heap[pozitie] ;
heap[pozitie] = aux ;
aux = poz[n] ;
poz[n] = poz[pozitie] ;
poz[pozitie] = aux ;
n--;
sift (pozitie) ;
}
//void create_heap () {
// int i ;
// for (i = 1 ; i <= n ; i++ ) {
// sift(i) ;
// }
//}
void percolate (int nod ) {
int tata ;
tata = daddy(nod) ;
while (heap[tata] > heap[nod] && nod > 1 ) {
aux = heap[tata] ;
heap[tata] = heap[nod] ;
heap[nod] = aux ;
aux = poz[tata] ;
poz[tata] = poz[nod] ;
poz[nod] = aux ;
nod = tata ;
tata = daddy(nod) ;
}
}
int main() {
FILE *fin, *fout ;
fin = fopen ("heapuri.in", "r" ) ;
fout = fopen ("heapuri.out", "w" ) ;
int m, op, i, val, j ;
fscanf (fin, "%d", &m ) ;
for (i = 0 ; i < m ; i++ ) {
fscanf (fin, "%d", &op ) ;
if (op == 1 ) {
fscanf (fin, "%d", &val ) ;
n++;
heap[n] = v[n] = val ;
poz[n] = n ;
percolate(n) ;
heap[n+1] = 1000000001;
}
else {
if (op == 2 ) {
fscanf (fin, "%d", &val ) ;
j = 1 ;
while (poz[j] != val && j < n )
j++;
delet(j) ;
}
else
fprintf (fout, "%d\n", heap[1] ) ;
}
}
fclose (fin) ;
fclose (fout);
return 0;
}