#P3644. [APIO2015] 巴邻旁之桥

    ID: 1440 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2015线段树APIO概率论,统计期望

[APIO2015] 巴邻旁之桥

Description

An east–west Mu Xi River divides Palembang City into two parts, region AA and region BB.

Exactly 10000000011000000001 buildings are built along each bank. Along each bank, buildings are numbered from 00 to 10000000001000000000. Every pair of adjacent buildings is 11 unit apart, and the river is 11 unit wide. In region AA, building ii is directly opposite building ii in region BB.

There are NN residents in the city. The ii-th resident’s home is at building SiS_i in region PiP_i, and their office is at building TiT_i in region QiQ_i. A resident’s home and office may lie on different banks of the river, in which case they must take a boat to commute, which many people find inconvenient. To allow residents to drive to work, the government decides to build at most KK bridges across the river.

Due to technical reasons, each bridge must connect the two banks exactly, be strictly perpendicular to the river, and no two bridges may intersect.

After the government builds at most KK bridges, let DiD_i be the shortest driving distance from the ii-th resident’s home to their office. Help the government build the bridges to minimize D1+D2++DND_1 + D_2 + \cdots + D_N.

Input Format

The first line contains two positive integers KK and NN, the maximum number of bridges and the number of residents.

Each of the next NN lines contains four parameters: PiP_i, SiS_i, QiQ_i, and TiT_i, meaning that the ii-th resident’s home is at building SiS_i in region PiP_i, and their office is at building TiT_i in region QiQ_i.

Output Format

Output a single line containing one integer, the minimum value of D1+D2++DND_1 + D_2 + \cdots + D_N.

1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
24

2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
22

Hint

Constraints

For all testdata, it is guaranteed that PiP_i and QiQ_i are one of the characters "A" and "B", 0Si,Ti10000000000 \leq S_i, T_i \leq 1000000000, and there may be more than 11 home or office (or both) in the same building.

Subtask 1 (8 points) K=1K = 1
1N10001 \leq N \leq 1000.

Subtask 2 (14 points) K=1K = 1
1N1000001 \leq N \leq 100000.

Subtask 3 (9 points) K=2K = 2
1N1001 \leq N \leq 100.

Subtask 4 (32 points) K=2K = 2
1N10001 \leq N \leq 1000.

Subtask 5 (37 points) K=2K = 2
1N1000001 \leq N \leq 100000.

Translated by ChatGPT 5