732.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include <iostream>
  2. #include <vector>
  3. using std::vector;
  4. using std::cout; using std::endl;
  5. class MyCalendarThree {
  6. public:
  7. MyCalendarThree() {
  8. root = build();
  9. }
  10. int book(int start, int end) {
  11. // end is not included, so -1
  12. update(root, 1, 0, N, start, end - 1);
  13. return query(root, 0, N, 0, N);
  14. }
  15. void print() {
  16. for (int i = 0; i < st.size(); i++) {
  17. SegNode node = st[i];
  18. cout << "pos: " << i << ", k: " << node.k << ", lazy: " << node.lazy << ", lc: " << node.lc << ", rc: " << node.rc << endl;
  19. }
  20. }
  21. private:
  22. struct SegNode {
  23. // "lazy" only apply to children, not node itself
  24. int lazy;
  25. int k;
  26. int lc;
  27. int rc;
  28. };
  29. vector<SegNode> st;
  30. // 10 ^ 9
  31. static constexpr int N = 1000000000;
  32. int root;
  33. int build() {
  34. int pos = st.size();
  35. st.push_back(SegNode{});
  36. return pos;
  37. }
  38. void push_down(int pos) {
  39. if (st[pos].lc == 0) st[pos].lc = build();
  40. if (st[pos].rc == 0) st[pos].rc = build();
  41. st[st[pos].lc].lazy += st[pos].lazy;
  42. st[st[pos].rc].lazy += st[pos].lazy;
  43. }
  44. void push_up(int pos) {
  45. if (st[pos].lc != 0) st[pos].k = std::max(st[pos].k, st[st[pos].lc].k);
  46. if (st[pos].rc != 0) st[pos].k = std::max(st[pos].k, st[st[pos].rc].k);
  47. }
  48. void update(int pos, int val, int l, int r, int ql, int qr) {
  49. if (ql <= l && r <= qr) {
  50. st[pos].k += val;
  51. st[pos].lazy += val;
  52. return;
  53. }
  54. if (st[pos].lazy != 0) {
  55. push_down(pos);
  56. st[st[pos].lc].k += st[pos].lazy;
  57. st[st[pos].rc].k += st[pos].lazy;
  58. st[pos].lazy = 0;
  59. }
  60. int mid = l + (r - l) / 2;
  61. if (ql <= mid) {
  62. if (st[pos].lc == 0) st[pos].lc = build();
  63. update(st[pos].lc, val, l, mid, ql, qr);
  64. }
  65. if (mid < qr) {
  66. if (st[pos].rc == 0) st[pos].rc = build();
  67. update(st[pos].rc, val, mid + 1, r, ql, qr);
  68. }
  69. push_up(pos);
  70. }
  71. int query(int pos, int l, int r, int ql, int qr) {
  72. if (ql <= l && r <= qr) return st[pos].k;
  73. if (st[pos].lazy != 0) {
  74. push_down(pos);
  75. st[st[pos].lc].k += st[pos].lazy;
  76. st[st[pos].rc].k += st[pos].lazy;
  77. st[pos].lazy = 0;
  78. }
  79. int mid = l + (r - l) / 2;
  80. int ans = 0;
  81. if (ql <= mid) {
  82. if (st[pos].lc == 0) st[pos].lc = build();
  83. ans = std::max(ans, query(st[pos].lc, l, mid, ql, qr));
  84. }
  85. if (mid < qr) {
  86. if (st[pos].rc == 0) st[pos].rc = build();
  87. ans = std::max(ans, query(st[pos].rc, mid + 1, r, ql, qr));
  88. }
  89. return ans;
  90. }
  91. };
  92. // [26,35],[26,32],[25,32],[18,26],[40,45],[19,26],[48,50],[1,6],[46,50],[11,18]
  93. // 1 2 3 3 3 3 3 3 3 3
  94. // 1 5 11 17 18 19 25 26 31 34 40 44 46 48 49
  95. // ----------
  96. // ------
  97. // ----------
  98. // ----------
  99. // ------
  100. // ------
  101. // ------
  102. // -----
  103. // ----------
  104. // ----------
  105. int main() {
  106. MyCalendarThree* sol = new MyCalendarThree();
  107. cout << "actual: " << sol->book(26,35) << ", expected: " << 1 << endl;
  108. cout << "actual: " << sol->book(26,32) << ", expected: " << 2 << endl;
  109. cout << "actual: " << sol->book(25,32) << ", expected: " << 3 << endl;
  110. cout << "actual: " << sol->book(18,26) << ", expected: " << 3 << endl;
  111. cout << "actual: " << sol->book(40,45) << ", expected: " << 3 << endl;
  112. cout << "actual: " << sol->book(19,26) << ", expected: " << 3 << endl;
  113. cout << "actual: " << sol->book(48,50) << ", expected: " << 3 << endl;
  114. cout << "actual: " << sol->book(1,6) << ", expected: " << 3 << endl;
  115. cout << "actual: " << sol->book(46,50) << ", expected: " << 3 << endl;
  116. cout << "actual: " << sol->book(11,18) << ", expected: " << 3 << endl;
  117. return 0;
  118. }