Pagini recente » Cod sursa (job #34545) | Cod sursa (job #1287835) | Cod sursa (job #1746541) | Cod sursa (job #2100124) | Cod sursa (job #2899049)
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
ifstream f ("zeap.in");
ofstream g ("zeap.out");
unordered_map<int, int> arbore;
string aux;
vector<int> heap;
int x,maxim = -1;
void inserare(int x){
if(!arbore[x]){
heap.push_back(x);
int i = heap.size()-1;
arbore[x] = i;
while(heap[i/2] > heap[i]){
arbore[x] = i/2;
arbore[heap[i/2]] = i;
swap(heap[i/2],heap[i]);
i /= 2;
}
}
if(maxim < x) maxim = x;
}
void stergere(int x){
if(!arbore[x]){
g<<"-1\n";
}
else{
if(x == maxim) maxim = -1;
int i = heap.size()-1, x2 = heap[i];
arbore[heap[i]] = arbore[x];
heap[arbore[x]] = heap[i];
arbore[x] = 0;
heap.pop_back();
x = x2;
i = arbore[x];
while(heap[i/2] > heap[i]){
arbore[x] = i/2;
arbore[heap[i/2]] = i;
swap(heap[i/2],heap[i]);
i /= 2;
}
bool ok = false;
while(!ok){
if(i*2+1 < heap.size()){
if(heap[i] > heap[i*2+1] || heap[i] > heap[i*2]){
if(heap[i*2] > heap[i*2+1]){
swap(arbore[heap[i*2+1]],arbore[heap[i]]);
swap( heap[i*2+1], heap[i]);
i = i*2+1;
}
else {
swap(arbore[heap[i*2]],arbore[heap[i]]);
swap( heap[i*2], heap[i]);
i *= 2;
}
}
else ok = true;
}
else if(i*2 < heap.size()){
if(heap[i*2] < heap[i]){
swap(arbore[heap[i*2]],arbore[heap[i]]);
swap( heap[i*2], heap[i]);
i *= 2;
}
ok = true;
}
else ok = true;
}
}
}
void MAX(){
x = heap.size();
if(x >= 3){
if(maxim != -1){
g<<maxim - heap[1]<<'\n';
}
else{
for(int i = x-1; i >= (x-1)/2+1 ;i--){
if(heap[i]> maxim) maxim = heap[i];
}
g<<maxim - heap[1]<<'\n';
}
}
else g<<"-1\n";
}
void MIN(){
x = heap.size();
if(x >= 3){
if(x >= 4 && heap[3] < heap[2]) g<<heap[3]-heap[1]<<'\n';
else g<<heap[2]-heap[1]<<'\n';
}
else g<<"-1\n";
}
int main() {
heap.push_back(0);
while(!f.eof()){
f>>aux;
if(aux == "MAX")MAX();
else if(aux == "MIN")MIN();
else if(aux == "S"){
f>>x;
stergere(x);
}
else if(aux == "C"){
f>>x;
if(arbore[x]) g<<"1\n";
else g<<"0\n";
}
else if(aux == "I"){
f>>x;
inserare(x);
}
}
return 0;
}