slow_coder4's blog

By slow_coder4, history, 8 years ago, In English

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

  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
8 years ago, # |
  Vote: I like it +3 Vote: I do not like it

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
  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

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

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.