Pagini recente » Cod sursa (job #1015240) | Cod sursa (job #820301) | Cod sursa (job #1431595) | Cod sursa (job #632478) | Cod sursa (job #915069)
Cod sursa(job #915069)
/***************************************************
* Alex Palcuie
* Romania - 2013
* alex [dot] palcuie [at] gmail [dot] com
* http://palcu.blogspot.com/
****************************************************/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <utility>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <cstring>
using namespace std;
// Defines
#define NAME(n) #n
#define pr(x) db((x), (#x))
#define prv(v,n) dbv((v), (#v), (n))
#define prw(v) dbw((v), (#v))
#define X first
#define Y second
#define pb push_back
// Helpers
template <typename T>
inline void db(const T x, const char * name){
cerr << name << " = " << x << '\n';
}
template <typename T> inline void dbv(const T * v, const char * name, int n){
fprintf(stderr, "=== %s ===\n", name);
for(int i=0; i<n; i++)
cerr << v[i] << " ";
cerr << '\n';
}
template <typename T> inline void dbs(T x){
cerr << x << ' ';
}
template<typename T>
void dbw(const std::vector<T>& t, const char * name){
fprintf(stderr, "=== %s ===\n", name);
unsigned n = t.size();
for(typename std::vector<T>::size_type i=0; i<n; ++i)
std::cerr << t[i] << ' ';
cerr << '\n';
}
// Structs
// Constants
const int N = 200008;
const int INF = 0x3f3f3f3f;
// Globals
int nOperatii, nHeap=1, location[N];
pair<int,int> heap[N];
// Functions
int left_son(int x){
return x * 2;
}
int right_son(int x){
return left_son(x)+1;
}
int father(int x){
return x/2;
}
void bubble_up(int x){
while(heap[x].X < heap[father(x)].X){
swap(heap[x], heap[father(x)]);
swap(location[heap[x].Y], location[heap[father(x)].Y]);
x = location[heap[father(x)].Y];
}
}
void add(int x, int index){
heap[nHeap].X = x;
heap[nHeap].Y = index;
location[index] = nHeap++;
bubble_up(location[index]);
}
bool isWrong(pair<int, int> x, pair<int,int> y){
return y.X && x.X>y.X;
}
void bubble_down(int x){
while(isWrong(heap[x], heap[left_son(x) ])
|| isWrong(heap[x], heap[right_son(x)])){
int newLocation = min(heap[left_son(x)], heap[right_son(x)]).X;
swap(location[heap[x].Y], location[heap[newLocation].Y]);
swap(heap[x], heap[newLocation]);
x = heap[x].Y;
}
}
void remove(int x){
int newLocation = location[x];
location[heap[nHeap-1].Y] = newLocation;
swap(heap[nHeap-1], heap[newLocation]);
nHeap--;
bubble_down(newLocation);
}
void print_min(){
printf("%d\n", heap[1].X);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
#endif
scanf("%d", &nOperatii);
heap[0].X = -INF; int nInserat = 1;
for(int i=1; i<=nOperatii; i++){
int op, x;
scanf("%d", &op);
switch(op){
case 1:
scanf("%d", &x);
add(x, nInserat++);
break;
case 2:
scanf("%d", &x);
remove(x);
break;
case 3:
print_min();
break;
}
}
return 0;
}