Pagini recente » Cod sursa (job #1141054) | Cod sursa (job #1913491) | Cod sursa (job #2066477) | Cod sursa (job #2545855) | Cod sursa (job #3174882)
#include <bits/stdc++.h>
#define NMAX 1002
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
int n;
pair<int,int> H[NMAX];
int poz[NMAX];
void Insert(int value,int pos) {
if (pos==1) {
H[pos]={value,n};
poz[n]=pos;
return;
}
if (!(value>=H[pos/2].first)) H[pos]=H[pos/2], poz[H[pos/2].second]=pos, Insert(value,pos/2);
else H[pos]={value,n}, poz[n]=pos;
}
void Combine(int pos) {
int x=H[pos].first,time=H[pos].second, tata=pos, fiu=2*tata;
while (fiu<=n) {
if (fiu+1<=n && H[fiu+1].first<H[fiu].first) fiu++;
if (H[fiu].first<x) {
H[tata]=H[fiu]; poz[H[fiu].second]=tata;
tata=fiu;
fiu=2*tata;
}
else break;
}
H[tata]={x,time}; poz[time]=tata;
}
int main()
{
int t;
fin>>t;
while (t--) {
int tip,x;
fin>>tip;
if (tip!=3) fin>>x;
switch (tip) {
case 1:
Insert(x,++n);
break;
case 2:
H[poz[x]]=H[n--];
Combine(poz[x]);
break;
case 3:
fout<<H[1].first<<'\n';
break;
}
}
return 0;
}