Cod sursa(job #2948831)

Utilizator Ilie_MityIlie Dumitru Ilie_Mity Data 28 noiembrie 2022 15:53:33
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.97 kb
//Duck on the grind
#include<cstdio>
#include<cstring>
typedef signed long long int ll;
const ll MOD=998244353;
const int NMAX=200005;

struct el
{
	int x, pos;
};

el heap[NMAX];
int Nheap;
int posHeap[NMAX];

int min() {return heap[0].x;}

void insert(el a)
{
	int pos=Nheap;
	while(pos && heap[(pos-1)>>1].x>a.x)
	{
		heap[pos]=heap[(pos-1)>>1];
		posHeap[heap[pos].pos]=pos;
		pos=(pos-1)>>1;
	}
	heap[pos]=a;
	posHeap[a.pos]=pos;
	++Nheap;
}

bool push_up(int pos)
{
	bool up=0;
	el a=heap[pos];
	while(pos && heap[(pos-1)>>1].x>a.x)
	{
		heap[pos]=heap[(pos-1)>>1];
		posHeap[heap[pos].pos]=pos;
		pos=(pos-1)>>1;
		up=1;
	}
	heap[pos]=a;
	posHeap[a.pos]=pos;
	return up;
}

void push_down(int pos)
{
	bool ok=1;
	el a=heap[pos];
	while((pos<<1)+2<Nheap && ok)
	{
		if(heap[(pos<<1)|1].x<heap[(pos<<1)+2].x)
		{
			if(heap[(pos<<1)|1].x<a.x)
			{
				heap[pos]=heap[(pos<<1)|1];
				posHeap[heap[pos].pos]=pos;
				pos=(pos<<1)|1;
			}
			else
				ok=0;
		}
		else
		{
			if(heap[(pos<<1)+2].x<a.x)
			{
				heap[pos]=heap[(pos<<1)+2];
				posHeap[heap[pos].pos]=pos;
				pos=(pos<<1)+2;
			}
			else
				ok=0;
		}
	}
	if(((pos<<1)|1)<Nheap && heap[(pos<<1)|1].x<a.x)
	{
		heap[pos]=heap[(pos<<1)|1];
		posHeap[heap[pos].pos]=pos;
		pos=(pos<<1)|1;
	}
	heap[pos]=a;
	posHeap[a.pos]=pos;
}

void remove(int pos)
{
	//swap heap[posHeap[pos]], heap[--Nheap]
	pos=posHeap[pos];
	posHeap[heap[--Nheap].pos]=pos;
	heap[pos]=heap[Nheap];
	if(!push_up(pos))
		push_down(pos);
}

int main()
{
	freopen("heapuri.in", "r", stdin);
	freopen("heapuri.out", "w", stdout);
	int N, i, op, x, added=0;
	scanf("%d", &N);
	for(i=0;i<N;++i)
	{
		scanf("%d", &op);
		switch(op)
		{
		case 1:
			{
				scanf("%d", &x);
				insert({x, ++added});
				break;
			}
		case 2:
			{
				scanf("%d", &x);
				remove(x);
				break;
			}
		case 3:
			{
				printf("%d\n", min());
				break;
			}
		}
	}
	return 0;
}