#P8621. [蓝桥杯 2014 国 A] 供水设施
[蓝桥杯 2014 国 A] 供水设施
Description
There are many settlements on Planet X. Pear decides to build a large water project to solve the water supply problem for the settlements under his control. There are now water towers and also settlements. The settlements are on the north side, numbered from to from west to east in a row; the water towers are on the south side, also numbered from to from west to east in a row.
There are vertical one-way water pipelines (powered by pumps) that carry water from the water towers on the south side to the corresponding settlements on the north side.
We may regard both settlements and water towers as points on a plane. The settlement coordinates are , and the water towers are .
In addition to the vertical pipelines, there are one-way horizontal pipelines, connecting and , or connecting and . The former is called a rightward waterway, and the latter is a leftward one. There will not be two waterways overlapping, even if they have different directions.
A schematic of the layout is shown in the figure.

Obviously, the water from each water tower can reach several settlements (not just the corresponding one). For example, in the figure above, water tower can reach settlements , , , and .
Now Pear decides to build one more horizontal one-way pipeline on top of this layout. For convenience, Pear requires this waterway to go from left to right, that is, it connects a point and the point immediately to its right (for example, the horizontal waterway in the figure that connects the two vertical lines and ).
Pear’s goal is that, after building this waterway, as many pairs of water towers and settlements as possible become reachable. In other words, suppose that after construction, the -th water tower can reach settlements; you need to maximize .
By definition, this new waterway must be parallel to the -axis, but its -coordinate does not have to be an integer. Note that although the input has no overlapping waterways, your plan may let the newly built pipeline overlap with existing waterways.
Input Format
The first line contains three positive integers , , , as described in the statement: is the number of vertical lines, is the number of horizontal lines, and is the -coordinate of the settlements.
The next lines each contain three integers. The first two positive integers indicate the starting coordinate of a waterway;
.
Then there is a number or . If it is , the waterway goes left; otherwise, it goes right.
It is guaranteed that all waterways are valid, meaning they will not flow to an undefined location.
Output Format
Output one line containing a positive integer, which is the maximum value of required by the problem.
4 3 2
1 1 1
3 1 0
3 1 1
11
7 9 4
2 3 0
7 2 0
6 3 1
6 1 0
2 1 1
3 3 1
5 2 0
2 2 1
7 1 0
21
Hint
For of the testdata, , .
For of the testdata, , .
For of the testdata, , .
For of the testdata, , .
Time limit: 4 seconds, 256 MB. Lanqiao Cup 2014, the 5th National Contest.
Translated by ChatGPT 5
京公网安备 11011102002149号