crowulll

Untitled

Nov 11th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. struct subArrayHeap;
  4. struct subArrayList;
  5. struct subArrayHeap {
  6. long long l, r;
  7. long long inx;
  8. subArrayList* same_List;
  9. subArrayHeap(long long new_l, long long new_r) {
  10. l = new_l;
  11. r = new_r;
  12. inx = -1;
  13. same_List = nullptr;
  14. }
  15. long long length() {
  16. return this->r - this->l + 1;
  17. }
  18. bool operator > (subArrayHeap *B) {
  19. return (this->length() > B->length() || (this->length() == B->length() && this->l < B->l));
  20. }
  21. };
  22.  
  23. subArrayHeap* Max(subArrayHeap* A, subArrayHeap* B) {
  24. if (*A > B) {
  25. return A;
  26. } else {
  27. return B;
  28. }
  29. }
  30.  
  31. struct subArrayList {
  32. long long l, r;
  33. subArrayHeap* same_Heap;
  34. subArrayList* Left;
  35. subArrayList* Right;
  36. bool empty;
  37. subArrayList(long long new_l, long long new_r) {
  38. l = new_l;
  39. r = new_r;
  40. Left = nullptr;
  41. Right = nullptr;
  42. same_Heap = nullptr;
  43. empty = true;
  44. }
  45. void kill() {
  46. if (this->Left) {
  47. this->Left->Right = this->Right;
  48. }
  49. if (this->Right) {
  50. this->Right->Left = this->Left;
  51. }
  52. delete this;
  53. }
  54. };
  55.  
  56. void killHeap(subArrayHeap* A) {
  57. A->same_List->same_Heap = nullptr;
  58. delete A;
  59. }
  60.  
  61. struct Heap {
  62. std::vector<subArrayHeap*> heap;
  63. Heap() {
  64. this->heap.resize(0);
  65. }
  66. long long sizeHeap() {
  67. return this->heap.size();
  68. }
  69. bool notEmpty() {
  70. if (this->sizeHeap() == 0) {
  71. return false;
  72. } else {
  73. return true;
  74. }
  75. }
  76. subArrayHeap* top() {
  77. return this->heap[0];
  78. }
  79. subArrayHeap* Parent(subArrayHeap* A) {
  80. if (A->inx == 0) {
  81. return nullptr;
  82. } else {
  83. subArrayHeap* returning = heap[(A->inx - 1)/2];
  84. return returning;
  85. }
  86. }
  87. subArrayHeap* LeftChild(subArrayHeap* A) {
  88. if (2*(A->inx) + 1 >= this->sizeHeap()) {
  89. return nullptr;
  90. } else {
  91. return this->heap[2*(A->inx) + 1];
  92. }
  93. }
  94. subArrayHeap* RightChild(subArrayHeap* A) {
  95. if (2*(A->inx) + 2 >= this->sizeHeap()) {
  96. return nullptr;
  97. } else {
  98. return this->heap[2*(A->inx) + 2];
  99. }
  100. }
  101. void SwapHeap(subArrayHeap* A, subArrayHeap* B) {
  102. subArrayHeap* x = A;
  103. long long inx_A = A->inx;
  104. this->heap[A->inx] = B;
  105. this->heap[B->inx] = x;
  106. A->inx = B->inx;
  107. B->inx = inx_A;
  108. }
  109. void siftUp(subArrayHeap* A) {
  110. if (A->inx == 0) {
  111. return;
  112. }
  113. if (*Parent(A) > A) {
  114. return;
  115. } else {
  116. this->SwapHeap(A, this->Parent(A));
  117. this->siftUp(A);
  118. }
  119. }
  120. void siftDown(subArrayHeap* A) {
  121. if (this->RightChild(A)) {
  122. if (*Max(this->LeftChild(A), this->RightChild(A)) > A) {
  123. SwapHeap(Max(this->LeftChild(A), this->RightChild(A)), A);
  124. siftDown(A);
  125. } else {
  126. return;
  127. }
  128. } else if (this->LeftChild(A)) {
  129. if (*this->LeftChild(A) > A) {
  130. SwapHeap(A, this->LeftChild(A));
  131. siftDown(A);
  132. } else {
  133. return;
  134. }
  135. }
  136. }
  137. void killTop() {
  138. SwapHeap(this->heap[0], this->heap[this->sizeHeap() - 1]);
  139. killHeap(this->heap[this->sizeHeap() - 1]);
  140. this->heap.pop_back();
  141. this->siftDown(this->heap[0]);
  142. }
  143. void killAny(subArrayHeap* A) {
  144. A->l = -1;
  145. A->r = 2147483650;
  146. this->siftUp(A);
  147. this->killTop();
  148. }
  149. void addHeap(subArrayList* B) {
  150. auto* A = new subArrayHeap(B->l, B->r);
  151. B->same_Heap = A;
  152. A->same_List = B;
  153. B->empty = true;
  154. this->heap.push_back(A);
  155. A->inx = this->sizeHeap() - 1;
  156. siftUp(A);
  157. }
  158. };
  159.  
  160. struct request {
  161. bool occupy;
  162. bool achieved;
  163. subArrayList* place;
  164. request() {
  165. this->occupy = false;
  166. this->achieved = false;
  167. this->place = nullptr;
  168. }
  169. };
  170.  
  171. struct Manager {
  172. Heap H;
  173. long long N, M;
  174. std::vector<request> req;
  175. Manager(long long new_N, long long new_M) {
  176. this->N = new_N;
  177. this->M = new_M;
  178. this->req.resize(M + 1);
  179. auto* BIG = new subArrayList(1, N);
  180. this->H.addHeap(BIG);
  181. }
  182. long long occupy(long long K, long long r) {
  183. this->req[r].occupy = true;
  184. if (this->H.notEmpty() && this->H.top()->length() >= K) {
  185. this->req[r].achieved = true;
  186. if (this->H.top()->length() == K) {
  187. subArrayList* place = this->H.top()->same_List;
  188. this->H.top()->same_List->empty = false;
  189. this->H.top()->same_List->same_Heap = nullptr;
  190. H.killTop();
  191. this->req[r].place = place;
  192. return place->l;
  193. } else {
  194. auto* new_free = new
  195. subArrayList(this->H.top()->l + K, this->H.top()->r);
  196. this->H.top()->same_List->r = this->H.top()->same_List->l + K - 1;
  197. this->H.top()->same_List->empty = false;
  198. if (this->H.top()->same_List->Right) {
  199. this->H.top()->same_List->Right->Left = new_free;
  200. new_free->Right = this->H.top()->same_List->Right;
  201. }
  202. this->H.top()->same_List->Right = new_free;
  203. new_free->Left = this->H.top()->same_List;
  204. this->H.top()->same_List->same_Heap = nullptr;
  205. new_free->same_Heap = H.top();
  206. this->H.top()->same_List = new_free;
  207. this->H.top()->l = new_free->l;
  208. this->H.top()->r = new_free->r;
  209. H.siftDown(this->H.top());
  210. this->req[r].place = new_free->Left;
  211. // this->H.printHeap();
  212. return new_free->Left->l;
  213. }
  214. } else {
  215. // this->H.printHeap();
  216. return -1;
  217. }
  218. }
  219. void release(long long r) {
  220. if (this->req[r].achieved) {
  221. subArrayList* place = this->req[r].place;
  222. // 1.
  223. if (place->Left && place->Right &&
  224. place->Left->empty && place->Right->empty) {
  225. subArrayList* placeLeft = place->Left;
  226. place->Left->r = place->Right->r;
  227. this->H.killAny(place->Right->same_Heap);
  228. place->Right->kill();
  229. place->kill();
  230. placeLeft->same_Heap->r = placeLeft->r;
  231. this->H.siftUp(placeLeft->same_Heap);
  232. // 2.
  233. } else if (place->Right && place->Right->empty) {
  234. place->Right->l = place->l;
  235. place->Right->same_Heap->l = place->Right->l;
  236. this->H.siftUp(place->Right->same_Heap);
  237. place->kill();
  238. // 3.
  239. } else if (place->Left && place->Left->empty) {
  240. place->Left->r = place->r;
  241. place->Left->same_Heap->r = place->Left->r;
  242. this->H.siftUp(place->Left->same_Heap);
  243. place->kill();
  244. // 4.
  245. } else {
  246. place->empty = true;
  247. H.addHeap(place);
  248. }
  249. }
  250. }
  251. void Process_One_Request(long long T, long long r) {
  252. if (T > 0) {
  253. std::cout << this->occupy(T, r) << std::endl;
  254. } else {
  255. this->release(-T);
  256. }
  257. }
  258. void Process_All_Requests() {
  259. for (int i = 1; i < M + 1; ++i) {
  260. long long T;
  261. std::cin >> T;
  262. Process_One_Request(T, i);
  263. }
  264. }
  265. };
  266.  
  267. int main() {
  268. long long N, M;
  269. std::cin >> N >> M;
  270. Manager Man(N, M);
  271. Man.Process_All_Requests();
  272. }
  273.  
Advertisement
Add Comment
Please, Sign In to add comment