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

    ID: 18410 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>交互题Special Judge进制2026高校校赛

[ECUSTPC 2026 Spring] 左灯右行

Background

:::epigraph I love you in a way. :::

Problem Description

This problem shares the same background as Problem E “Right Light, Left Walk”. The two problems are closely related, so it is recommended to read the other problem’s statement before attempting either one.

This is an interactive problem.

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

Specifically, the city lies on both sides of the E River. On the north bank there are nn metro stations N1,N2,…,NnN_1, N_2, \dots, N_n, and 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. For the ii-th loop line, the stations it connects are Ni−Ni+1−Si+1−Si−NiN_i - N_{i+1} - S_{i+1} - S_i - N_i. However, these loop lines are all one-way. The direction may be $N_i \leftarrow N_{i+1} \leftarrow S_{i+1} \leftarrow S_i \leftarrow N_i$, or it may be 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, representing the cost of riding one stop between the following 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 riding one stop is determined by the operating direction of the loop line; it could be Ni→Ni+1N_i \to N_{i+1} or Ni←Ni+1N_i \leftarrow N_{i+1}.

Unfortunately, a spacetime black hole has swallowed both the operating directions and the prices on the price lists. Little T plans to ask Big K for help to find out the operating direction of each metro line.

Big K knows the operating directions. However, for each loop line, Little T may arbitrarily assign a new set of prices for its four adjacent edges. Big K knows the true operating directions, and will answer Little T’s queries based on these assigned prices.

Little T may ask at most 35003500 questions:

  • From station xx (it can be NiN_i or SiS_i, same below) to station yy, riding the metro under the fare table assigned by Little T, what is the minimum metro cost that must be paid?
  • If different loop lines connect the same pair of adjacent stations, then all those edges exist simultaneously and can all be used. The cost of one trip is the sum of the weights of the edges traveled, and transfers at stations have no extra cost.

After asking these questions, Little T must confirm with Big K the operating direction of each metro line.

Please help Little T solve this problem.

Note that the directions of the metro lines are predetermined and will not change during the interaction. In other words, the interactor is not adaptive.

Interaction Protocol

For each test point, there is only one set of testdata.

The first line contains an integer n (2≤n≤105)n \ (2 \le n \le 10^5), indicating the parameter. There are 2n2n stations in total and n−1n-1 loop lines in total.

You must first output n−1n-1 lines. In the ii-th line, output 4 integers ui,di,li,riu_i, d_i, l_i, r_i, representing the movement costs between the 4 pairs of adjacent stations on the ii-th loop line: 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}. You must guarantee that these four integers are in [0,109][0, 10^9].

Then the interaction starts immediately. In each interaction, you may ask the interactor a question in the following format:

  • Output one line. First output a character ? to indicate a query, then output 4 elements Sx,idx,Sy,idyS_x, id_x, S_y, id_y. You must ensure that Sx,Sy∈{N,S}S_x, S_y \in \{\texttt{N}, \texttt{S}\} and $id_x, id_y \in \{m \in \mathbb{N} : 1 \le m \le n\}$. This means Little T asks Big K: from the idxid_x-th station on bank SxS_x, i.e. (Sx)idx(S_x)_{id_x}, to the idyid_y-th station on bank SyS_y, i.e. (Sy)idy(S_y)_{id_y}, what is the minimum metro cost that must be paid.
  • If your query is valid and you have not exceeded the query limit, the interactor will output one line containing an integer dd, representing the minimum metro cost between the two stations.
  • If you exceed the corresponding query limit, the interactor will output an integer −1-1 and terminate your interaction process. In this case, you will receive a Wrong Answer verdict.

If you have determined the metro directions, output the answer in the following format:

  • Output one line. First output a character !, then output a string SS of length n−1n-1. The ii-th character Si∈{I,O}S_i \in \{\texttt{I}, \texttt{O}\}. If Si=IS_i = \texttt{I}, it means the direction of the ii-th loop line is $N_i \leftarrow N_{i+1} \leftarrow S_{i+1} \leftarrow S_i \leftarrow N_i$. If Si=OS_i = \texttt{O}, it means the direction of the ii-th loop line is Ni→Ni+1→Si+1→Si→NiN_i \to N_{i+1} \to S_{i+1} \to S_i \to N_i.

After outputting the answer, you should exit safely.

Each output must end with a newline and flush the buffer, otherwise you may get unexpected results.

To flush the buffer, you can:

  • For C or C++, use fflush(stdout) or cout.flush().
  • For Java or Kotlin, use System.out.flush().
  • For Python, use stdout.flush().
3
10
12
10 3 1 2
10 10 1 1
? N 1 N 2
? N 2 N 3
! OI

Hint

Explanation for Sample 1

:::align{center} :::

The figure above shows the operating directions of the metro and Little T’s price markings.

Note that the interactive process in the sample is for reference only. The actual interactive process is not unique, and this example’s interactive process may not be feasible or optimal. The sample for this problem will not appear in the additional files.

Translated by ChatGPT 5