Cod sursa(job #2577296)

Utilizator Iulia25Hosu Iulia Iulia25 Data 8 martie 2020 22:33:27
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>

using namespace std;

ifstream cin ("heapuri.in");
ofstream cout ("heapuri.out");
int poz[200005];
struct heap
{
 int intrat,val;
}h[200005];
int t,k, n;
void inserare(int x)
{
 h[n+1].val=x;
 t++;
 h[n+1].intrat=t;
 n++;
 poz[t]=n;
 bool promovare=true;
 while(promovare==true)
 {
  if(h[n].val<h[n/2].val && n > 1)
  {
   heap aux=h[n];
   h[n]=h[n/2];
   h[n/2]=aux;
   int auxpoz=poz[h[n].intrat];
   poz[h[n].intrat]=poz[h[n/2].intrat];
   poz[h[n/2].intrat]=auxpoz;
  }
  else
  promovare=false;
 }
}
void stergere(int timp)
{
 int pozdesters=poz[timp];
 poz[h[n].intrat] = pozdesters;
 h[pozdesters]=h[n];
 h[n]={0,0};
 n--;
 bool cernere=true;
 int i=pozdesters;
 while(cernere==true)
 {
  if((h[i*2].val<h[i*2+1].val && i*2+1<=n || i*2+1>n && i*2<=n) && h[i*2].val<h[i].val)
  {
   swap(h[i*2],h[i]);
   swap(poz[h[i*2].intrat],poz[h[i].intrat]);
  }
  else
  if((h[i*2+1].val<h[i*2].val && i*2+1<=n) && h[i*2+1].val<h[i].val)
  {
   swap(h[i*2+1],h[i]);
   swap(poz[h[i*2+1].intrat],poz[h[i].intrat]);
  }
  else
  cernere=false;
 }
}
int main()	{
  int n,tip,x,timp;
  cin>>n;
  for(int i=1;i<=n;++i)
  {
   cin>>tip;
   if(tip==1)
   {
    cin>>x;
    inserare(x);
   }
   else
   if(tip==2)
   {
    cin>>timp;
    stergere(timp);
   }
   else
   cout<<h[1].val<<'\n';
  }
  return 0;
}