#P9296. [POI 2020/2021 R1] Gra platformowa / 平台游戏

[POI 2020/2021 R1] Gra platformowa / 平台游戏

Background

This problem is translated from XXVIII Olimpiada Informatyczna – Stage I Gra platformowa

Problem Description

Bajtazar is playing a game on his new computer.

In this game, there are nn platform levels, numbered from 11 to nn from top to bottom. The player needs to move between these platforms. Each platform has length XX, so the player’s current position can be described by a pair (i,x)(i,x), where ii is the platform level and xx is the position on that platform (11 is the left end, and XX is the right end).

The player starts at the left end of some platform. The goal is to reach the right end of any platform, and the player can only move to the right. To make the game harder, there are holes at some positions on the platforms. In that case, the player may choose a jump operation to directly jump over the hole, or go through the hole to the platform above or below. It is guaranteed that there are no two holes adjacent horizontally or vertically.

More specifically, suppose the player is currently at (i,x)(i,x). The player can perform the following operations:

  • F\texttt{F} operation: If there is no hole at (i,x+1)(i,x+1), the player moves to (i,x+1)(i,x+1); otherwise, the player moves to (i+1,x+1)(i+1,x+1).
  • A\texttt{A} operation: If there is a hole at (i,x+1)(i,x+1), the player moves to (i,x+2)(i,x+2).
  • B\texttt{B} operation: If there is a hole at (i1,x)(i-1,x), the player moves to (i1,x+1)(i-1,x+1).

Now Bajtazar wants to finish the game using as few jump operations as possible (i.e., A\texttt{A} and B\texttt{B} operations). Can you help him accomplish this task?

Input Format

The first line contains three integers n,X,zn,X,z, meaning there are nn platform levels, each platform has length XX, and there are zz queries.

The next nn lines describe the platforms. Line ii starts with an integer kik_i, meaning the ii-th platform has kik_i holes. Then follow kik_i increasing integers giving the positions of the holes on that platform. The input guarantees that there are no holes at the left end or the right end of any platform, and there are no two holes adjacent horizontally or vertically.

The next zz lines each contain an integer pjp_j, meaning that in the jj-th query, the player starts from the left end of platform level pjp_j.

Output Format

For the jj-th query, output one integer on the jj-th line: the minimum number of A\texttt{A} and B\texttt{B} operations needed to start from (pj,1)(p_j,1) and reach the right end of any platform.

3 9 3
1 6
2 3 8
2 5 7
3
2
1
1
1
0
5 20 5
1 16
3 7 15 18
3 6 9 14
3 3 8 11
2 2 5
1
2
3
4
5
0
0
0
1
2

Hint

[Sample Explanation 1]:

pp5jnwd.png

The figure above shows an optimal plan for the first query of Sample 11: the player first performs F\texttt{F} twice to reach (3,3)(3,3), then performs B\texttt{B} once to reach (2,4)(2,4), then performs F\texttt{F} five times to reach (3,9)(3,9).

For the second query, the optimal plan is to perform F\texttt{F} once, then A\texttt{A} once, and finally F\texttt{F} five times.

For the third query, the optimal plan is to perform F\texttt{F} eight times in a row.

[Constraints]:

All testdata satisfy: 1n1051 \leq n \leq 10^5, 1X1091 \leq X \leq 10^9, 1z1051 \leq z \leq 10^5, k2×106\sum k\le 2\times 10^6.

Subtask ID Constraints Score
00 Sample 00
11 z5z \leq 5n×X106n\times X \leq 10^6 3030
22 z5z \leq 5 5050
33 No additional constraints 2020

Translated by ChatGPT 5