Pagini recente » Cod sursa (job #2356934) | Cod sursa (job #2812420) | Cod sursa (job #1996636) | Cod sursa (job #2982421) | Cod sursa (job #1025299)
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
//#include <cmath>
#include <queue>
#include <deque>
#include <iomanip>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define baza 10
#define MAX 201000
typedef long long int lli;
int x[MAX], heap[MAX], fr[MAX], z;
void put(int i)
{
heap[++z]=i;
fr[i]=z;
int c=z;
while(x[heap[c]]<x[heap[c/2]] && c>=1)
{
swap(heap[c],heap[c/2]);
swap(fr[heap[c]],fr[heap[c/2]]);
c/=2;
}
}
void elimina(int i)
{
swap(heap[i],heap[z]);
swap(fr[heap[i]],fr[heap[z]]);
z--;
if(i==z+1)
return;
int c=i;
while(x[heap[c]]<x[heap[c/2]] && c>=1)
{
swap(heap[c],heap[c/2]);
swap(fr[heap[c]],fr[heap[c/2]]);
c/=2;
}
while((x[heap[c]]>x[heap[2*c]] || x[heap[c]]>x[heap[2*c+1]]) && 2*c<=z)
{
if(x[heap[c]]>x[heap[2*c]])
{
swap(heap[c],heap[c*2]);
swap(fr[heap[c]],fr[heap[c*2]]);
c=c*2;
}
else
{
if(2*c+1>z)
break;
swap(heap[c],heap[c*2+1]);
swap(fr[heap[c]],fr[heap[c*2+1]]);
c=c*2+1;
}
}
}
int main()
{
int i, n=0,l,s, t,j;
fin>>t;
for(i=1;i<=t;i++)
{
fin>>l;
if(l==1)
{
fin>>s;
n++;
x[n]=s;
put(n);
}
if(l==2)
{
fin>>s;
elimina(fr[s]);
}
if(l==3)
{
fout<<x[heap[1]]<<"\n";
}
}
return 0;
}