Блог пользователя slow_coder4

Автор slow_coder4, история, 8 лет назад, По-английски

How can solve this-> http://www.lightoj.com/volume_showproblem.php?problem=1083 problem with segment tree. Please give me some hints.. Thanks..

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

»
8 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I think it is not segment tree problem.It is just histogram problem.You have to find largeast area with stack.

»
8 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Like Kerim.K suggested above, this problem can be solved in a simpler and faster way with a stack, but I think you're asking specifically for the Segment Tree solution, so I'll explain it.

You can use the Segment Tree to find, for every element, the rightmost element to the left and the leftmost element to the right that have a lower height. Let those indices be l and r respectively, and the height of the current element be h, then the area of the rectangle you can create is h * (r - l - 1). To do this with the Segment Tree, the parameter is the height and the value you update it with is the index. You can either use 2 different Segment Trees (one for min queries and the other for max queries) or you can get away with a single Segment Tree by negating values and initializing accordingly. I'll share my code, which uses one single Segment Tree.

C++ Code
  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    would you explain more about your update function? how is it working?

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      It's a regular update function for a Max Segment Tree. The only trick is that when we query for minimum element, instead of changing functions for updating and querying, we just use negative values.