Pagini recente » Cod sursa (job #2569549) | Cod sursa (job #581997) | Cod sursa (job #1929828) | Cod sursa (job #1333193) | Cod sursa (job #1888820)
#include <fstream>
using namespace std;
ifstream f("heap.in");
ofstream g("heap.out");
int a[200005],n,l,v,i,t,x,m;
int hp[200010], poz[200010];
void Raise(int x) {
while (x/2 >= 1 && a[hp[x]] < a[hp[x/2]]) {
poz[hp[x]] = x/2;
poz[hp[x/2]] = x;
swap(hp[x],hp[x/2]);
}
}
void Lower(int x) {
int y = 0;
while (x !=y) {
y = x;
if (2*y+1 <= l && a[hp[2*y+1]]<a[hp[y]])
x = 2*y+1;
if (2*y <= l && a[hp[2*y]]<a[hp[y]])
x = 2*y;
poz[hp[y]] = x;
poz[hp[x]] = y;
swap(hp[x],hp[y]);
}
}
void add(int x) {
a[++n] = x; l++;
hp[l] = n;
poz[n] = l;
Raise(l);
}
void rem(int x) {
a[x] = -1;
if (poz[x])
Raise(x);
else return;
poz[1] = 0;
hp[1] = hp[l];
poz[hp[l]] = 1;
Lower(1);
//if (l > 0)
//Raise(l);
}
int main() {
f >> m;
while (m--) {
f >> t;
if (t == 3)
g<<a[hp[1]]<<'\n';
else {
f >> x;
if (t==1)
add(x);
else rem(x);
}
}
return 0;
}