Pagini recente » Cod sursa (job #1869512) | Cod sursa (job #520805) | Cod sursa (job #2865282) | testroundid | Cod sursa (job #2077724)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
#define R scanf
#define P printf
int heap[maxn], dimh;///invheap imi spune ce cheie are x
int v[maxn], n;
int invheap[maxn];
inline void SWAP(int noda, int nodb) {
swap(heap[noda], heap[nodb]);
swap(invheap[heap[noda]], invheap[heap[nodb]]);
}
inline void upheap(int nod) {
while(nod > 1 && v[heap[nod]] < v[heap[nod / 2]]) {
SWAP(nod, nod / 2);
nod /= 2;
}
}
inline void downheap(int nod) {
while(1) {
int lson = nod * 2;
int rson = nod * 2 + 1;
if(lson <= dimh) {
if(rson <= dimh) {
if(v[heap[lson]] >= v[heap[nod]] && v[heap[rson]] >= v[heap[nod]])
return;
if(v[heap[lson]] <= v[heap[rson]]) {
SWAP(lson, nod);
nod = lson;
}
else {
SWAP(rson, nod);
nod = rson;
}
}
else {
if(v[heap[lson]] >= v[heap[nod]])
return;
SWAP(lson, nod);
nod = lson;
}
}
else {
return;
}
}
}
int main() {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
int nrop;
R("%d", &nrop);
while(nrop--) {
int o, x;
R("%d", &o);
if(o == 1) {
R("%d", &x);
v[++n] = x;
dimh++;
heap[dimh] = n;
invheap[n] = dimh;
upheap(dimh);//urc pozitia dimh
}
else if(o == 2) {
R("%d", &x);///trb sa tai v[x]
int nod = invheap[x];
SWAP(nod, dimh);
dimh--;
downheap(nod);
}
else {
P("%d\n", v[heap[1]]);
}
}
return 0;
}