Pagini recente » Cod sursa (job #1668818) | Cod sursa (job #956136) | Cod sursa (job #2823721) | Cod sursa (job #896824) | Cod sursa (job #2296343)
#include<fstream>
#include<vector>
#include<string>
#define N 200000
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
// vector<bool> erased;
class Heap{
public:
int value;
int id;
Heap (int a = 0, int b = 0){
value = a;
id = b;
}
bool operator < (Heap a){
return (value < a.value);
}
// bool isErased(){
// return erased[id];
// }
};
vector<Heap> heap;
vector<int> pos;
// void insertHeap(Heap x){
// heap.push_back(x);
// int p = heap.size() - 1;
// while(p > 0){
// if (heap[p] < heap[(p - 1) / 2]){
// swap(heap[p], heap[(p - 1) / 2]);
// p = (p - 1) / 2;
// }
// else break;
// }
// }
// void popHeap(){
// int x = heap[0].value;
// swap(heap[0], heap.back());
// heap.pop_back();
// int p = 0;
// while(p < heap.size()){
// //cout << p << ' ' << heap[p].value << endl;
// int fiu = p;
// if (p * 2 + 1 < heap.size() && heap[p * 2 + 1] < heap[fiu]){
// fiu = 2 * p + 1;
// }
// if (p * 2 + 2 < heap.size() && heap[p * 2 + 2] < heap[fiu]){
// fiu = p * 2 + 2;
// //cout << 2 * p + 2 << ' ' << heap[2 * p + 2].value << endl;
// }
// //if (x == 12) cout << p << ' ' << fiu << endl;
// if (fiu == p) break;
// swap(heap[p], heap[fiu]);
// p = fiu;
// }
// }
void moveup(int &p){
while(p > 0){
if (heap[p] < heap[(p - 1) / 2]){
swap(heap[p], heap[(p - 1) / 2]);
swap(pos[heap[p].id], pos[heap[(p - 1) / 2].id]);
p = (p - 1) / 2;
}
else break;
}
}
void movedown(int &p){
while(p < heap.size()){
int fiu = p;
if (p * 2 + 1 < heap.size() && heap[p * 2 + 1] < heap[fiu]){
fiu = 2 * p + 1;
}
if (p * 2 + 2 < heap.size() && heap[p * 2 + 2] < heap[fiu]){
fiu = p * 2 + 2;
}
if (fiu == p) break;
swap(heap[p], heap[fiu]);
swap(pos[heap[p].id], pos[heap[fiu].id]);
p = fiu;
}
}
void insert(int x){
// while(!heap.empty() && heap[0].isErased()){
// //if (heap[0].value == 12) cout << heap[1].value << ' ' <<heap[2].value << endl;
// popHeap();
// }
//cout << x << ' ' << erased.size() << endl;
// insertHeap(Heap(x, erased.size()));
// erased.push_back(false);
pos.push_back(heap.size());
heap.push_back(Heap(x, pos.size() - 1));
int p = heap.size() - 1;
moveup(p);
}
void erase(int x){
// erased[x] = true;
// while(!heap.empty() && heap[0].isErased()){
// //if (heap[0].value == 12) cout << heap[1].value << ' ' <<heap[2].value << endl;
// popHeap();
// }
int p = pos[x];
if (p == heap.size() - 1){
heap.pop_back();
return ;
}
swap(heap[p], heap.back());
swap(pos[heap[p].id], pos[heap.back().id]);
heap.pop_back();
moveup(p);
movedown(p);
}
int query(){
// while(!heap.empty() && heap[0].isErased()){
// //if (heap[0].value == 12) cout << heap[1].value << ' ' <<heap[2].value << endl;
// popHeap();
// }
if (!heap.empty()) return heap[0].value;
else return -1;
}
string s;
int p;
int getInt(){
if (p == s.size()){
cin >> s;
p = 0;
}
while(s[p] == ' ') p++;
int nr = 0;
while(p < s.size() && s[p] >= '0' && s[p] <= '9'){
nr = nr * 10 + s[p] - '0';
p++;
}
return nr;
}
int main(){
int n = getInt();
//cin >> n;
for(int i = 1; i <= n; i++){
int op = getInt();
//cin >> op;
if (op == 3) cout << query() << '\n';
else {
int x = getInt();
//cin >> x;
if (op == 1) insert(x);
if (op == 2) erase(x - 1);
}
}
return 0;
}