Cod sursa(job #2098366)

Utilizator Lazar_LaurentiuLazar Laurentiu Lazar_Laurentiu Data 2 ianuarie 2018 18:55:58
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
#define MAX 200001
#define VMAX 2000000000
#define x first
#define y second

using namespace std;

int n,pos[MAX],m,a,p,o,ord,pa;
pair<int,int> h[MAX];
void schimba(int p1,int p2){
  swap(h[p1],h[p2]);
  swap(pos[h[p1].y],pos[h[p2].y]);
}
void o1(int v){
  m++;
  h[m]=make_pair(v,ord);
  pos[ord]=m;
  p=m;
  while(p/2>0&&h[p].x<h[p/2].x){
    schimba(p,p/2);
    p=p/2;
  }
}
void o2(int v){
  schimba(m,v);m--;
  p=v;
  while(p*2<=m){
    pa=0;
    if(h[p*2].x<h[p].x)pa=p*2;
    if(p*2+1<=m&&h[p*2+1].x<h[p].x&&h[p*2+1].x<h[p*2].x)pa=p*2+1;
    if(pa==0)break;
    schimba(p,pa);p=pa;
  }
}

int main()
{
    ifstream f ("heapuri.in");
    ofstream g ("heapuri.out");
    f>>n;h[0]=make_pair(VMAX,VMAX);
    for(int i=1;i<=n;i++){
      f>>o;
      if(o==1){
        f>>a; ord++; o1(a);
      } else if(o==2){
        f>>a;
        o2(pos[a]);
      } else g<<h[1].x<<'\n';
//      for(int j=1,p2=1;j<=m;j++){
//        cout<<h[j].x<<" ";
//        if(j==p2)cout<<'\n',p2=p2+p2*2;
//      }
//      cout<<"\n-----------------------\n";
    }
    f.close ();
    g.close ();
    return 0;
}