Cod sursa(job #3244732)

Utilizator FRD233Fodor Rares-Costin FRD233 Data 26 septembrie 2024 11:11:26
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <bits/stdc++.h>
using namespace std;
struct pereche{ int val;
                int ord;
              }H[200001];
int n,m,op,x,k,v[200001];
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int mycmp(pereche a, pereche b)
{   return  a.val<b.val;
}
void comb(int i, int n)
{   pereche x=H[i];
    int tata=i,fiu=2*i;
    while(fiu<=n)
    {   if(fiu<n && mycmp(H[fiu+1],H[fiu])) fiu++;
        if(mycmp(H[fiu],x))
        {   H[tata]=H[fiu];
            v[H[tata].ord]=fiu;
            tata=fiu;
            fiu=2*tata;
        }
        else fiu=n+1;
    }
    H[tata]=x;
    v[x.ord]=tata;
}
void insert_heap(pereche y)
{   n++;
    int tata=n/2,fiu=n;
    while(tata && mycmp(y,H[tata]))
    {   H[fiu]=H[tata];
        v[H[fiu].ord]=v[H[tata].ord];
        v[H[tata].ord]=fiu;
        fiu=tata;
        tata=fiu/2;
    }
    H[fiu]=y;
    v[y.ord]=fiu;
}
void stergere_heap(int i)
{   H[i]=H[n];
    v[H[n].ord]=1;
    n--;
    int tata=i,fiu=2*i;
    pereche x=H[i];
    while(fiu<=n)
    {   if(fiu<n && mycmp(H[fiu+1],H[fiu])) fiu++;
        if(mycmp(H[fiu],x))
        {   H[tata]=H[fiu];
            v[H[tata].ord]=fiu;
            tata=fiu;
            fiu=2*tata;
        }
        else fiu=n+1;
    }
    H[tata]=x;
    v[x.ord]=tata;
}
int main()
{   f>>m;
    while(m--)
    {   f>>op;
        if(op==1)
        {   f>>x;
            v[++k]=k;
            pereche y={x,k};
            insert_heap(y);
        }
        else if(op==2)
            {   f>>x;
                stergere_heap(v[x]);
            }
            else g<<H[1].val<<'\n';
    }

    return 0;
}