Pagini recente » Cod sursa (job #1178910) | Cod sursa (job #174488) | Cod sursa (job #719051) | Cod sursa (job #347311) | Cod sursa (job #610221)
Cod sursa(job #610221)
#include <fstream>
#define NMAX 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,i,o,x,heap[NMAX],pos[NMAX],v[NMAX],nrh,nrt;
void push(int a) {
while (a/2!=0 && v[heap[a]]<v[heap[a/2]]) {
swap(heap[a],heap[a/2]);
pos[heap[a]]=a;
pos[heap[a/2]]=a/2;a/=2;
}
}
void pop(int a) {
int b=0;
while (a!=b) {
b=a;
if (2*b<=nrh && v[heap[a]]>v[heap[b*2]]) a=b*2;
if (2*b+1<=nrh && v[heap[a]]>v[heap[b*2+1]]) a=b*2+1;
swap(heap[a],heap[b]);
pos[heap[a]]=a;pos[heap[b]]=b;
}
}
int main () {
f >> n;
for (i=1;i<=n;i++) {
f >> o;
if (o==1) {
f >> x;
nrh++;nrt++;
v[nrt]=x;
heap[nrh]=nrt;
pos[nrt]=nrh;
push(nrh);
}
if (o==2) {
f >> x;
v[x]=-1;push(pos[x]);
pos[heap[1]]=0;heap[1]=heap[nrh];
nrh--;
pos[heap[1]]=1;
if (nrh>1) pop(1);
}
if (o==3) g << v[heap[1]] << '\n';
}
f.close();g.close();
return 0;
}