#P16397. [ECUSTPC 2026 Spring] 右灯左行

    ID: 18411 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>线段树2026高校校赛线性 DP

[ECUSTPC 2026 Spring] 右灯左行

Background

:::epigraph Conflict, :::

Problem Description

This problem shares the same background as Problem D "Left Light, Keep Right". The two problems are closely related, so it is recommended to read the text of the other one before trying either problem.

This is not an interactive problem.

Little T is puzzled by the metro map of City T...

Specifically, the city lies on both sides of the River E. On the north bank, there are nn metro stations N1,N2,…,NnN_1, N_2, \dots, N_n. On the south bank, there are nn metro stations S1,S2,…,SnS_1, S_2, \dots, S_n.

City T has a total of n−1n-1 metro lines. Each line is a loop line. The ii-th loop connects the stations Ni−Ni+1−Si+1−Si−NiN_i - N_{i+1} - S_{i+1} - S_i - N_i. However, these loop lines all operate in one direction only: it can be either $N_i \leftarrow N_{i+1} \leftarrow S_{i+1} \leftarrow S_i \leftarrow N_i$ or Ni→Ni+1→Si+1→Si→NiN_i \to N_{i+1} \to S_{i+1} \to S_i \to N_i.

Each loop line has a price list ui,di,li,riu_i, d_i, l_i, r_i, which represent the cost of taking the metro for one stop between these 4 pairs of adjacent stations (Ni−Ni+1N_i - N_{i+1}, Si−Si+1S_i - S_{i+1}, Ni−SiN_i - S_i, and Ni+1−Si+1N_{i+1} - S_{i+1}). Note that the travel direction for one stop is determined by the operating direction of the loop line; for example, it may be Ni→Ni+1N_i \to N_{i+1} or Ni←Ni+1N_i \leftarrow N_{i+1}.

Fortunately, a spacetime white hole has spat out the operating directions and prices of these loop lines. Big K plans to ask Little T to compute some travel costs by metro.

Big K will tell Little T the operating direction and price list of each loop line. Then Little T needs to answer qq questions:

  • From station xx (it can be NiN_i or SiS_i, and the same below) to station yy, under the given metro price lists, what is the minimum metro fare that needs to be paid?
  • If different loop lines connect the same pair of adjacent stations, then all such edges exist and can all be used. The cost of one trip is the sum of the weights of the edges taken, and transfers at stations do not cost extra.

Please help Little T solve this problem.

Input Format

The first line contains an integer T (1≤T≤105)T \ (1 \le T \le 10^5), the number of testdata.

For each testdata, the first line contains two integers nn and q (2≤n≤105,1≤q≤105)q \ (2 \le n \le 10^5, 1 \le q \le 10^5), representing the parameter and the number of queries. There are 2n2n stations in total and n−1n-1 loop lines in total.

Then n−1n-1 lines follow. In the ii-th line, 4 integers $u_i, d_i, l_i, r_i \ (0 \le u_i, d_i, l_i, r_i \le 10^9)$ are given, representing the travel costs between the four pairs of adjacent stations on the ii-th loop: Ni−Ni+1N_i - N_{i+1}, Si−Si+1S_i - S_{i+1}, Ni−SiN_i - S_i, and Ni+1−Si+1N_{i+1} - S_{i+1}.

Then one line contains a string SS of length n−1n-1. The ii-th character Si∈{I,0}S_i \in \{\texttt{I}, \texttt{0}\}. If Si=IS_i = \texttt{I}, then the direction of the ii-th loop is $N_i \leftarrow N_{i+1} \leftarrow S_{i+1} \leftarrow S_i \leftarrow N_i$. If Si=0S_i = \texttt{0}, then the direction of the ii-th loop is Ni→Ni+1→Si+1→Si→NiN_i \to N_{i+1} \to S_{i+1} \to S_i \to N_i.

Then qq lines follow. Each line contains 4 elements Sx,idx,Sy,idyS_x, id_x, S_y, id_y, $(S_x, S_y \in \{\text{N}, \text{S}\}, id_x, id_y \in \{m \in \mathbb{N} : 1 \le m \le n\})$, meaning Big K asks Little T the minimum metro fare needed to travel from the idxid_x-th station on bank SxS_x, (Sx)idx(S_x)_{id_x}, to the idyid_y-th station on bank SyS_y, (Sy)idy(S_y)_{id_y}.

It is guaranteed that over all testdata, ∑n≤3×105\sum n \le 3 \times 10^5 and ∑q≤3×105\sum q \le 3 \times 10^5.

Output Format

For each testdata, output qq lines. In the ii-th line, output an integer dd, the answer to the ii-th query received by Little T, i.e., the minimum metro fare needed to travel from (Sx)idx(S_x)_{id_x} to (Sy)idy(S_y)_{id_y}.

3
3 5
10 3 1 2
10 10 1 1
OI
N 1 N 2
N 2 N 3
N 2 S 2
S 2 N 2
N 1 N 1
6 1
5 1 7 3
2 6 3 4
6 8 1 7
10 0 7 8
4 5 4 9
IOIOI
N 4 S 4
6 1
4 5 7 6
5 0 9 1
1 5 1 5
10 4 7 1
2 1 3 3
OIOIO
S 6 S 4
10
12
1
14
0
14
17

Hint

Explanation for Sample 1

:::align{center} :::

The figure above shows the operating directions and price lists of the metro in the first testdata.

Translated by ChatGPT 5