Cod sursa(job #2878837)

Utilizator Vali_nnnValentin Nimigean Vali_nnn Data 27 martie 2022 22:54:33
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
long long heap[200001],a[200001],ok[1000001];
int n,m,i,p,q,k,j;
int cmp(int x,int y)
{return x<y;

}
void Swap(int p,int q)
{
    swap(heap[p],heap[q]);
}
void upheap(int x)
{
    int papa = x / 2;
    if(papa&& cmp(heap[x],heap[papa]))
    {
        Swap(x, papa);
        upheap(papa);
    }
}
void downheap(int x)
{
    int bebe = x * 2;
    if(bebe + 1 <=n&& cmp(heap[bebe + 1],heap[bebe]))
        bebe= bebe+1;
    if(bebe <=n && cmp(heap[bebe], heap[x]))
    {
        Swap(bebe, x);
        downheap(bebe);
    }
}
void Insert(int k)
{n++;
 heap[n]=k;
 upheap(n);
}
int main()
{f>>m;
for(i=1;i<=m;i++)
{f>>p;
if(p==1)
{k++;
    f>>q;
    a[k]=q;
    ok[q]++;
    Insert(q);

}
if(p==2)
{f>>q;
if(ok[a[q]]>1&&a[q]!=heap[1])
    ok[a[q]]--;
if(ok[a[q]]==1&&a[q]!=heap[1])
    ok[a[q]]--;

if(a[q]=heap[1]&&ok[a[q]]>=1)
{ok[a[q]]==0;

Swap(1,n);
n--;
downheap(1);

}


while(ok[heap[1]]==0)
    {

Swap(1,n);
n--;
downheap(1);

}
}
   if(p==3)
   {
g<<heap[1];
g<<'\n';
   }
}



}