#P17273. [eJOI 2026] Automata

    ID: 17392 Type: RemoteJudge 2000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 9 Uploaded By: Tags>交互题Special JudgeeJOI(欧洲)2026

[eJOI 2026] Automata

题目描述

There is a field consisting of NN cells in a row, numbered 00 to N−1N-1 from left to right. Each cell has a distinct height: the height of cell ii is pip_i, and the sequence p0,p1,…,pN−1p_0,p_1,\ldots,p_{N-1} is a permutation of the numbers 0,1,…,N−10,1,\ldots,N-1.

Two cells with indices ii and jj are called close if and only if ∣i−j∣≤1|i-j|\le 1. In particular, every cell is close to itself.

A robot stands on the field, initially placed at some cell. The robot accepts commands of the following two types:

  • MAX: among all cells close to the robot's current cell, its next position is the unique cell with the maximum height;
  • MIN: among all cells close to the robot's current cell, its next position is the unique cell with the minimum height.

Since a cell is close to itself, a command may leave the robot at the same cell.

A program is a finite sequence of commands, where every command is either MAX or MIN. If the robot starts at cell XX and follows program SS, it ends at a uniquely determined cell, denoted by result⁡(S,X)\operatorname{result}(S,X).

You will be asked QQ queries. The ii-th query gives a set of KiK_i starting cells x0,x1,…,xKi−1x_0,x_1,\ldots,x_{K_i-1}. The robot will be placed at one of them, but which one is not known in advance. For each query, determine whether there is a program that moves the robot to the same final cell regardless of the chosen starting position.

Formally, determine whether there exists a program SS such that

$\operatorname{result}(S,x_0)=\operatorname{result}(S,x_1)=\cdots=\operatorname{result}(S,x_{K_i-1}).$

The permutation pp is the same for all queries.

You do not need to construct such a program; only report whether one exists.

Implementation details

Implement the following two functions:

void initialize(std::vector<int> p)
  • pp: a permutation of the numbers from 00 to N−1N-1.
bool exists_program(std::vector<int> x)
  • xx: the cells for one query, given in strictly increasing order.

initialize is called exactly once, before any calls to exists_program.

exists_program is called QQ times, once for each query. It must return true if there exists a program after which the robot ends at the same cell no matter which given cell it started from, and false otherwise.

输入格式

Input format:

  • line 11: two integers NN and QQ;
  • line 22: NN integers p0,p1,…,pN−1p_0,p_1,\ldots,p_{N-1}, where pip_i is the height of cell ii;
  • line 3+i3+i: an integer KiK_i, followed by KiK_i integers x0,x1,…,xKi−1x_0,x_1,\ldots,x_{K_i-1} describing the ii-th query.

输出格式

Output format:

  • line 11: a binary string of length QQ whose ii-th character is 1 if the answer to query ii is true, and 0 otherwise.
3 3
0 2 1
2 0 2
3 0 1 2
2 1 2
111
7 3
0 4 2 1 3 5 6
3 0 1 3
3 1 3 4
2 3 6
001

提示

Explanation of example 1

Here p=[0,2,1]p=[0,2,1]. For the second query, the robot may start at cell 00, 11, or 22. Consider the program [MAX][\texttt{MAX}].

  • Starting at cell 00, the close cells are 00 and 11. Since p0<p1p_0<p_1, the robot moves to cell 11.
  • Starting at cell 11, the close cells are 00, 11, and 22. Since p0<p1p_0<p_1 and p1>p2p_1>p_2, the robot stays at cell 11.
  • Starting at cell 22, the close cells are 11 and 22. Since p1>p2p_1>p_2, the robot moves to cell 11.

Thus a program exists that ends at cell 11 from all three starting cells, so the answer is true. Other valid programs include [MIN,MAX][\texttt{MIN},\texttt{MAX}], $[\texttt{MIN},\texttt{MIN},\texttt{MAX},\texttt{MIN},\texttt{MAX},\texttt{MIN}]$, and [MAX,MIN,MAX][\texttt{MAX},\texttt{MIN},\texttt{MAX}].

Explanation of example 2

Here p=[0,4,2,1,3,5,6]p=[0,4,2,1,3,5,6].

For the first two queries, no program can make the robot end at the same cell from every given starting cell, so both answers are false.

For the last query, the robot may start at cell 33 or 66. Consider [MAX,MAX,MAX][\texttt{MAX},\texttt{MAX},\texttt{MAX}].

  • Starting at cell 33, the close cells are 22, 33, and 44. Since p3<p4p_3<p_4 and p2<p4p_2<p_4, the robot moves to cell 44. Applying the next two commands similarly, it ends at cell 66.
  • Starting at cell 66, it remains at cell 66 throughout.

Therefore the answer is true. The program $[\texttt{MIN},\texttt{MAX},\texttt{MAX},\texttt{MAX}]$ also works. In contrast, [MAX,MAX][\texttt{MAX},\texttt{MAX}] ends at cell 55 from cell 33, but at cell 66 from cell 66.

Constraints

  • 3≤N≤2⋅1053\le N\le 2\cdot 10^5
  • 1≤Q≤5⋅1051\le Q\le 5\cdot 10^5
  • p0,p1,…,pN−1p_0,p_1,\ldots,p_{N-1} is a permutation of 0,1,…,N−10,1,\ldots,N-1
  • 2≤Ki≤N2\le K_i\le N
  • 0≤x0<x1<⋯<xKi−1<N0\le x_0<x_1<\cdots<x_{K_i-1}<N for every query
  • The sum of KiK_i over all QQ queries does not exceed 10610^6

Subtasks

Subtask Points NN QQ KiK_i Additional constraints
0 - The examples.
1 3 ≤2⋅105\le 2\cdot 10^5 ≤5⋅105\le 5\cdot 10^5 =2=2 If the answer is positive, a valid program using exactly 11 command exists.
2 7 If the answer is positive, a valid program using at most 55 commands exists.
3 9 ≤100\le 100 ≤500\le 500 -
4 17 ≤5000\le 5000 ≤5⋅105\le 5\cdot 10^5
5 7 ≤2⋅105\le 2\cdot 10^5 x1=x0+1x_1=x_0+1 for every query.
6 8 ≤5000\le 5000 ≤N\le N -
7 13 ≤2⋅105\le 2\cdot 10^5 There exist 0<a<b<N−10<a<b<N-1 such that $p_0<p_1<\cdots<p_a>p_{a+1}>\cdots>p_b<p_{b+1}<\cdots<p_{N-1}$.
8 p0<p1>p2<p3>⋯<pN−1p_0<p_1>p_2<p_3>\cdots<p_{N-1} if NN is even; p0<p1>p2<p3>⋯>pN−1p_0<p_1>p_2<p_3>\cdots>p_{N-1} if NN is odd.
9 23 -