#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 [7,13][7,13], meaning that the traffic flow on this section of road is at least 77 and at most 1313.

The highway segment passing the farm is NN miles long, and cars travel only in one direction, from mile 11 to mile NN. Farmer John wants to install NN sensors—each monitoring a 11-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 NN sensors, determine the most accurate possible ranges for the traffic flow before mile 11 and after mile NN. These ranges should be consistent with the readings of all NN sensors.

Input Format

The first line of input contains NN (1N1001\le N\le 100). The remaining NN lines each describe a 11-mile section, in order from mile 11 to mile NN. 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 010000\ldots 1000, 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 11. Output two integers on the second line: the most accurate possible range of traffic flow after mile NN. 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 22 and 33 together tell us that the traffic flow through these two sections is some value in the range [11,14][11,14], because only this range is consistent with both readings [10,14][10,14] and [11,15][11,15]. At mile 11, exactly 11 unit of traffic enters through the on-ramp, so before mile 11, the traffic flow must be within the range [10,13][10,13]. At mile 44, between 22 and 33 units of traffic leave through the off-ramp, so the possible traffic flow range after this section is [8,12][8,12].

Translated by ChatGPT 5