vadimk772336

Untitled

Oct 30th, 2021
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct heap_elements;
  7.  
  8. struct list_segments
  9. {
  10.     int l;
  11.     int r;
  12.     int status;
  13.     struct list_segments* next;
  14.     struct list_segments* prev;
  15.     int to_heap;
  16. };
  17.  
  18. struct heap_elements
  19. {
  20.     int l;
  21.     int r;
  22.     int pos;
  23.     list_segments* to_list;
  24. };
  25.  
  26. bool operator<(const heap_elements& a, const heap_elements& b)
  27. {
  28.  
  29.     if ((a.r - a.l < b.r - b.l))
  30.         return true;
  31.     if ((a.r - a.l == b.r - b.l) & (a.l > b.l))
  32.         return true;
  33.  
  34.     return false;
  35. }
  36.  
  37. class Heap
  38. {
  39.     std::vector<struct heap_elements> h;
  40.     std::vector<int> ID_toHeapIdx;
  41.     int heap_size;
  42.     int curr_ID;
  43.  
  44. public:
  45.     Heap();
  46.     void siftup();
  47.     void siftdown(int pos);
  48.     void add(int, int, list_segments*);
  49.     void delete_vertex(int pos);
  50.     void out();
  51.     bool isempty();
  52.     heap_elements* get_head();
  53. };
  54.  
  55. Heap::Heap()
  56. {
  57.     std::vector<struct heap_elements> h;
  58.     heap_size = 0;
  59.     curr_ID = 0;
  60.     std::vector<int> ID_toHeapIdx;
  61. }
  62.  
  63. heap_elements* Heap::get_head()
  64. {
  65.     return &h[0];
  66. }
  67.  
  68. bool Heap::isempty()
  69. {
  70.     if (heap_size == 0)
  71.         return true;
  72.     return false;
  73. }
  74.  
  75. void Heap::siftup(std::vector<int>& heapToIdx)
  76. {
  77.     int curr, parent;
  78.     curr = heap_size - 1;
  79.     parent = (curr - 1) / 2;
  80.     while (parent >= 0 && curr > 0)
  81.     {
  82.         if (h[parent] < h[curr])
  83.         {
  84.             heap_elements buff = h[curr];
  85.             h[curr] = h[parent];
  86.             h[parent] = buff;
  87.  
  88.  
  89.             int tmp;
  90.             heapToIdx[parent] = tmp;
  91.             heapToIdx[parent] = heapToIdx[curr];
  92.             heapToIdx[curr] = tmp;
  93.  
  94.             ID_toHeapIdx[heapToIdx[parent]] = tmp;
  95.             ID_toHeapIdx[heapToIdx[parent]] = ID_toHeapIdx[heapToIdx[curr]];
  96.             ID_toHeapIdx[heapToIdx[curr]] = tmp;
  97.  
  98.             h[curr].pos = curr;
  99.             h[parent].pos = parent;
  100.  
  101.             h[curr].to_list->to_heap = curr;
  102.             h[parent].to_list->to_heap = parent;
  103.         }
  104.         curr = parent;
  105.         parent = (curr - 1) / 2;
  106.     }
  107. }
  108.  
  109. void Heap::siftdown(int pos,std::vector<int>& heapToIdx)
  110. {
  111.     int parent, max_child;
  112.  
  113.     int curr = pos;
  114.     int child_l = 2 * curr + 1;
  115.     int child_r = 2 * curr + 2;
  116.  
  117.     if (h[child_r] < h[child_l])
  118.         max_child = child_l;
  119.     else
  120.         max_child = child_r;
  121.  
  122.     while (child_l < heap_size)
  123.     {
  124.         if (child_l == heap_size - 1)
  125.             max_child = child_l;
  126.         else if (h[child_r] < h[child_l])
  127.             max_child = child_l;
  128.         else
  129.             max_child = child_r;
  130.  
  131.         if (h[curr] < h[max_child])
  132.         {
  133.             heap_elements buff = h[curr];
  134.             h[curr] = h[max_child];
  135.             h[max_child] = buff;
  136.  
  137.  
  138.             int tmp;
  139.             heapToIdx[max_child] = tmp;
  140.             heapToIdx[max_child] = heapToIdx[curr];
  141.             heapToIdx[curr] = tmp;
  142.  
  143.  
  144.             ID_toHeapIdx[heapToIdx[max_child]] = tmp;
  145.             ID_toHeapIdx[heapToIdx[max_child]] = ID_toHeapIdx[heapToIdx[curr]];
  146.             ID_toHeapIdx[heapToIdx[curr]] = tmp;
  147.  
  148.             h[curr].pos = curr;
  149.             h[max_child].pos = max_child;
  150.  
  151.             h[curr].to_list->to_heap = curr;
  152.             h[max_child].to_list->to_heap = max_child;
  153.         }
  154.  
  155.         curr = max_child;
  156.         child_l = 2 * curr + 1;
  157.         child_r = 2 * curr + 2;
  158.     }
  159. }
  160.  
  161. void Heap::add(int l, int r, list_segments* to_list, ID, std::vector<int>& heapToIdx)
  162. {
  163.  
  164.     ID_toHeapIdx.push_back(ID);
  165.     //curr_ID++;
  166.  
  167.     heap_elements vertex;
  168.     vertex.l = l;
  169.     vertex.r = r;
  170.     vertex.to_list = to_list;
  171.  
  172.     h.push_back(vertex);
  173.     h[heap_size].pos = heap_size;
  174.  
  175.     if (h[heap_size].to_list != NULL)
  176.         h[heap_size].to_list->to_heap = heap_size;
  177.  
  178.     heap_size++;
  179.     siftup(heapToIdx);
  180. }
  181.  
  182. void Heap::delete_vertex(int ID)
  183. {
  184.     int pos = ID_toHeapIdx[ID]; //По ID понял где лежит этот элемент в куче
  185.     h[pos] = h[heap_size - 1];
  186.     h.pop_back();
  187.     heap_size--;
  188.     siftdown(pos);
  189. }
  190.  
  191. void Heap::out(void)
  192. {
  193.  
  194.     for (int i = 0; i < heap_size; i++)
  195.     {
  196.         cout << "(" << h[i].l << "," << h[i].r << ") ";
  197.     }
  198.     cout << endl;
  199. }
  200.  
  201. class LIST_SEGMENTS
  202. {
  203.     struct list_segments* Head;
  204.     int count;
  205.  
  206. public:
  207.     LIST_SEGMENTS();
  208.     list_segments* get_head();
  209.     void add(int, int, int, int, list_segments*);
  210.     void delete_segment(list_segments* segment);
  211.     void add_head(int, int, int, int);
  212.     void Print();
  213. };
  214.  
  215. LIST_SEGMENTS::LIST_SEGMENTS()
  216. {
  217.     Head = NULL;
  218.     count = 0;
  219. }
  220.  
  221. void LIST_SEGMENTS::add_head(int l, int r, int status, int to_heap)
  222. {
  223.     list_segments* buff = new list_segments;
  224.     buff->prev = 0;
  225.     buff->l = l;
  226.     buff->r = r;
  227.     buff->status = status;
  228.     buff->next = Head;
  229.  
  230.     if (Head != NULL)
  231.         Head->prev = buff;
  232.  
  233.     Head = buff;
  234.     count++;
  235. }
  236.  
  237. void LIST_SEGMENTS::add(int l, int r, int status, int to_heap, list_segments* segment)
  238. {
  239.     list_segments* after_segment = new list_segments;
  240.     after_segment->l = l;
  241.     after_segment->r = r;
  242.     after_segment->status = status;
  243.     after_segment->next = segment->next;
  244.     after_segment->prev = segment;
  245.     after_segment->to_heap = to_heap;
  246.  
  247.     if (segment->next != NULL)
  248.         segment->next->prev = after_segment;
  249.  
  250.     segment->next = after_segment;
  251.     count++;
  252. }
  253.  
  254. void LIST_SEGMENTS::delete_segment(list_segments* segment)
  255. {
  256.     if (segment->next != NULL)
  257.         segment->next->prev = segment->prev;
  258.     if (segment->prev != NULL)
  259.         segment->prev->next = segment->next;
  260.     else
  261.         Head = segment->next;
  262.     delete segment;
  263. }
  264.  
  265. list_segments* LIST_SEGMENTS::get_head()
  266. {
  267.     return Head;
  268. }
  269.  
  270. void LIST_SEGMENTS::Print()
  271. {
  272.    
  273.    
  274.     list_segments* buff = Head;
  275.     while (buff->next != NULL)
  276.     {
  277.         cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";";
  278.         buff = buff->next;
  279.     }
  280.  
  281.     cout << "(" << buff->l << ";" << buff->status << ";"  << buff->r << ")"<< ";\n";
  282. }
  283.  
  284. void find_memory(Heap* heap, LIST_SEGMENTS* List, int K, int query_number,
  285.     std::vector<int>& answers, list_segments** requests)
  286. {
  287.     int old_l;
  288.     heap_elements* heap_head;
  289.  
  290.     heap_head = heap->get_head();
  291.     if (!heap->isempty() & (heap_head->r - heap_head->l + 1) >= K)
  292.     {
  293.         if ((heap_head->r - heap_head->l + 1) == K)
  294.         {
  295.             heap_head->to_list->status = 0;
  296.             requests[query_number] = heap_head->to_list;
  297.             answers.push_back(heap_head->l);
  298.             heap->delete_vertex(0);
  299.         }
  300.         else
  301.         {
  302.             old_l = heap_head->l;
  303.             heap_head->l = old_l + K;
  304.             heap_head->to_list->l = old_l + K;
  305.  
  306.             if (heap_head->to_list->prev != NULL)
  307.             {
  308.                 List->add(old_l, old_l + K - 1, 0, -1, heap_head->to_list->prev);
  309.                 requests[query_number] = heap_head->to_list->prev;
  310.             }
  311.             else
  312.             {
  313.                 List->add_head(old_l, old_l + K - 1, 0, -1);
  314.                 requests[query_number] = List->get_head();
  315.             }
  316.  
  317.             heap->siftdown(0);
  318.             answers.push_back(old_l);
  319.         }
  320.     }
  321.     else
  322.     {
  323.         requests[query_number] = NULL;
  324.         answers.push_back(-1);
  325.     }
  326. }
  327.  
  328. void free_up_memory(Heap* heap, LIST_SEGMENTS* List, int q, int query_number,  //Сделать обновление кар АЙДИ при добавлении
  329.     std::vector<int>& answers, list_segments** requests)
  330. {
  331.     if (q == 246 | query_number > 242 & query_number < 250) cout << "Зашли в функцию освбождения памяти" << endl;
  332.     if (requests[q] != NULL)
  333.     {
  334.         if (q == 246 | query_number > 242 & query_number < 250) cout << "Освобождаем отрезок" << endl;
  335.         requests[q]->status = 1;
  336.         if (q == 246 | query_number > 242 & query_number < 250) {
  337.             cout << "Выведем л р отерзка, на который ссылается req[q], q=" << q << endl;
  338.             cout << requests[q]->l << requests[q]->r << endl;
  339.            
  340.         }
  341.         if (requests[q]->prev != NULL)
  342.         {
  343.             if (q == 246 | query_number > 242 & query_number < 250) cout << "Prev у этого отерзка не пуст" << endl;
  344.             if (requests[q]->prev->status == 1 & requests[q]->prev->r == requests[q]->l - 1)
  345.             {
  346.                
  347.                 requests[q]->l = requests[q]->prev->l;
  348.                 heap->delete_vertex(requests[q]->prev->to_heap);
  349.                 List->delete_segment(requests[q]->prev);
  350.                 if (q == 246 | query_number > 242 & query_number < 250) cout << "слили с левым краем т.к. можно" << endl;
  351.             }
  352.         }
  353.         if (requests[q]->next != NULL)
  354.         {
  355.             if (q == 246 | query_number > 242 & query_number < 250) cout << "next у этого отерзка не пуст" << endl;
  356.             if (requests[q]->next->status == 1 & requests[q]->next->l - 1 == requests[q]->r)
  357.             {
  358.                 requests[q]->r = requests[q]->next->r;
  359.                 heap->delete_vertex(requests[q]->next->to_heap);
  360.                 List->delete_segment(requests[q]->next);
  361.                 if (q == 246 | query_number > 242 & query_number < 250) cout << "cлили с правым краем т.к. можно" << endl;
  362.             }
  363.         }
  364.         if (q == 246 | query_number > 242 & query_number < 250) {
  365.             cout << "Сейчас будем добавлять отредактированный (если сливали) отрезок в кучу пока что она выглядит так:" << endl;
  366.             heap->out();
  367.         }
  368.         heap->add(requests[q]->l, requests[q]->r, requests[q]);
  369.         if (q == 246 | query_number > 242 & query_number < 250) {
  370.             cout << "Толко что добвили в кучу, теперь она такая:" << endl;
  371.             heap->out();
  372.         }
  373.         requests[query_number] = requests[q];
  374.         requests[q] = NULL;
  375.         if (q == 246 | query_number > 242 & query_number < 250) cout << "Реквест requests[q] = NULL сделали" << endl;
  376.     }
  377.     else
  378.     {  
  379.         if (q == 246 | query_number > 242 & query_number < 250) cout << "Нечего освобождать поэтому вышли" << endl;
  380.         requests[query_number] = NULL;
  381.     }
  382. }
  383.  
  384.  
  385.  
  386.  
  387. int main()
  388. {
  389.    
  390.     /*
  391.     -9
  392.     6 8 2 3 -1 3 3 -5 2 2 //1 3 -1 -1 1 -1
  393.     1 7 2 2 1 1 -4 -1 -3 //-1 -1 1 -1
  394.     6 9 2 2 2 -1 -3 -2  2 2 2 // 1 3 5 1 3 5
  395.     6 2 3 -1 //1,-1,1,3
  396.     6 7 6 6-2-1 2 4 -5
  397.     10 14 8 2 -2 5 -4 -4 -1 4 4 2 -8 -10 -9 10 //1 9 -1 1 5 9 1
  398.     1 1 1  // 1
  399.     1 2 1 2   //1 -1
  400.     1 9 2 -1 1 1 -1 2 1 -3 1 //-1 1 -1 -1 -1 1
  401.     8 10 2 2 -1 1 3 -2 3 -4 3 -9
  402.  
  403.     */
  404.     int N, M, K, q, old_l;
  405.  
  406.     int count_tests;
  407.     ifstream ifs("tests.txt");
  408.     ifs >> count_tests;
  409.     ofstream out("results.txt");
  410.  
  411.  
  412.     for (int i = 0; i < count_tests; ++i)
  413.     {
  414.         ifs >> N;
  415.         ifs >> M;
  416.         Heap heap;
  417.         LIST_SEGMENTS List;
  418.         heap_elements* heap_head;
  419.         std::vector<int> answers;
  420.         std::vector<int> heapToIdx;
  421.         int curr_ID=0;
  422.         List.add_head(1, N, 1, 0);
  423.         heap.add(1, N, List.get_head());
  424.         heapToIdx.push_back(curr_ID);
  425.         curr_ID++;
  426.  
  427.         list_segments* requests[M];
  428.         for (int query_number = 0; query_number < M; ++query_number)
  429.         {
  430.             ifs >> K;
  431.             cout << "\n ЗАПРОС НОМЕР " << query_number+1 <<  " Enter K: " << K << endl;
  432.             if (K > 0)
  433.             {
  434.                 find_memory(&heap, &List, K, query_number, answers, requests);
  435.             }
  436.             else
  437.             {
  438.                 q = -K - 1;
  439.                 free_up_memory(&heap, &List, q, query_number, answers, requests);
  440.             }
  441.            
  442.             if (query_number > 240 & query_number < 250) {
  443.                 cout << "Куча после запроса: ";
  444.                 heap.out();
  445.                 cout << "List после запроса: ";
  446.                 List.Print();
  447.                 cout << "requests после запроса " << query_number << ": ";
  448.    
  449.                 for (int f=240; f < query_number+1; ++f) {
  450.                     if (requests[f] == NULL)
  451.                         cout << " null; ";
  452.                     else
  453.                         cout << "(" << requests[f]->l << ";" << requests[f]->status << ";"  << requests[f]->r << ")"<< "; ";
  454.                 }
  455.                 cout << "\n---------------";
  456.             }
  457.            
  458.            
  459.         }
  460.     for (int i = 0; i < answers.size(); ++i){
  461.         out << answers[i] << ' ';
  462.         cout << answers[i] << " ";
  463.     }
  464.     cout << endl;
  465.     out << '\n';
  466.     }
  467.  
  468.     return 0;
  469. }
  470.  
Advertisement
Add Comment
Please, Sign In to add comment