Pagini recente » Cod sursa (job #363335) | Cod sursa (job #1420222) | Cod sursa (job #2611337) | Cod sursa (job #1931781) | Cod sursa (job #2449212)
#include <bits/stdc++.h>
#define NUM 200005
using namespace std;
int heap[NUM];
int val[NUM];
int poz[NUM];
int t, n, cod, num_int;
void push(int x)
{
// percolate, modificat
int aux;
while(x / 2 && val[heap[x]] < val[heap[x / 2]])
{
aux = heap[x];
heap[x] = heap[x / 2];
heap[x / 2] = aux;
poz[heap[x]] = x;
poz[heap[x / 2]] = x / 2;
x /= 2;
}
}
void pop(int x)
{
// sift, modificat
int aux, y = 0;
while(x != y)
{
y = x;
if(2 * y <= n && val[heap[x]] > val[heap[2 * y]])
x = 2 * y;
if(2 * y + 1 <= n && val[heap[x]] > val[heap[2 * y + 1]])
x = 2 * y + 1;
aux = heap[x];
heap[x] = heap[y];
heap[y] = aux;
poz[heap[x]] = x;
poz[heap[y]] = y;
}
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f >> t;
int x;
for(int i = 0; i < t; ++i)
{
f >> cod;
if(cod < 3)
f >> x;
if(cod == 1)
{
/*
adaug valoarea in val si introduc elementul in heap, retinand
pozitia in poz
*/
num_int++;
n++;
val[num_int] = x;
heap[n] = num_int;
poz[num_int] = n;
push(n);
}
if(cod == 2)
{
/*
modific valoarea valorii cautate cu -1, pentru a urca in varful
heap-ului. Dupa, il pot sterge mai usor
*/
val[x] = -1;
push(poz[x]);
poz[heap[1]] = 0;
heap[1] = heap[n--];
poz[heap[1]] = 1;
if(n > 1)
pop(1);
}
if(cod == 3)
g << val[heap[1]] << "\n";
}
f.close();
g.close();
}