Cod sursa(job #2743691)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 23 aprilie 2021 14:13:17
Problema Heapuri cu reuniune Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.31 kb
#include <cstdio>
#include <iostream>
#include <cassert>

using namespace std;

struct node {
  int x;
  node* kid;
  node* nxt;
  node() {
    x = 0;
    kid = 0;
    nxt = 0;
  }
};

node* root[123];
node* nothing;

node* join(node* a, node* b) {
  if (!a) {
    return b;
  }
  if (!b) {
    return a;
  }
  if (a->x > b->x) {
    swap(a, b);
  }
  a->kid = b;
  b->nxt = a->nxt;
  return a;
}


node* tupac(node* a) {
  if (!a) {
    return a;
  }
  if (!a->nxt) {
    return a;
  }
  return join(join(a, a->nxt), tupac(a->nxt->nxt));
}

node* del(node* a) {
  return tupac(a->kid);
}

node* ins(node* a, int x) {
  node* b = new node;
  b->x = x;
  return join(a, b);
}


int main() {
  freopen ("mergeheap.in", "r", stdin);
  freopen ("mergeheap.out", "w", stdout);

  int n, q;
  scanf("%d %d", &n, &q);
  while (q--) {
    int tp;
    scanf("%d", &tp);
    if (tp == 1) {
      int i, x;
      scanf("%d %d", &i, &x);
      root[i] = ins(root[i], -x);
    }
    if (tp == 2) {
      int i;
      scanf("%d", &i);
      assert(root[i]);
      printf("%d\n", -root[i]->x);
      root[i] = del(root[i]);
    }
    if (tp == 3) {
      int i, j;
      scanf("%d %d", &i, &j);
      root[i] = join(root[i], root[j]);
      root[j] = nothing;
    }
  }
}