Cod sursa(job #2808488)

Utilizator vladburacBurac Vlad vladburac Data 25 noiembrie 2021 10:37:01
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <stdio.h>
using namespace std;
#define NMAX 200000

pair <int, int> heap[NMAX+1];
int sters[NMAX+1];
int heapSize = 1, n = 1;

void adauga( int val ) {
  int i;
  heap[heapSize].first = val;
  heap[heapSize].second = n;
  heapSize++;
  n++;
  i = heapSize - 1;
  while( i && heap[i] < heap[i/2] ) {
    swap( heap[i], heap[i/2] );
    i = i / 2;
  }
}

void sterge( int poz ) {
  heap[poz] = heap[heapSize-1];
  heapSize--;
  while( 2 * poz < heapSize && heap[poz] > min( heap[2*poz], heap[2*poz+1] ) ) {
    if( heap[2*poz] < heap[2*poz+1] ) {
      swap( heap[poz], heap[2*poz] );
      poz = 2 * poz;
    }
    else {
      swap( heap[poz], heap[2*poz+1] );
      poz = 2 * poz + 1;
    }
  }
}

int main() {
  FILE *fin, *fout;
  int q, op, x;
  fin = fopen( "heapuri.in", "r" );
  fout = fopen( "heapuri.out", "w" );
  fscanf( fin, "%d", &q );
  while( q-- ) {
    fscanf( fin, "%d", &op );
    if( op == 1 ) {
      fscanf( fin, "%d", &x );
      adauga(x);
    }
    else if( op == 2 ) {
      fscanf( fin, "%d", &x );
      sters[x] = 1;
    }
    else {
      while( sters[heap[1].second] )
        sterge(1);
      fprintf( fout, "%d\n", heap[1].first );
    }
    /* for( i = 1; i < heapSize; i++ )
      printf( "%d ", heap[i] );
    printf( "\n" );
    for( i = 1; i < heapSize; i++ )
      printf( "%d ", ind2[i] );
    printf( "\n" );
    printf( "\n" ); */
  }
  fclose( fin );
  fclose( fout );
  return 0;
}