732.cc 4.1 KB

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