For each bar, find the first smaller bar to its left and right. The rectangle using this bar's height extends from just after the left boundary to just before the right boundary.
Use a monotonic increasing stack. When you encounter a bar shorter than the stack top, the current position is the right boundary for that top bar. Pop it, calculate its area, and continue.
Process from left to right. When popping, the new stack top is the left boundary (or -1 if stack becomes empty). The current index is the right boundary.
Add a sentinel bar of height 0 at the end to flush all remaining bars from the stack.