- #include <iostream>
- using namespace std;
- struct node
- {
- int value;
- char content [34];
- };
- node HEAP [32] [2500];
- int last [32];
- // funkcja pomocnicza, zwracajaca numer wybranego kopca
- int HeapNumber (char sign)
- {
- int numb = (int) sign - '0' - 17;
- return numb;
- }
- void ClearTab (char tab [])
- {
- for (int i = 0; i < 34; i++)
- {
- tab [i] = 0;
- }
- }
- // funkcja zwracajaca numer przodka elementu
- int ParentNode (int numb)
- {
- if (numb == 1)
- {
- return 1;
- }
- else if (numb % 5 == 0)
- {
- return numb / 5;
- }
- else if (numb % 5 == 1)
- {
- return (numb - 1) / 5;
- }
- else
- {
- return (numb + 5 - (numb % 5)) / 5;
- }
- }
- int SmallestChild (int heap_numb, int current_node)
- {
- int tmp = current_node * 5 - 3;
- int smallest = HEAP [heap_numb] [tmp].value;
- int smallest_node_numb = tmp;
- for (int i = 1; i < 5; i++)
- {
- if (HEAP [heap_numb] [tmp + i].value < smallest && HEAP [heap_numb] [tmp + i].value > 0)
- {
- smallest = HEAP [heap_numb] [tmp + i].value;
- smallest_node_numb = tmp + i;
- }
- }
- return smallest_node_numb;
- }
- void AddToHeap (int value, char content [], int heap_numb)
- {
- int current = ++last [heap_numb];
- HEAP [heap_numb] [current].value = value;
- for (int i = 0; i < 34; i++)
- {
- HEAP [heap_numb] [current].content [i] = content [i];
- }
- while (HEAP [heap_numb] [current].value < HEAP [heap_numb] [ParentNode (current)].value)
- {
- swap (HEAP [heap_numb] [current], HEAP [heap_numb] [ParentNode (current)]);
- current = ParentNode (current);
- }
- }
- void RemoveFromHeap (int heap_numb)
- {
- if (HEAP [heap_numb] [1].value)
- {
- char tmp [34];
- for (int i = 0; i < 34; i++)
- {
- tmp [i] = HEAP [heap_numb] [1].content [i];
- }
- int current = 1, temp;
- HEAP [heap_numb] [1] = HEAP [heap_numb] [last [heap_numb]];
- HEAP [heap_numb] [last [heap_numb]].value = 0;
- ClearTab (HEAP [heap_numb] [last [heap_numb]].content);
- --last [heap_numb];
- while (HEAP [heap_numb] [SmallestChild (heap_numb, current)].value < HEAP [heap_numb] [current].value && HEAP [heap_numb] [SmallestChild (heap_numb, current)].value > 0)
- {
- temp = SmallestChild (heap_numb, current);
- swap (HEAP [heap_numb] [current], HEAP [heap_numb] [SmallestChild (heap_numb, current)]);
- current = temp;
- }
- cout << tmp << endl;
- }
- }
- void JoinHeaps (int heap_numb, int heap_numb2)
- {
- for (int i = last [heap_numb2]; i >= 1; i--)
- {
- AddToHeap (HEAP [heap_numb2] [last [heap_numb2]].value, HEAP [heap_numb2] [last [heap_numb2]].content, heap_numb);
- HEAP [heap_numb2] [last [heap_numb2]].value = 0;
- ClearTab (HEAP [heap_numb2] [last [heap_numb2]].content);
- last [heap_numb2]--;
- }
- }
- void ChangePriority (int heap_numb, int old, int fresh)
- {
- int i = 1, j = 0, temp;
- while (i <= last [heap_numb])
- {
- if (HEAP [heap_numb] [i].value == old)
- {
- HEAP [heap_numb] [i].value = fresh;
- while (HEAP [heap_numb] [i].value < HEAP [heap_numb] [ParentNode (i)].value)
- {
- swap (HEAP [heap_numb] [i], HEAP [heap_numb] [ParentNode (i)]);
- i = ParentNode (i);
- j++;
- }
- if (j = 0)
- {
- while (HEAP [heap_numb] [SmallestChild (heap_numb, i)].value < HEAP [heap_numb] [i].value && HEAP [heap_numb] [SmallestChild (heap_numb, i)].value > 0)
- {
- temp = SmallestChild (heap_numb, i);
- swap (HEAP [heap_numb] [i], HEAP [heap_numb] [SmallestChild (heap_numb, i)]);
- i = temp;
- }
- }
- break;
- }
- i++;
- }
- }
- int main ()
- {
- char polecenie, heap_sign, heap_sign2, content [34];
- int numer_kopca, numer_kopca2, value, value2;
- while (cin >> polecenie)
- {
- // dodaj element, np. I M 10 ZAWARTOSC
- if (polecenie == 'I')
- {
- cin >> heap_sign;
- numer_kopca = HeapNumber (heap_sign);
- cin >> value;
- cin >> content;
- AddToHeap (value, content, numer_kopca);
- }
- // usun maksymalny element, np. D S
- else if (polecenie == 'D')
- {
- cin >> heap_sign;
- numer_kopca = HeapNumber (heap_sign);
- RemoveFromHeap (numer_kopca);
- }
- // join heaps, ex. J A B
- else if (polecenie == 'J')
- {
- cin >> heap_sign;
- numer_kopca = HeapNumber (heap_sign);
- cin >> heap_sign2;
- numer_kopca2 = HeapNumber (heap_sign2);
- if (numer_kopca != numer_kopca2)
- {
- JoinHeaps (numer_kopca, numer_kopca2);
- }
- }
- else if (polecenie == 'M')
- {
- cin >> heap_sign;
- numer_kopca = HeapNumber (heap_sign);
- cin >> value;
- cin >> value2;
- if (value != value2)
- {
- ChangePriority (numer_kopca, value, value2);
- }
- }
- }
- return 0;
- }