Pagini recente » Cod sursa (job #1976486) | Cod sursa (job #3270117) | Cod sursa (job #35342) | Cod sursa (job #718093) | Cod sursa (job #2896441)
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
#define cin f
#define cout g
const int Max = 2e5 + 1;
void nos()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int k, heap[Max], arr[Max];
set < int > lazy;
void urca(int poz)
{
while(poz != 1)
{
int tata = poz / 2;
if(heap[tata] > heap[poz])
{
swap(heap[tata], heap[poz]);
poz = tata;
}
else
break;
}
}
void push(int x)
{
heap[++ k] = x;
urca(k);
}
void coboara(int poz)
{
if(poz * 2 > k)
return;
int left = heap[poz * 2], right = -1;
if(poz * 2 + 1 <= k)
right = heap[poz * 2 + 1];
if(right == -1 or left < right)
{
if(left < heap[poz])
{
swap(heap[poz], heap[poz * 2]);
coboara(poz * 2);
}
}
else
{
if(right < heap[poz])
{
swap(heap[poz], heap[poz * 2 + 1]);
coboara(poz * 2 + 1);
}
}
}
void pop(int poz)
{
swap(heap[poz], heap[k]);
k --;
coboara(poz);
}
int main()
{
nos();
int n, m = 0;
cin >> n;
for(int i = 1; i <= n; i ++)
{
int op; cin >> op;
if(op == 1)
{
int x; cin >> x;
arr[++ m] = x;
push(x);
}
else if(op == 2)
{
int x; cin >> x;
lazy.insert(arr[x]);
}
else
{
cout<<heap[1]<<'\n';
}
while(k > 0 and lazy.size() > 0 and heap[1] == *lazy.begin())
{
pop(0);
lazy.erase(lazy.begin());
}
}
return 0;
}