Cod sursa(job #967321)

Utilizator dropsdrop source drops Data 27 iunie 2013 15:28:23
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include <bitset>
#include <queue>
#include <deque>
#include <list>
#include <ctime>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream ff("heapuri.in");
ofstream gg("heapuri.out");
#define gg cout
#define max 200001
int n, k, r, pp[max];
struct hp{ 
	hp(int x0=0,int k0=0,int r0=0){ x=x0; k=k0; pp[k]=r0; }
	int x, k;
} hh[max];
bool operator<(const hp& a, const hp& b){ return a.x<b.x; }
void swp(int t,int f){ swap(pp[hh[t].k],pp[hh[f].k]); swap(hh[t] ,hh[f]); }

void ins(int x, int k){
	int t, f;
	hh[++r]=hp(x,k,r);
	f=r; t=f/2;
	while(t>0 && hh[f]<hh[t]){
		swp(t, f);
		f=t; t=f/2;
	}
}

void del(int k){
	int t, f;
	swp(k, r--);
	t=k; f=2*t;
	if(f<r && hh[f+1]<hh[f])f++;
	while(f<=r && hh[f]<hh[t]){
		swp(t, f);
		t=f; f=2*t;
		if(f<r && hh[f+1]<hh[f])f++;
	}
}

int main(){
	int op, x;
	ff >> n;
	for(int i=0;i<n;i++){
		ff >> op;
		if(op==3) gg << hh[1].x << "\n"; else {
		ff >> x;
		if(op==1) ins(x,++k); else del(pp[x]);
		}
	}
	return 0;
}