Pagini recente » Borderou de evaluare (job #782725) | Borderou de evaluare (job #1872169) | Borderou de evaluare (job #2809193) | Borderou de evaluare (job #3341600) | Cod sursa (job #1169909)
#include <fstream>
using namespace std;
pair<int,int> heap[200005],tmp;
int Q,p,c,pd,N,NR,aux,op;
int poz[200005];
int main() {
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>Q;
while(Q--) {
f>>op;
if(op==1) {
++N;
poz[++NR]=N;
f>>heap[N].first;
heap[N].second=N;
c=N;
p=c/2;
while(p && heap[c].first<heap[p].first) {
tmp=heap[c];
heap[c]=heap[p];
heap[p]=tmp;
aux=poz[heap[c].second];
poz[heap[c].second]=poz[heap[p].second];
poz[heap[p].second]=aux;
c=p;
p=c/2;
}
}
else
if(op==2) {
f>>pd;
pd=poz[pd];
N--;
heap[pd]=heap[N+1];
poz[heap[pd].second]=pd;
c=pd;
p=c/2;
while(p && heap[c].first<heap[p].first) {
tmp=heap[c];
heap[c]=heap[p];
heap[p]=tmp;
aux=poz[heap[c].second];
poz[heap[c].second]=poz[heap[p].second];
poz[heap[p].second]=aux;
c=p;
p=c/2;
}
p=pd;
c=2*p;
while(c<=N && heap[c].first<heap[p].first) {
if(c<N && heap[c].first>heap[c+1].first)
c++;
tmp=heap[c];
heap[c]=heap[p];
heap[p]=tmp;
aux=poz[heap[c].second];
poz[heap[c].second]=poz[heap[p].second];
poz[heap[p].second]=aux;
p=c;
c=p*2;
}
}
else
g<<heap[1].first<<"\n";
}
return 0;
}