#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 NN settlements under his control. There are now NN water towers and also NN settlements. The settlements are on the north side, numbered from 11 to NN from west to east in a row; the water towers are on the south side, also numbered from 11 to NN from west to east in a row.

There are NN 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 (1,K)(N,K)(1,K) \sim (N,K), and the water towers are (1,0)(N,0)(1,0) \sim (N,0).

In addition to the NN vertical pipelines, there are MM one-way horizontal pipelines, connecting (Xi,Yi)(X_i,Y_i) and ((Xi)+1,Yi)((X_i)+1,Y_i), or connecting (Xi,Yi)(X_i,Y_i) and ((Xi)1,Yi)((X_i)-1,Y_i). 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 44 can reach settlements 33, 44, 55, and 66.

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 55 and 66).

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 ii-th water tower can reach AiA_i settlements; you need to maximize A1+A2++AnA_1+A_2+ \cdots +A_n.

By definition, this new waterway must be parallel to the xx-axis, but its yy-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 NN, MM, KK, as described in the statement: NN is the number of vertical lines, MM is the number of horizontal lines, and KK is the yy-coordinate of the settlements.

The next MM lines each contain three integers. The first two positive integers Xi,YiX_i,Y_i indicate the starting coordinate of a waterway;

1XiN,0<Yi<K1 \le X_i \le N,0<Y_i<K.

Then there is a number 00 or 11. If it is 00, 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 A1+A2++AnA_1+A_2+ \cdots +A_n 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 20%20\% of the testdata, N,K20N,K \le 20, M100M \le 100.

For 40%40\% of the testdata, N,K100N,K \le 100, M1000M \le 1000.

For 60%60\% of the testdata, N,K1000N,K \le 1000, M100000M \le 100000.

For 100%100\% of the testdata, N,K50000N,K \le 50000, M105M \le 10^5.

Time limit: 4 seconds, 256 MB. Lanqiao Cup 2014, the 5th National Contest.

Translated by ChatGPT 5