Pagini recente » Cod sursa (job #2409206) | Cod sursa (job #1827271) | Cod sursa (job #999460) | Cod sursa (job #2388368) | Cod sursa (job #3245166)
#include <bits/stdc++.h>
const std::string FILENAME = "heapuri";
std::ifstream f(FILENAME + ".in");
std::ofstream g(FILENAME + ".out");
const int NMAX = 2e5 + 5;
int n;
int cer;
int x;
int q;
int cnt;
int v[NMAX]; // pozitia elementului al i lea in heap
std :: pair<int, int> h[2 * NMAX];
int sus(int x)
{
cnt++;
int ret = cnt;
h[cnt].first = x;
h[cnt].second = q;
while (ret > 1 && h[ret].first < h[ret / 2].first)
{
std :: swap(v[h[ret].second], v[h[ret / 2].second]);
std :: swap(h[ret], h[ret / 2]);
ret /= 2;
}
return ret;
}
void jos(int x)
{
int poz = v[x];
std :: swap(h[poz], h[cnt]);
std :: swap(v[h[poz].second], v[h[cnt].second]);
cnt --;
int k = poz;
while (k * 2 <= cnt)
{
int son = k * 2;
if (k * 2 + 1 <= cnt && h[k * 2].first > h[k * 2 + 1].first)
{
son = k * 2 + 1;
}
if (h[k].first <= h[son].first)
{
break;
}
std :: swap(v[h[k].second], v[h[son].second]);
std :: swap(h[k], h[son]);
k = son;
}
}
int main()
{
f >> n;
for (int i = 1; i <= n; i++)
{
f >> cer;
if (cer == 1)
{
q ++;
f >> x;
v[q] = sus(x);
}
else if (cer == 2)
{
f >> x;
jos(x);
}
else
{
g << h[1].first << '\n';
}
}
return 0;
}