Pagini recente » Cod sursa (job #2068543) | Cod sursa (job #153130) | Cod sursa (job #1799985) | Cod sursa (job #822051) | Cod sursa (job #2703791)
#include <bits/stdc++.h>
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define FOR(n) for(int i=0;i<n;i++)
#define vt vector
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define nax 100005
#define MXL 1000000000000000000
#define PI 3.14159265
#define pb push_back
#define pf push_front
#define sc second
#define fr first
#define int ll
#define endl '\n'
#define ld long double
struct bheap
{
vector<pair<int, int>> heap;
vector<int> pos;
stack<int> free;
int minimum()
{
return heap[0].fr;
}
void insert(int x)
{
heap.pb({x, pos.size()});
if(free.empty())
{
pos.pb(heap.size()-1);
}
else
{
pos.pb(free.top());
free.pop();
}
while(heap[(pos.back()-1)/2].fr > heap[pos.back()].fr && pos.back() != 0)
{
pos[heap[(pos.back()-1)/2].sc] = pos.back();
swap(heap[(pos.back()-1)/2], heap[pos.back()]);
pos.back() = (pos.back()-1)/2;
}
}
void remove_xth(int x)
{
int now = pos[x-1];
while(2*now+1 < heap.size())
{
if(2*now+2 >= heap.size())
{
swap(heap[now], heap[2*now+1]);
pos[heap[now].sc] = (pos[heap[now].sc]-1)/2;
now = 2*now+1;
}
else
{
if(heap[2*now+1].fr <= heap[2*now+2].fr)
{
swap(heap[now], heap[2*now+1]);
pos[heap[now].sc] = (pos[heap[now].sc]-1)/2;
now = 2*now+1;
}
else
{
swap(heap[now], heap[2*now+2]);
pos[heap[now].sc] = (pos[heap[now].sc]-1)/2;
now = 2*now+2;
}
}
}
heap[now].fr = MXL;
free.push(now);
}
};
int32_t main(){
startt;
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
bheap heap;
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
int c;
cin >> c;
if(c == 3)
{
cout << heap.minimum() << endl;
}
if(c == 2)
{
int x;
cin >> x;
heap.remove_xth(x);
}
if(c == 1)
{
int x;
cin >> x;
heap.insert(x);
}
}
}