QOJ.ac

QOJ

时间限制: 15 s 内存限制: 128 MB 总分: 100

#9971. New Authentic Magical Girl Risuka

统计

Given a permutation $a_1, \dots, a_n$, there are $m$ queries. The $i$-th query provides $m_i$ intervals $[l_j, r_j]$ for $1 \le j \le m_i$, satisfying $1 \le l_j \le r_j \le n$ and $r_j < l_{j+1}$. You need to find the number of pairs $(p, q)$ such that $p < q$, $a_p < a_q$, and there exist $1 \le u < v \le m_i$ such that $l_u \le p \le r_u$ and $l_v \le q \le r_v$.

Input

The first line contains two integers $n$ and $m$.

The next line contains $n$ integers representing $a_1, \dots, a_n$.

For each query, the first line contains $m_i$, followed by $m_i$ lines each containing $l_j, r_j$.

Output

Output $m$ lines, each containing the answer for the corresponding query.

Constraints

For $100\%$ of the data, $1 \le n \le 5 \times 10^5$, $1 \le \sum_{i=1}^m m_i \le 5 \times 10^5$, $m_i \ge 1$, $1 \le a_i \le n$, and all values are integers.

For $0\%$ of the data, $n \le 10^3$ and $\sum_{i=1}^m m_i \le 10^3$.

For an additional $10\%$ of the data, $m_i \le 10$.

For an additional $10\%$ of the data, $m \le 5$.

For the remaining $80\%$ of the data, there are no special restrictions.

Examples

Input 1

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

Output 1

1
0

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.