segment_tree.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <iostream>
  2. #include "segment_tree.hpp"
  3. #define endl '\n'
  4. using namespace std;
  5. void example1()
  6. {
  7. cout << "\nExample 1" << endl;
  8. // Segment tree that allows setting an element in a range and fetching a single element.
  9. auto st = segment_tree<int, int>()
  10. ->with_merge([&](int &a, int &b) {
  11. return 0;
  12. })
  13. ->with_lazy([&](int &a, int &b, int &c) {
  14. if (a)
  15. {
  16. b = a;
  17. c = a;
  18. }
  19. })
  20. ->with_update([&](int &a, int &b) {
  21. a = b;
  22. })
  23. ->init(4);
  24. st->update(0, 1);
  25. cout << "Set value 1 at position 0" << endl;
  26. st->update(1, 2);
  27. cout << "Set value 2 at position 1" << endl;
  28. cout << "All values" << endl;
  29. for (int i = 0; i < 4; ++i)
  30. cout << st->query(i) << " ";
  31. cout << endl;
  32. cout << "Set value 4 in the range [1,3)" << endl;
  33. st->update(1, 3, 4);
  34. cout << "All values" << endl;
  35. for (int i = 0; i < 4; ++i)
  36. cout << st->query(i) << " ";
  37. cout << endl;
  38. }
  39. void example2()
  40. {
  41. cout << "\nExample 2" << endl;
  42. // Segment tree of maximum that allows setting adding in a point
  43. // It stores a pair on each node:
  44. // - Maximum on the range
  45. // - Lazy to set children
  46. auto st = segment_tree<int, int>()
  47. ->with_merge([&](int &a, int &b) {
  48. return max(a, b);
  49. })
  50. ->with_update([&](int &a, int &b) {
  51. a += b;
  52. })
  53. ->init(4);
  54. st->update(0, 1);
  55. st->update(1, 5);
  56. for (int i = 0; i < 4; ++i)
  57. cout << st->query(i) << " ";
  58. cout << endl;
  59. st->update(1, 3);
  60. st->update(2, 2);
  61. for (int i = 1; i <= 4; ++i)
  62. {
  63. for (int j = 0; j + i <= 4; ++j)
  64. cout << st->query(j, j + i) << " ";
  65. cout << endl;
  66. }
  67. }
  68. void example3()
  69. {
  70. cout << "\nExample 3" << endl;
  71. // Segment tree of maximum that allows setting a value on range
  72. // It stores a pair on each node:
  73. // - Maximum on the range
  74. // - Lazy to set children
  75. typedef pair<int, int> T;
  76. auto st = segment_tree<T, int>()
  77. ->with_merge([&](T &a, T &b) {
  78. return make_pair(max(a.first, b.first), 0);
  79. })
  80. ->with_lazy([&](T &a, T &b, T &c) {
  81. if (a.second)
  82. {
  83. b = make_pair(a.second, a.second);
  84. c = make_pair(a.second, a.second);
  85. }
  86. })
  87. ->with_update([&](T &a, int &b) {
  88. a = make_pair(b, b);
  89. })
  90. ->init(4);
  91. st->update(0, 1);
  92. st->update(1, 5);
  93. for (int i = 0; i < 4; ++i)
  94. cout << st->query(i).first << " ";
  95. cout << endl;
  96. st->update(1, 4, 3);
  97. st->update(2, 4, 2);
  98. for (int i = 1; i <= 4; ++i)
  99. {
  100. for (int j = 0; j + i <= 4; ++j)
  101. cout << st->query(j, j + i).first << " ";
  102. cout << endl;
  103. }
  104. }
  105. int main()
  106. {
  107. example1();
  108. example2();
  109. example3();
  110. return 0;
  111. }