Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<list>
- struct Block;
- std::list<Block *> List_;
- struct Block {
- int left, right;
- bool type;
- std::list<Block *>::iterator listPos_it;
- int heapPos;
- Block(int lo, int ro, bool typeo) {
- left = lo;
- right = ro;
- type = typeo;
- listPos_it = List_.end();
- heapPos = -1;
- }
- friend bool operator< (Block a, Block b) {
- if (a.right - a.left < b.right - b.left) {
- return true;
- }
- if (a.right - a.left > b.right - b.left) {
- return false;
- }
- return a.right > b.right;
- }
- int length() {
- return this -> right - this -> left;
- }
- };
- struct Heap {
- static const int CAPACITY = 1000000;
- Block * heap[CAPACITY];
- int size;
- Heap() {
- size = 0;
- }
- void shift_down(int it) {
- int max = it;
- if (2 * it + 1 < size && *heap[max] < *heap[2 * it + 1]) {
- max = 2 * it + 1;
- }
- if (2 * it + 2 < size && *heap[max] < *heap[2 * it + 2]) {
- max = 2 * it + 2;
- }
- if (max != it) {
- std::swap(heap[it], heap[max]);
- std::swap(heap[it] -> heapPos, heap[max] -> heapPos);
- shift_down(max);
- }
- }
- void shift_up(int it) {
- if (it != 0 && *heap[(it - 1) / 2] < *heap[it]) {
- std::swap(heap[it], heap[(it - 1) / 2]);
- std::swap(heap[it] -> heapPos, heap[(it - 1) / 2] -> heapPos);
- shift_up((it - 1) / 2);
- }
- }
- void insert(Block * val) {
- heap[size] = val;
- heap[size] -> heapPos = size;
- shift_up(size);
- size++;
- }
- void erase(int it) {
- std::swap(heap[it], heap[size - 1]);
- heap[it] -> heapPos = it;
- heap[size - 1] -> heapPos = -1;
- heap[size - 1] = 0;
- size--;
- if (it != size) {
- shift_down(it);
- shift_up(it);
- }
- }
- Block * get_max() {
- return heap[0];
- }
- };
- int allocate(int reque_number, int element, Heap * Pyr, std::list<Block *> List, Block ** Array) {
- if (element > Pyr -> get_max() -> length()) {
- return -2;
- }
- Block * RootBlock = Pyr -> get_max();
- Block * FillRootBlock = new Block(RootBlock -> left, RootBlock -> left + element, 1);
- Block * EmptyRootBlock = new Block(RootBlock -> left + element, RootBlock -> right, 0);
- Pyr -> erase(0);
- Pyr -> insert(EmptyRootBlock);
- std::list<Block*>::iterator it = RootBlock -> listPos_it;
- ++it;
- List.insert(List.insert(it, EmptyRootBlock), FillRootBlock);
- List.erase(it);
- Array[reque_number] = FillRootBlock;
- return FillRootBlock -> left;
- }
- void deallocate(int element, Heap * Pyr, std::list<Block *> List, Block ** Array) {
- Block * Cur = Array[-element - 1];
- if (!Cur) return;
- Cur -> type = 0;
- std::list<Block*>::iterator it = Cur -> listPos_it;
- std::list<Block*>::iterator LeftNode = it;
- LeftNode--;
- std::list<Block*>::iterator RightNode = it;
- RightNode++;
- if (RightNode != List.end() && (*RightNode) -> type == 0) {
- Cur -> right = (*RightNode) -> right;
- Pyr -> erase((*RightNode) -> heapPos);
- List.erase(RightNode);
- }
- if (LeftNode != List.begin() && (*LeftNode) -> type == 0) {
- Cur -> left = (*LeftNode) -> left;
- Pyr -> erase((*LeftNode) -> heapPos);
- List.erase(LeftNode);
- }
- Pyr -> insert(Cur);
- Array[-element - 1] = 0;
- }
- int main() {
- int num, req;
- std::cin >> num >> req;
- int * Request = new int[req];
- for (int i = 0; i < req; ++i) {
- std::cin >> Request[i];
- }
- Block * Bl = new Block(0, num, 0);
- Heap * He = new Heap();
- He->insert(Bl);
- List_.insert(List_.begin(), Bl);
- Block ** Mas = new Block *[req];
- for (int i = 0; i < req; ++i) {
- if (Request[i] >= 0) {
- std::cout << allocate(i, Request[i], He, List_, Mas) + 1 << std::endl;
- }
- else {
- deallocate(Request[i], He, List_, Mas);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement