Pagini recente » Borderou de evaluare (job #643076) | Cod sursa (job #1847144)
#include <iostream>
#include <fstream>
#include <vector>
#define MAX 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int h[MAX],n,x,y,p,m;
vector <int> val;
int dad(int k){
return k/2;
}
int l_son(int k){
return 2*k;
}
int r_son(int k){
return 2*k+1;
}
void cobor(int k){///sift
int son;
do{
son=0;
if(l_son(k)<=n){
son=l_son(k);
if(r_son(k) <= n && h[r_son(k)] > h[l_son(k)])
son=r_son(k);
if(h[son]<=h[k])
son=0;
}
if(son){
swap(h[k],h[son]);
k=son;
}
}while(son);
}
void urc(int k){///percolate
int key=h[k];
while(k>1 && key > h[dad(k)]){
h[k]=h[dad(k)];
k=dad(k);
}
h[k]=key;
}
void elimin(int k){
h[k]=h[n];
n--;
if(k>1 && h[k]>h[dad(k)])
urc(k);
else
cobor(k);
}
void inserare(int x){
n++;
h[n]=x;
urc(n);
}
int caut(int x, int k){
if(x==h[k])
return k;
else{
if(h[r_son(k)] < x)
return caut(x,l_son(k));
else{
if(h[l_son(k)] < x)
return caut(x,r_son(k));
else{
if(h[r_son(k)] > x && h[l_son(k)] > x){
return caut(x,l_son(k));
return caut(x,r_son(k));
}
}
}
}
}
int main()
{
fin>>m;
for(int i=1;i<=m;++i){
fin>>x;
if(x==1){
fin>>y;
inserare(-y);
val.push_back(-y);
}
if(x==2){
fin>>y;
int poz=caut(val[y-1],1);
elimin(poz);
}
if(x==3)
fout<<-h[1]<<'\n';
}
return 0;
}