Cod sursa(job #2562746)

Utilizator RedPipperNastasa Stefan-Alexandru RedPipper Data 29 februarie 2020 17:46:49
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.32 kb
#include <fstream>
	
#define INF 1e6
	
using namespace std;
	
 
	
ifstream fin("heapuri.in");
	
ofstream fout("heapuri.out");
	
 
	
const int MAXSIZE = 2e5;
	
 
	
typedef int Heap[MAXSIZE];
	
int history[MAXSIZE], poz[MAXSIZE];
	
Heap a;
	
 
	
//basic methods
	
inline int father(int nod)
	
{
	
    return nod/2;
	
}
	
 
	
inline int left_son(int nod)
	
{
	
    return nod*2;
	
}
	
 
	
inline int right_son(int nod)
	
{
	
    return nod*2+1;
	
}
	
 
	
inline int minOf(Heap H)
	
{
	
    return H[1];
	
}
	
 
	
//done basic methods
	
 
	
void cerneNod(Heap H, int nod, int siz3)
	
{
	
    int son;
	
    do
	
    {
	
        //imi ia cel mai mic fiu care exista in heap
	
        son = 0;
	
 
	
        if(left_son(nod) <= siz3)
	
        {
	
            son = left_son(nod);
	
            if(right_son(nod) <=siz3 && H[right_son(nod)]<=H[left_son(nod)])
	
                son = right_son(nod);
	
 
	
 
	
            //daca cel mai mic fiu este mai mare decat parintele
	
            //nu imi ia nici un fiu
	
            if(H[son] >= H[nod])
	
                son = 0;
	
        }
	
 
	
        if(son)
	
        {
            
            swap(H[son],H[nod]);
            poz[nod] = H[nod];
            nod = son;
	
        }
	
 
	
    }while(son);
	
 
	
}
	
 
	
void urcaNod(Heap H, int nod)
	
{
	
    int temp = H[nod];
    while(nod>1 && (temp<H[father(nod)]))
	
    {
	
        H[nod] = H[father(nod)];
        poz[nod] = H[nod];
        nod = father(nod);
    }
	
    poz[nod] = temp;
    H[nod] = temp;
}
	
 
	
int pleasFind(Heap H,int x)
	
{
	
    int nod = history[x];
	
    int i=1;
	
    while(nod != H[i])
	
       ++i;
	
 
	
    return i;
	
}
	
 
	
int main()
	
{
	
    int n;
	
    fin>>n;
	
    while(n--)
	
    {
	
        int op;
	
        fin>>op;
	
        int x;
	
        int fnd;
	
        switch(op)
	
        {
	
            case 1:
	
                fin>>x;
	
                a[0]++;
	
                a[a[0]] = x;
                history[++history[0]] = x;
                poz[history[history[0]]] = a[0];
	
                urcaNod(a, a[0]);
	
                break;
	
            case 2:
	
                fin>>x;
	
                a[poz[x]] = INF;
	
                cerneNod(a,poz[x],a[0]);
	
                break;
	
 
	
            case 3:
	
                fout<<minOf(a)<<endl;
	
                break;
	
        }
	
    }
	
    return 0;
	
}