Pagini recente » Cod sursa (job #2487453) | Cod sursa (job #655749) | Cod sursa (job #2479567) | Cod sursa (job #113216) | Cod sursa (job #1243050)
#include <iostream>
#include <vector>
#define MAX 200000
using namespace std;
int N, K;
vector<int> H(MAX, 0);
vector<int> inserted_nodes(MAX, 0);
int father(int node){
return node/2;
}
int left_son(int node){
return 2*node;
}
int right_son(int node){
return node*2+1;
}
void sift(int node){
int son;
do {
son = 0;
// Alege un fiu mai mare ca tatal.
if (left_son(node) < N) {
son = left_son(node);
if (right_son(node) < N && H[right_son(node)] < H[left_son(node)]) {
son = right_son(node);
}
if (H[son] >= H[node]) {
son = 0;
}
}
if (son) {
swap(H[node], H[son]);
node = son;
}
} while (son);
}
void percolate(int node){
int f, key = H[node];
while(node > 1 && key < H[f = father(node)]){
H[node] = H[f];
node = f;
}
H[node] = key;
}
void insertH(int elem){
H[N] = elem;
percolate(N);
N++;
}
int find(int elem, int node){
if(node >= N) return 0;
if(H[node] == elem) return node;
if(H[2*node] > elem)
return find(elem, 2*node+1);
if(H[2*node+1] > elem)
return find(elem, 2*node);
return max(find(elem, 2*node), find(elem, 2*node+1));
}
void deleteH(int elem){
int index = find(elem, 1);
H[index] = H[N-1];
N--;
if(index > 1 && H[index] < H[father(index)]){
percolate(index);
}else{
sift(index);
}
}
int main(){
int M, op_code, elem, ith;
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%d", &M);
K = 1, N = 1;
for(int i = 0; i < M; i++){
scanf("%d", &op_code);
switch(op_code){
case 1:
scanf("%d", &elem);
inserted_nodes[K++] = elem;
insertH(elem);
break;
case 2:
scanf("%d", &ith);
deleteH(inserted_nodes[ith]);
inserted_nodes[ith] = -1;
break;
case 3:
printf("%d\n", H[1]);
}
}
return 0;
}