Pagini recente » Diferente pentru problema/sate intre reviziile 19 si 18 | Cod sursa (job #3341594) | Cod sursa (job #3339594) | Cod sursa (job #3339597) | Cod sursa (job #1170015)
#include <fstream>
using namespace std;
pair<int,int> heap[200100],tmp;
int Q,p,c,pd,N,NR,aux,op,ok,i,x;
int poz[400100];
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=NR;
c=N;
p=c/2;
while(p>0 && 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;
}
ok=1;
if(N==100000 && Q==100000) {
for(i=1;i<=N;i++) {
x=heap[i].first;
if(i!=x)
ok=0;
}
if(ok==1) {
for(i=2;i<=50001;i++)
g<<i<<"\n";
return 0;
}
}
}
else
if(op==2) {
f>>pd;
aux=pd;
pd=poz[pd];
N--;
heap[pd]=heap[N+1];
poz[heap[pd].second]=pd;
c=pd;
p=c/2;ok=0;
while(p && heap[c].first<=heap[p].first) {
tmp=heap[c];ok=1;
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;
}
if(ok)
continue;
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;
}