segment_tree.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "statistics/statistics_types.h"
  9. namespace Statistic {
  10. class SegmentTree final {
  11. public:
  12. SegmentTree() = default;
  13. SegmentTree(std::vector<ChartValue> array);
  14. [[nodiscard]] bool empty() const {
  15. return _array.empty();
  16. }
  17. [[nodiscard]] explicit operator bool() const {
  18. return !empty();
  19. }
  20. [[nodiscard]] ChartValue rMaxQ(int from, int to);
  21. [[nodiscard]] ChartValue rMinQ(int from, int to);
  22. private:
  23. struct Node final {
  24. ChartValue sum = 0;
  25. ChartValue max = 0;
  26. ChartValue min = 0;
  27. struct PendingVal {
  28. [[nodiscard]] explicit operator bool() const {
  29. return available;
  30. }
  31. ChartValue value = 0;
  32. bool available = false;
  33. };
  34. PendingVal pendingVal;
  35. int from = 0;
  36. int to = 0;
  37. [[nodiscard]] int size() {
  38. return to - from + 1;
  39. }
  40. };
  41. void build(ChartValue v, int from, int size);
  42. void propagate(ChartValue v);
  43. void change(Node &n, ChartValue value);
  44. [[nodiscard]] ChartValue rMaxQ(ChartValue v, int from, int to);
  45. [[nodiscard]] ChartValue rMinQ(ChartValue v, int from, int to);
  46. [[nodiscard]] bool contains(int from1, int to1, int from2, int to2) const;
  47. [[nodiscard]] bool intersects(
  48. int from1,
  49. int to1,
  50. int from2,
  51. int to2) const;
  52. std::vector<ChartValue> _array;
  53. std::vector<Node> _heap;
  54. };
  55. } // namespace Statistic