Cod sursa(job #3237874)

Utilizator tsg38Tsg Tsg tsg38 Data 13 iulie 2024 21:43:50
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

ifstream fin( "heapuri.in" );
ofstream fout( "heapuri.out" );

const int DIM = 2e5 + 1;
const int INF = 2e9;

struct Heap {
  int val[DIM], idx;
  int tin[DIM], timer;
  int who[DIM];
  
  void init() {
	idx = timer = 0;
  }
  void swp( int u, int v ) {
	swap(val[u], val[v]);
    who[tin[u]] = v;
	who[tin[v]] = u;
	swap(tin[u], tin[v]);
  }
  int get_val( int node ) {
	if ( node > idx ) return INF;
    return val[node];
  }
  void fixup( int node ) {
    while ( node && val[node >> 1] > val[node] ) {
	  swp(node, node >> 1);
	  node >>= 1;
	}
  }
  void fixdown( int node ) {
	while ( node < idx ) {
	  int low = (get_val(2 * node) < get_val(2 * node + 1) ? 2 * node : 2 * node + 1);
      if ( val[node] <= get_val(low) ) {
		return;
	  }
	  swp(node, low);
	  node = low;
	}
  }
  void insert( int num ) {
	val[++idx] = num; 
	
	tin[idx] = ++timer;
	who[timer] = idx;

	fixup(idx);
  }
  void erase( int time ) {
    int u = who[time];
	swp(who[time], idx);
	--idx;
    fixdown(u);  
  }
  int get_min() {
	return val[1];
  }

  void print( int node, int dep = 0 ) {
	if ( node > idx ) return;
	print(2 * node, dep + 1);
    for ( int i = 1; i <= dep; ++i ) cout << " ";
	cout << val[node] << "\n"; 
	print(2 * node + 1, dep + 1);
  }
};

int main() {
  ios_base::sync_with_stdio(0);
  fin.tie(0);
  int q, t, x;
  Heap h;

  fin >> q;
  h.init();
  while ( q-- ) {
	fin >> t;
    if ( t == 1 ) {
	  fin >> x;
	  h.insert(x);
	} else if ( t == 2 ) {
	  fin >> x;
	  h.erase(x);
	} else {
	  fout << h.get_min() << "\n";
	}
  }
  fin.close();
  fout.close();
  return 0;
}