#P1695. [USACO19FEB] Measuring Traffic B
[USACO19FEB] Measuring Traffic B
Problem Description
Recently, the highway next to Farmer John’s farm has seen a noticeable increase in traffic, or at least it seems so to Farmer John. To confirm this, he plans to use a set of sensors to measure the traffic flow on the highway, where each sensor is used to measure the traffic flow value on a small section of road.
Unfortunately, one day while passing the barn, Farmer John tripped, and the box containing the sensors fell into a huge milk tank. After that, they no longer worked properly. Instead of producing an exact traffic flow reading as before, each sensor can now only output a range of possible results. For example, a sensor might output the range , meaning that the traffic flow on this section of road is at least and at most .
The highway segment passing the farm is miles long, and cars travel only in one direction, from mile to mile . Farmer John wants to install sensors—each monitoring a -mile section of the highway. On some sections, there are on-ramps that allow cars to enter the highway; on every such section, Farmer John will place the sensor on the on-ramp to measure the (approximate) incoming traffic flow. On some sections, there are off-ramps that allow cars to leave the highway; on every such section, Farmer John will place the sensor on the off-ramp. Each section contains at most one ramp. If a section has neither an on-ramp nor an off-ramp, Farmer John will place the sensor on the main road of the highway.
Given the readings from Farmer John’s sensors, determine the most accurate possible ranges for the traffic flow before mile and after mile . These ranges should be consistent with the readings of all sensors.
Input Format
The first line of input contains (). The remaining lines each describe a -mile section, in order from mile to mile . Each line contains a string: on (if this section has an on-ramp), off (if this section has an off-ramp), or none (if this section has no ramp), followed by two integers in the range , giving the lower and upper bounds of the sensor’s reading for this section. If the section contains a ramp, the sensor reading comes from the ramp; otherwise, it comes from the main road. At least one highway section description will be none.
Output Format
Output two integers on the first line: the most accurate possible range of traffic flow before mile . Output two integers on the second line: the most accurate possible range of traffic flow after mile . The input guarantees that a solution satisfying the requirements exists.
4
on 1 1
none 10 14
none 11 15
off 2 3
10 13
8 12
Hint
Sample Explanation 1
In this example, the readings for sections and together tell us that the traffic flow through these two sections is some value in the range , because only this range is consistent with both readings and . At mile , exactly unit of traffic enters through the on-ramp, so before mile , the traffic flow must be within the range . At mile , between and units of traffic leave through the off-ramp, so the possible traffic flow range after this section is .
Translated by ChatGPT 5