QOJ.ac

QOJ

حد الوقت: 2 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#7771. Not This Data Structure Problem

الإحصائيات

Given a sequence $a$ of length $n$ and $q$ queries, for each query, calculate the weight $f(a_{l\sim r})$ of the interval $[l, r]$.

The function $f(A)$ is defined as the number of non-empty rounds performed during $m$ rounds of operations, where $m$ is the length of the array $A$.

Specifically, in the $i$-th round (starting from $i=1$ to $m$), we check the relationship between $A_i$ and $A_t$ for all $t$ from $i+1$ to $m$. If $A_i < A_t$, we swap $A_i$ and $A_t$. A round is considered "non-empty" if at least one swap occurs during that round.

For example, for $f([1, 4, 2, 3])$, after each round, the array becomes $[4, 1, 2, 3]$, $[4, 3, 1, 2]$, $[4, 3, 2, 1]$, and $[4, 3, 2, 1]$. The first 3 rounds are non-empty, so $f([1, 4, 2, 3]) = 3$.

Note that the function $f$ does not cause any actual changes to the original array $a$; it is only used to calculate the weight.

Input

The first line contains two integers $n$ and $q$. The next line contains $n$ integers representing the array $a$. The next $q$ lines each contain two integers representing the interval $[l, r]$ for each query.

Output

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

Examples

Input 1

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

Output 1

0
4
2
3
2

Input 2

10 10
4 10 4 8 2 3 3 10 6 8 
3 4
4 4
3 5
8 9
2 5
10 10
1 2
1 5
1 4
7 8

Output 2

1
0
1
0
1
0
1
2
2
1

Input 3

See the provided files.

Constraints

For all data, $1 \le n, q \le 10^6$ and $1 \le a_i \le n$.

Test Case ID $n, q$ Is $a$ a permutation?
$1\sim 1$ $\le 100$ Yes
$2\sim 2$ $\le 100$ No
$3\sim 5$ $\le 3000$ Yes
$6\sim 8$ $\le 3000$ No
$9\sim 10$ $\le 10000$ Yes
$11\sim 12$ $\le 10000$ No
$13\sim 13$ $\le 200000$ Yes
$14\sim 16$ $\le 200000$ No
$17\sim 17$ $\le 1000000$ Yes
$18\sim 20$ $\le 1000000$ No

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.