QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100

#5173. Coloring

Statistiques

You are given a strip of paper with $n$ cells, where the $i$-th cell has color $a_i$.

Every second, Little A can perform one of the following two operations:

  • Paint a cell a certain color.
  • Move to an adjacent cell, provided that the color of the current cell and the target cell are the same.

Little A wants to move along the strip, but he does not want to spend too much time, so he will ask you $Q$ questions. The $i$-th question asks for the minimum time required to move from cell $u_i$ to $v_i$.

Note that each question is independent, meaning you do not need to actually modify the colors on the strip.

Input

The first line contains two positive integers $n$ and $Q$, representing the length of the strip and the number of queries, respectively.

The next line contains $n$ positive integers, where the $i$-th number represents the color $a_i$ of the $i$-th cell on the strip.

The next $Q$ lines each contain two positive integers $u_i$ and $v_i$, representing a query for the minimum time in seconds to move from $u_i$ to $v_i$.

Output

Output $Q$ lines, each containing an integer representing the answer to the corresponding query.

Examples

Input 1

5 4
2 2 3 1 3
1 5
2 4
5 2
3 3

Output 1

6
4
5
0

Constraints

For $100\%$ of the data, $1 \le u_i, v_i \le n \le 10^6$, $1 \le Q \le 10^6$, and $1 \le a_i \le m \le n$.

  • Subtask 1 (3 points): $n \le 10^4$, $Q, m \le 100$.
  • Subtask 2 (13 points): $n, Q \le 10^5$, $m \le 3$.
  • Subtask 3 (18 points): $n, Q \le 5000$.
  • Subtask 4 (66 points): No additional constraints.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.