Pagini recente » Cod sursa (job #627419) | Cod sursa (job #1206295) | Cod sursa (job #723738) | Cod sursa (job #2455347) | Cod sursa (job #659069)
Cod sursa(job #659069)
#include <fstream>
#define NMAX 200001
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,i,o,x,l,d,nr;
int a[NMAX],heap[NMAX],p[NMAX];
void push(int x) {
while (x/2!=0 && a[heap[x]]<a[heap[x/2]]) {
swap(heap[x],heap[x/2]);
p[heap[x]]=x;
x/=2;
p[heap[x]]=x;
}
}
void pop(int x) {
int y=0;
while (x!=y) {
y=x;
if (y*2<=l && a[heap[x]]>a[heap[2*y]]) x=y*2;
if (y*2+1<=l && a[heap[x]]>a[heap[2*y+1]]) x=y*2+1;
swap(heap[x],heap[y]);
p[heap[x]]=x;p[heap[y]]=y;
}
}
int main () {
f >> n;
for (i=1;i<=n;i++) {
f >> o;
if (o==3) g << a[heap[1]] << '\n';
if (o==1) {
f >> x;
a[++nr]=x;
heap[++l]=nr;
p[nr]=l;
push(l);
}
if (o==2) {
f >> x;
a[x]=-1;
push(p[x]);
p[heap[1]]=0;
heap[1]=heap[l];
l--;
p[heap[1]]=1;
if (l>1) pop(1);
}
}
f.close();g.close();
return 0;
}