3D Histogram
Time limit2.5sMemory limit512 MB
Given n blocks with heights a_i and lengths b_i, find the largest volume of a box over any run of adjacent blocks, where the box's height and length are the minimums of a and b over that run.
- Level
Hard8 of 10
- Topics
- Array, Divide and conquer, Segment tree
- Solved
- No attempts yet
Problem
Mr. Malnar (over the phone): Listen, I had to put up some posters around Zagreb in the middle of the night. I came across a fence made of planks of varying heights, and I was thinking about how to calculate the largest area of a poster that can fit on the fence. Wouldn't that make a nice COCI problem?
Associate: You were doing what?! Anyway, the problem is no good. It's a standard trick with a monotone queue. We even teach it to elementary school students at our camps.
Mr. Malnar: What if you twist it a bit and ask for the answer for every prefix or something like that? That should be hard enough.
Associate: That exact problem appeared in our CERC qualification contest last year. It was tough, and it boils down to the Harbingers trick, but now everyone has seen it.
Mr. Malnar: Are you sure there is nothing we can do?
Associate: I think we have exhausted all the histogram problems. COCI 2010/2011 (Tabovi), COCI 2015/2016 (Poplava), COCI 2017/2018 (Krov), IOI selection test 2018 (Histogram)... You get the point.
Mr. Malnar: What if the histogram is three-dimensional?
Associate: Umm...
You are given a 3D histogram that consists of blocks placed next to each other. The -th block is 1 meter wide, meters tall, and meters long. From the front, the histogram looks like bars of heights . From the top, it looks like bars of heights .
Find the block with the maximum volume that can be placed inside the given 3D histogram. The sides of that block must be parallel to the sides of the blocks that make up the histogram.
Input
The first line contains the integer . The -th of the following lines contains integers and ().
Output
Print the volume in cubic meters.
Hint
Clarification of the first example:
The largest block uses parts of the first two blocks. It is 2 meters wide, 4 meters tall, and 3 meters long. Its volume is cubic meters.
