Advertisement
Guest User

tetris

a guest
Dec 5th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define firstSon my_sons.first.first, my_sons.first.second
  4. #define secondSon my_sons.second.first, my_sons.second.second
  5.  
  6. using namespace std;
  7. #define MIXED -1
  8.  
  9. struct tree {
  10.     int max_height;
  11.     int act_height;
  12.     tree *right_son;
  13.     tree *left_son;
  14.  
  15.     tree() {
  16.         max_height = 0;
  17.         act_height = 0;
  18.         left_son = nullptr;
  19.         right_son = nullptr;
  20.     }
  21. }; // 1 cale biale, 0 cale czarne
  22. tree *MainTree = new tree;
  23.  
  24. /*pair<pair<int, int>, pair<int, int>> sons(int l, int r) {
  25.     int mid = (r + l) / 2;
  26.     auto first = make_pair(l, mid);
  27.     auto second = make_pair(mid + 1, r);
  28.     return make_pair(first, second);
  29. }*/
  30.  
  31. int D; //szerokosc platformy
  32. int N; //number of blocks
  33.  
  34. void build_tree(tree *tli, int l, int r) {
  35.     if (l >= r) {
  36.         return;
  37.     }
  38.     tli->right_son = new tree;
  39.     tli->left_son = new tree;
  40.     int mid = (r + l) / 2;
  41.  
  42.     build_tree(tli->left_son, l, mid);
  43.     build_tree(tli->right_son, mid + 1, r);
  44. }
  45.  
  46. void cumming_on_sons (tree *t) {
  47.     if (t -> left_son) {
  48.         t -> left_son -> act_height = max(t -> act_height,
  49.                                           t -> left_son -> act_height);
  50.     }
  51.     if (t -> right_son) {
  52.         t -> right_son -> act_height = max(t -> act_height,
  53.                                            t -> right_son -> act_height);
  54.     }
  55. }
  56.  
  57. int max_height(tree *t, int lb, int rb, int l, int r) {
  58.     if (!t) {
  59.         return 0;
  60.     }
  61.     if (l == lb && r == rb) {
  62.         return t -> max_height;
  63.     }
  64.     int mid = (rb + lb) / 2;
  65.     if (r <= mid) {
  66.         return max_height(t->left_son, lb, mid, l, r);
  67.     }
  68.     else if (l >= mid + 1) {
  69.         return max_height(t->right_son, mid + 1, rb, l, r);
  70.     }
  71.     else {
  72.     int one_slice = max_height(t->left_son, lb, mid, l, mid);
  73.     int second_slice = max_height(t->right_son, mid + 1, rb, mid + 1, r);
  74.     return max(one_slice, second_slice);
  75.     }
  76. }
  77.  
  78. void put_block(tree *t, int lb, int rb, int l, int r, int new_height) {
  79.     if (!t) {
  80.         return;
  81.     }
  82.     t -> max_height = max(t->max_height, new_height);
  83.     if (l == lb && r == rb) {
  84.         t -> max_height = new_height;
  85.         t -> act_height = new_height;
  86.         return;
  87.     }
  88.     if (t -> act_height != MIXED) {
  89.         cumming_on_sons(t);
  90.     }
  91.     t -> act_height = MIXED;
  92.     int mid = (rb + lb) / 2;
  93.     if (r <= mid) {
  94.         put_block(t->left_son, lb, mid, l, r, new_height);
  95.     }
  96.     else if (mid + 1 <= l) {
  97.         put_block(t->right_son, mid + 1, rb, l, r, new_height);
  98.     }
  99.     else {
  100.         put_block(t->left_son, lb, mid, l, mid, new_height);
  101.         put_block(t->right_son, mid + 1, rb, mid + 1, r, new_height);
  102.     }
  103.     if (t->left_son && t->right_son && t->left_son->act_height == t->right_son->act_height) {
  104.         t->act_height = t->left_son->act_height;
  105.     }
  106. }
  107.  
  108. int main() {
  109.     cin >> D >> N;
  110.     build_tree(MainTree, 1, D);
  111.     for (int i = 0; i < N; ++i) {
  112.         int x, l;
  113.         cin >> l >> x;
  114.         int a = x;
  115.         int b = x + l;
  116.         int new_height = max_height(MainTree, 1, D, a, b) + 1;
  117.         put_block(MainTree, 1, D, a, b, new_height);
  118.         cout << MainTree->max_height << endl;
  119.     }
  120.     cout << MainTree->max_height;
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement