Pagini recente » Cod sursa (job #2369223) | Cod sursa (job #63926) | Cod sursa (job #628216) | Cod sursa (job #2427033) | Cod sursa (job #1093692)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class Heap {
public:
Heap() {}
int insert(int x) {
v.push_back(x);
pos[x] = v.size() - 1;
pushup(v.size() - 1);
}
int min() {
return v[0];
}
void remove(int x) {
pushdown(pos[x]);
}
private:
vector<int> v;
map<int, int> pos;
void pushdown(int p) {
if (p * 2 + 1 >= v.size()) {
pos.erase(v[p]);
return ;
}
int s = p * 2 + 1;
if (s+1 < v.size() && v[s+1] < v[s]) {
s ++;
}
this->swap(p, s);
pushdown(s);
}
void pushup(int p) {
#ifdef __DEBUG__
cout << p << "# ";
print();
cout << endl;
#endif
if (p == 0) return;
int t = p / 2;
if (v[t] > v[p]) {
this->swap(p, t);
pushup(t);
}
}
void swap(int p1, int p2) {
int v1 = v[p1],
v2 = v[p2];
std::swap(pos[v1], pos[v2]);
std::swap(v[p1], v[p2]);
}
int at(int p) {
if (v.size() < p) {
v.resize(p+1);
}
return v[p];
}
void print() {
for (int i=0; i<v.size(); ++i) {
cout << v[i] << " ";
}
}
};
int main() {
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int nq;
Heap h;
vector<int> chron;
in >> nq;
while (nq --) {
int op, val;
in >> op;
switch (op) {
case 1:
in >> val;
chron.push_back(val);
h.insert(val);
break;
case 2:
in >> val;
h.remove(chron[val-1]);
break;
case 3:
out << h.min() << "\n";
break;
}
}
out.close();
return 0;
}