#P4532. [CTSC2005] 合并正方形

[CTSC2005] 合并正方形

Description

A two-dimensional plane is initially empty. There is a sequence of commands that add points to the plane.

There are two kinds of points, called type A points and type B points (see Figure 1, where black squares denote type A points and small black dots denote type B points). Type A points always lie on the XX-axis and never overlap, while type B points can appear anywhere on the plane and may overlap. Each type B point has a weight WW.

Processing: (1) Initially, between every pair of adjacent type A points, draw a square whose sides make a 4545-degree angle with the XX-axis (see Figure 2). (2) Each time, you may merge any two squares that share at least one common point into a larger square; after merging, the two smaller squares disappear. The second and third squares from the left in Figure 2 are merged and shown as the gray-edged square in Figure 3.

After merging, the resulting square divides the plane into 99 regions. The 44 regions adjacent to the 44 sides of the square are labeled I, II, III, IV in Figure 3. Let w1w_1 be the sum of weights of type B points falling into region I, w2w_2 for region II, w3w_3 for region III, and w4w_4 for region IV. Let w5w_5 be the sum of weights of type B points falling inside the gray square (type B points are guaranteed not to lie on the boundaries of any region). Then the cost of this merge is w1+2w2+3w3+4w4+5w5w_1 + 2 w_2 + 3 w_3 + 4 w_4 + 5 w_5. Type B points in other regions are ignored. Each merge does not affect the positions or weights of the type B points.

Each time you perform a merge, the number of squares formed by type A points decreases by one, until only one square remains. The total merge cost is the sum of the costs of all merges. Different merge orders may result in different total costs.

Points are added to the plane one by one. After adding the ii-th type A point, the plane contains ii type A points and all previously added type B points. Let the minimum total merge cost at this moment be f(i)f(i).

Given a cost limit LL, compute the maximum number KK of type A points such that the minimum total merge cost for the first KK type A points does not exceed LL, i.e., f(K)Lf(K) \le L.

Input Format

The first line contains two numbers MM, LL, meaning there are MM add-point commands and the cost limit is LL.

Then follow MM lines, each starting with a letter indicating the point type. A denotes a type A point, and B denotes a type B point. For a type A point, one number follows giving its XX-coordinate; for a type B point, three numbers follow giving its XX-coordinate, YY-coordinate, and weight.

Output Format

Output a single integer KmaxK_{max}, which is the largest KK such that f(K)Lf(K) \le L.

8 30.0
A -2
A 0
B 7 8 5.0
B 4 -3 2.0
B -3 4 1.0
A 2
B -4 5 1.0
A 4
3

Hint

Sample explanation:

In the state after adding the last point, all points are as shown below. The number next to each type B point is its weight.

The minimum cost for the first 33 type A points is f(3)=27f(3) = 27, and for the first 44 type A points is f(4)=36f(4) = 36. Since f(3)<30f(3) < 30 and f(4)>30f(4) > 30, the maximum KK is 33.

Constraints:

  • 3number of type A points300003 \le \text{number of type A points} \le 30000.
  • 5M1000005 \le M \le 100000.
  • XX, YY are integers with absolute value at most 1000000010000000.
  • LL, WW are real numbers with 0<W100000 < W \le 10000, L1011L \le 10^{11}; all input real numbers have at most three decimal places.
  • 50%50\% of the testdata satisfies M3000M \le 3000.

Translated by ChatGPT 5