Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define fr first
- #define sc second
- #define m_p make_pair
- using namespace std;
- typedef long long ll;
- const int INF = 2e9 + 100;
- const int maxn = 1e3 + 10;
- struct heap {
- pair<int, int> v;
- int deg, id;
- heap *ch, *sib, *p;
- heap(int v_, int id_, int rid_) :
- v(m_p(v_, id_)), deg(0), id(rid_), ch(nullptr), sib(nullptr), p(nullptr) {}
- };
- typedef heap * ptr;
- vector<ptr> q;
- int n;
- class b_heap {
- public:
- int ind;
- vector<ptr> root;
- b_heap() {
- root.resize(22, nullptr);
- }
- void merge(b_heap &a) {
- ptr v = nullptr;
- for (int i = 0; i < 22; i++) {
- if (root[i] == nullptr || (a.root[i] != nullptr && a.root[i]->v < root[i]->v))
- swap(root[i], a.root[i]);
- if (root[i] == nullptr || (v != nullptr && v->v < root[i]->v))
- swap(root[i], v);
- if (a.root[i] == nullptr || (v != nullptr && v->v < a.root[i]->v))
- swap(a.root[i], v);
- if (a.root[i] != nullptr) {
- a.root[i]->sib = root[i]->ch;
- a.root[i]->p = root[i];
- root[i]->ch = a.root[i];
- root[i]->deg++;
- swap(root[i], v);
- a.root[i] = nullptr;
- }
- if (root[i] != nullptr)
- root[i]->id = ind;
- }
- }
- int get_min() {
- pair<int, int> ans;
- ans.sc = -1;
- for (auto i : root)
- if (i != nullptr)
- if (ans.sc == -1 || ans > i->v)
- ans = i->v;
- return ans.fr;
- }
- void extract_min(int id, int val);
- };
- b_heap arr[maxn];
- void b_heap::extract_min(int id, int val) {
- if (id == -1) {
- pair<int, int> ans;
- ans.sc = -1;
- for (int i = 0; i < (int)root.size(); i++)
- if (root[i] != nullptr)
- if (ans.sc == -1 || ans > root[i]->v)
- ans = root[i]->v, id = i;
- }
- if (id == -2) {
- for (int i = 0; i < (int)root.size(); i++)
- if (root[i] != nullptr && root[i]->v.sc == val) {
- id = i;
- break;
- }
- }
- int deg = root[id]->deg;
- ptr t = root[id]->ch;
- for (int i = 0; i < deg; i++) {
- t->p = nullptr;
- arr[n].root[deg - 1 - i] = t;
- t = t->sib;
- }
- root[id] = nullptr;
- merge(arr[n]);
- }
- int decrease_key(int id, int w) {
- ptr t = q[id];
- if (t == nullptr)
- return -1;
- t->v.fr = w;
- while (t->p != nullptr && t->p->v >= t->v)
- swap(t->p->v, t->v), q[t->v.sc] = t, q[t->p->v.sc] = t->p, t = t->p;
- while (t->p != nullptr)
- t = t->p;
- return t->id;
- }
- mt19937 rnd(23);
- int main() {
- #ifdef ONPC
- freopen("a.in", "r", stdin);
- freopen("a.out", "w", stdout);
- #else
- // freopen("priorityqueue2.in", "r", stdin);
- // freopen("priorityqueue2.out", "w", stdout);
- #endif // ONPC
- ios::sync_with_stdio(0);
- cin.tie(0);
- int m;
- cin >> n >> m;
- for (int i = 0; i <= n; i++)
- arr[i].ind = i;
- while (m--) {
- int ts;
- cin >> ts;
- if (ts == 0) {
- int v, id;
- cin >> id >> v;
- id--;
- arr[n].root[0] = new heap(v, (int)q.size(), n);
- q.push_back(arr[n].root[0]);
- arr[id].merge(arr[n]);
- }
- if (ts == 1) {
- int a, b;
- cin >> a >> b;
- a--;
- b--;
- arr[b].merge(arr[a]);
- }
- if (ts == 2) {
- int id;
- cin >> id;
- id--;
- if (q[id] != nullptr) {
- int ind = decrease_key(id, INT_MIN);
- arr[ind].extract_min(-2, id);
- }
- }
- if (ts == 3) {
- int id, v;
- cin >> id >> v;
- id--;
- if (q[id] != nullptr) {
- int ind = decrease_key(id, INT_MIN);
- arr[ind].extract_min(-2, id);
- arr[n].root[0] = new heap(v, id, n);
- q[id] = arr[n].root[0];
- arr[ind].merge(arr[n]);
- }
- }
- if (ts == 4) {
- int id;
- cin >> id;
- cout << arr[id - 1].get_min() << '\n';
- }
- if (ts == 5) {
- int id;
- cin >> id;
- arr[id - 1].extract_min(-1, 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement