Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- struct subArrayHeap;
- struct subArrayList;
- struct subArrayHeap {
- long long l, r;
- long long inx;
- subArrayList* same_List;
- subArrayHeap(long long new_l, long long new_r) {
- l = new_l;
- r = new_r;
- inx = -1;
- same_List = nullptr;
- }
- long long length() {
- return this->r - this->l + 1;
- }
- bool operator > (subArrayHeap *B) {
- return (this->length() > B->length() || (this->length() == B->length() && this->l < B->l));
- }
- };
- subArrayHeap* Max(subArrayHeap* A, subArrayHeap* B) {
- if (*A > B) {
- return A;
- } else {
- return B;
- }
- }
- struct subArrayList {
- long long l, r;
- subArrayHeap* same_Heap;
- subArrayList* Left;
- subArrayList* Right;
- bool empty;
- subArrayList(long long new_l, long long new_r) {
- l = new_l;
- r = new_r;
- Left = nullptr;
- Right = nullptr;
- same_Heap = nullptr;
- empty = true;
- }
- void kill() {
- if (this->Left) {
- this->Left->Right = this->Right;
- }
- if (this->Right) {
- this->Right->Left = this->Left;
- }
- delete this;
- }
- };
- void killHeap(subArrayHeap* A) {
- A->same_List->same_Heap = nullptr;
- delete A;
- }
- struct Heap {
- std::vector<subArrayHeap*> heap;
- Heap() {
- this->heap.resize(0);
- }
- long long sizeHeap() {
- return this->heap.size();
- }
- bool notEmpty() {
- if (this->sizeHeap() == 0) {
- return false;
- } else {
- return true;
- }
- }
- subArrayHeap* top() {
- return this->heap[0];
- }
- subArrayHeap* Parent(subArrayHeap* A) {
- if (A->inx == 0) {
- return nullptr;
- } else {
- subArrayHeap* returning = heap[(A->inx - 1)/2];
- return returning;
- }
- }
- subArrayHeap* LeftChild(subArrayHeap* A) {
- if (2*(A->inx) + 1 >= this->sizeHeap()) {
- return nullptr;
- } else {
- return this->heap[2*(A->inx) + 1];
- }
- }
- subArrayHeap* RightChild(subArrayHeap* A) {
- if (2*(A->inx) + 2 >= this->sizeHeap()) {
- return nullptr;
- } else {
- return this->heap[2*(A->inx) + 2];
- }
- }
- void SwapHeap(subArrayHeap* A, subArrayHeap* B) {
- subArrayHeap* x = A;
- long long inx_A = A->inx;
- this->heap[A->inx] = B;
- this->heap[B->inx] = x;
- A->inx = B->inx;
- B->inx = inx_A;
- }
- void siftUp(subArrayHeap* A) {
- if (A->inx == 0) {
- return;
- }
- if (*Parent(A) > A) {
- return;
- } else {
- this->SwapHeap(A, this->Parent(A));
- this->siftUp(A);
- }
- }
- void siftDown(subArrayHeap* A) {
- if (this->RightChild(A)) {
- if (*Max(this->LeftChild(A), this->RightChild(A)) > A) {
- SwapHeap(Max(this->LeftChild(A), this->RightChild(A)), A);
- siftDown(A);
- } else {
- return;
- }
- } else if (this->LeftChild(A)) {
- if (*this->LeftChild(A) > A) {
- SwapHeap(A, this->LeftChild(A));
- siftDown(A);
- } else {
- return;
- }
- }
- }
- void killTop() {
- SwapHeap(this->heap[0], this->heap[this->sizeHeap() - 1]);
- killHeap(this->heap[this->sizeHeap() - 1]);
- this->heap.pop_back();
- this->siftDown(this->heap[0]);
- }
- void killAny(subArrayHeap* A) {
- A->l = -1;
- A->r = 2147483650;
- this->siftUp(A);
- this->killTop();
- }
- void addHeap(subArrayList* B) {
- auto* A = new subArrayHeap(B->l, B->r);
- B->same_Heap = A;
- A->same_List = B;
- B->empty = true;
- this->heap.push_back(A);
- A->inx = this->sizeHeap() - 1;
- siftUp(A);
- }
- };
- struct request {
- bool occupy;
- bool achieved;
- subArrayList* place;
- request() {
- this->occupy = false;
- this->achieved = false;
- this->place = nullptr;
- }
- };
- struct Manager {
- Heap H;
- long long N, M;
- std::vector<request> req;
- Manager(long long new_N, long long new_M) {
- this->N = new_N;
- this->M = new_M;
- this->req.resize(M + 1);
- auto* BIG = new subArrayList(1, N);
- this->H.addHeap(BIG);
- }
- long long occupy(long long K, long long r) {
- this->req[r].occupy = true;
- if (this->H.notEmpty() && this->H.top()->length() >= K) {
- this->req[r].achieved = true;
- if (this->H.top()->length() == K) {
- subArrayList* place = this->H.top()->same_List;
- this->H.top()->same_List->empty = false;
- this->H.top()->same_List->same_Heap = nullptr;
- H.killTop();
- this->req[r].place = place;
- return place->l;
- } else {
- auto* new_free = new
- subArrayList(this->H.top()->l + K, this->H.top()->r);
- this->H.top()->same_List->r = this->H.top()->same_List->l + K - 1;
- this->H.top()->same_List->empty = false;
- if (this->H.top()->same_List->Right) {
- this->H.top()->same_List->Right->Left = new_free;
- new_free->Right = this->H.top()->same_List->Right;
- }
- this->H.top()->same_List->Right = new_free;
- new_free->Left = this->H.top()->same_List;
- this->H.top()->same_List->same_Heap = nullptr;
- new_free->same_Heap = H.top();
- this->H.top()->same_List = new_free;
- this->H.top()->l = new_free->l;
- this->H.top()->r = new_free->r;
- H.siftDown(this->H.top());
- this->req[r].place = new_free->Left;
- // this->H.printHeap();
- return new_free->Left->l;
- }
- } else {
- // this->H.printHeap();
- return -1;
- }
- }
- void release(long long r) {
- if (this->req[r].achieved) {
- subArrayList* place = this->req[r].place;
- // 1.
- if (place->Left && place->Right &&
- place->Left->empty && place->Right->empty) {
- subArrayList* placeLeft = place->Left;
- place->Left->r = place->Right->r;
- this->H.killAny(place->Right->same_Heap);
- place->Right->kill();
- place->kill();
- placeLeft->same_Heap->r = placeLeft->r;
- this->H.siftUp(placeLeft->same_Heap);
- // 2.
- } else if (place->Right && place->Right->empty) {
- place->Right->l = place->l;
- place->Right->same_Heap->l = place->Right->l;
- this->H.siftUp(place->Right->same_Heap);
- place->kill();
- // 3.
- } else if (place->Left && place->Left->empty) {
- place->Left->r = place->r;
- place->Left->same_Heap->r = place->Left->r;
- this->H.siftUp(place->Left->same_Heap);
- place->kill();
- // 4.
- } else {
- place->empty = true;
- H.addHeap(place);
- }
- }
- }
- void Process_One_Request(long long T, long long r) {
- if (T > 0) {
- std::cout << this->occupy(T, r) << std::endl;
- } else {
- this->release(-T);
- }
- }
- void Process_All_Requests() {
- for (int i = 1; i < M + 1; ++i) {
- long long T;
- std::cin >> T;
- Process_One_Request(T, i);
- }
- }
- };
- int main() {
- long long N, M;
- std::cin >> N >> M;
- Manager Man(N, M);
- Man.Process_All_Requests();
- }
Advertisement
Add Comment
Please, Sign In to add comment