#P91. [CSP-S 2024] Overspeed versus cameras

[CSP-S 2024] Overspeed versus cameras

Person in Charge

Problem Description

Little D has just joined the traffic management department of a certain country. His first task is to monitor speeding vehicles on a north-south main road of length LL. To test Little D, his supervisor first asks him to solve a simplified scenario.

This weekend, nn vehicles are expected on the main road. The ii-th vehicle enters the road at a position did_i from the southernmost end and drives north with an initial speed viv_i and acceleration aia_i (uniform acceleration). We only consider vehicles traveling from south to north, so vi>0v_i > 0, but aia_i can be positive, negative, or zero. A vehicle is considered to have left the main road when it reaches the northernmost end (i.e., at position LL from the southernmost end) or when its speed drops to 00 (which can only happen if ai<0a_i < 0).

The main road is equipped with mm speed detectors. The jj-th detector is located at position pjp_j from the southernmost end. Each detector can be turned on or off. When a vehicle passes an active detector, if its instantaneous speed exceeds the speed limit VV, the vehicle is flagged for speeding. Note that if a vehicle enters or exits the main road at a position with an active detector, the detector will also check the vehicle's speed.

The supervisor first wants to know how many of the nn vehicles will be flagged for speeding if all detectors are turned on.

Additionally, to save energy, the department wants to turn off some detectors. However, they do not want to miss any speeding vehicles. That is, if a vehicle is flagged for speeding when all detectors are on, it must still be flagged after some detectors are turned off. The supervisor also wants to know the maximum number of detectors that can be turned off under this condition.

Given the large value of nn, the supervisor allows Little D to solve these problems programmatically, so Little D turns to you for help.

Definitions and formulas related to acceleration:

  • Uniform acceleration means the object's acceleration remains constant during motion, i.e., the change in speed per unit time is constant.
  • For a vehicle with initial speed v0v_0 and acceleration a0a \neq 0, its speed after time tt is v1=v0+a×tv_1 = v_0 + a \times t, and its displacement (distance traveled) is s=v0×t+0.5×a×t2s = v_0 \times t + 0.5 \times a \times t^2.
  • For a vehicle with initial speed v0v_0 and acceleration a0a \neq 0, when its displacement is ss, its instantaneous speed is v02+2×a×s\sqrt{v_0^2 + 2 \times a \times s}.
  • For a vehicle with initial speed v0v_0 and acceleration a0a \neq 0, when its displacement is v12v022a\frac{v_1^2 - v_0^2}{2a}, its instantaneous speed is v1v_1.

If you use floating-point calculations, be mindful of potential precision issues.

Input Format

The first line of input contains a positive integer TT, the number of test cases.

This is followed by TT test cases, each formatted as follows:

The first line contains four integers nn, mm, LL, VV, representing the number of vehicles, the number of detectors, the length of the main road, and the speed limit, respectively.

The next nn lines:

The ii-th line contains three integers did_i, viv_i, aia_i, describing a vehicle.

The last line contains mm integers p1p_1, p2p_2, \dots, pmp_m, describing the positions of all detectors on the road.

Output Format

For each test case: Output one line containing two integers. The first integer is the number of vehicles flagged for speeding when all detectors are on. The second integer is the maximum number of detectors that can be turned off without missing any speeding vehicles.

Samples

1
5 5 15 3
0 3 0
12 4 0
1 1 4
5 5 -2
6 4 -4
2 5 8 9 15
3 3

Sample 1 Explanation

In this test case, the main road length is 1515, and the speed limit is 33. Detectors are located at 22, 55, 88, 99, and 1515 from the southernmost end.

  • The first vehicle enters at the southernmost end and moves at a constant speed of 33. It does not exceed the speed limit anywhere on the road.
  • The second vehicle enters at position 1212 and moves at a constant speed of 44. When it exits at the northernmost end, the detector at 1515 flags it for speeding.
  • The third vehicle enters at position 11 with an initial speed of 11 and acceleration of 44. After traveling 32122×4=1\frac{3^2 - 1^2}{2 \times 4} = 1 unit of distance (i.e., reaching position 22), its speed becomes 33. It continues to speed afterward. Thus, it is flagged by all detectors except the one at 22.
  • The fourth vehicle enters at position 55 with an initial speed of 55 and acceleration of 2-2. After traveling 32522×(2)\frac{3^2 - 5^2}{2 \times (-2)} units of distance (i.e., reaching position 99), its speed becomes 33. Thus, it is flagged by the detectors at 55 and 88 (positions in [5,9)[5, 9)).
  • The fifth vehicle enters at position 66 with an initial speed of 44 and acceleration of 4-4. After traveling 32422×(4)=78\frac{3^2 - 4^2}{2 \times (-4)} = \frac{7}{8} units of distance (i.e., reaching position 6786\frac{7}{8}), its speed becomes 33. Thus, it exceeds the speed limit in [6,678)[6, 6\frac{7}{8}), but no detectors are present in this interval, so it is not flagged.

Therefore, the second, third, and fourth vehicles are flagged for speeding, so the first output number is 33.

We can turn off the detectors at 22, 88, and 99, keeping only those at 55 and 1515. The three previously flagged vehicles are still flagged. This is the optimal solution, so the second output number is 33.

Sample 2

See the files detect/detect2.in and detect/detect2.ans in the contest directory.

This sample satisfies n,m10n, m \le 10.

Sample 3

See the files detect/detect3.in and detect/detect3.ans in the contest directory.

This sample satisfies Special Property A, with the first 10 test cases satisfying n,m3000n, m \le 3000.

Sample 4

See the files detect/detect4.in and detect/detect4.ans in the contest directory.

This sample satisfies Special Property B, with the first 10 test cases satisfying n,m3000n, m \le 3000.

Sample 5

See the files detect/detect5.in and detect/detect5.ans in the contest directory.

This sample satisfies Special Property C, with the first 10 test cases satisfying n,m3000n, m \le 3000.

Data Range

For all test data, the following guarantees hold:

  • 1T201 \le T \le 20;
  • 1n,m1051 \le n, m \le 10^5, 1L1061 \le L \le 10^6, 1V1031 \le V \le 10^3;
  • 0di<L0 \le d_i < L, 1vi1031 \le v_i \le 10^3, ai103|a_i| \le 10^3;
  • 0p1<p2<<pmL0 \le p_1 < p_2 < \dots < p_m \le L.
Test Case n,mn,m\le Special Property
11 1010 None
22 2020
33 30003000 A
44 10510^5
55 30003000 B
66 10510^5
77 30003000 C
88 10510^5
99 30003000 None
1010 10510^5

Special Property A: ai=0a_i = 0.

Special Property B: ai>0a_i > 0.

Special Property C: ai<0a_i < 0, and no vehicle exits at the northernmost end.