#P15804. [GESP202603 八级] 消息查找

    ID: 17869 远端评测题 1000ms 512MiB 尝试: 5 已通过: 1 显示难度提高 上传者: 标签>动态规划 DP离散化广度优先搜索 BFS动态规划优化最短路2026GESP

[GESP202603 八级] 消息查找

Background

Related multiple-choice and true/false problems: https://ti.luogu.com.cn/problemset/1212.

Problem Description

There are nn messages in A's chat history, numbered 1,2,,n1, 2, \dots, n in order. A message with a smaller number was sent earlier than a message with a larger number.

A message may quote a message with a smaller number, or it may quote nothing. A noticed that the number of messages that contain quotes will not be very large. An example of a chat history is:

  • [Message 1] A: Has anyone done today's first problem?
  • [Message 2] A: I got WA on the first problem, what could be the reason?
  • [Message 3: quoting message 1] B: Me me me
  • [Message 4: quoting message 2] C: I also got WA
  • [Message 5: quoting message 2] B: Did you forget to use long long?
  • [Message 6: quoting message 5] A: After changing it, I got AC. That's awesome!

For message ii (1in1 \le i \le n), A uses rir_i to mark whether message ii has a quote and the message number it quotes. If ri>0r_i > 0, then message ii quotes message rir_i; if ri=0r_i = 0, then message ii quotes no message.

There are a lot of messages in the chat history. To quickly find the needed messages, A plans to implement a simple message search tool. At any moment, the tool can be positioned at exactly one message. If it is currently at message ii (1<in1 < i \le n), then next you may choose one of the following two operations:

  • Move to message i1i - 1.
  • If message ii quotes message rir_i, move to message rir_i.

You may perform the operations any number of times (including zero times).

A has qq queries. In the kk-th query (1kq1 \le k \le q), A gives message numbers xk,ykx_k, y_k (yk<xky_k < x_k). A wants to know: if the tool is currently at xkx_k, what is the minimum number of operations needed to move to yky_k.

Input Format

The first line contains two positive integers n,qn, q, representing the number of messages and the number of queries.

The second line contains nn non-negative integers r1,r2,,rnr_1, r_2, \dots, r_n, describing the quoting relationships. The meaning is as described above.

Each of the next qq lines, the kk-th line (1kq1 \le k \le q) contains two positive integers xk,ykx_k, y_k, representing one query.

It is guaranteed that there are at most 1000 messages that contain quotes.

Output Format

Output qq lines. Each line contains one integer, representing the minimum number of operations needed to switch from message xkx_k to message yky_k.

6 3
0 0 1 2 2 5
4 1
6 2
6 3
2
2
3
5 5
0 0 0 1 3
4 1
4 2
5 1
5 2
5 3
1
2
2
2
1

Hint

Constraints

For 40%40\% of the test points, it is guaranteed that 1n20001 \le n \le 2000 and 1q20001 \le q \le 2000.

For all test points, it is guaranteed that 1n1051 \le n \le 10^5, 1q1051 \le q \le 10^5, 0ri<i0 \le r_i < i, 1yk<xkn1 \le y_k < x_k \le n, and there are at most 1000 messages that contain quotes.

Translated by ChatGPT 5