QOJ.ac

QOJ

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

#4344. rsrams

Statistiques

Given a sequence $a_1, \dots, a_n$ of length $n$, you need to process $m$ queries. Each query provides $l$ and $r$, and the corresponding answer is:

$\sum\limits_{L=l}^r \sum\limits_{R=L}^r \sum\limits_{c=1}^n c\cdot \left[R-L+1 < 2\sum\limits_{i=L}^R [a_i=c] \right]$

Where $[\mathrm{cond}]$ is $1$ if the condition in the brackets is true, and $0$ otherwise.

Input

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

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

The next $m$ lines each contain two integers $l$ and $r$, representing a query.

Output

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

Examples

Input 1

3 2
1 1 2
1 3
2 3

Output 1

6
3

Examples 2 ~ 3

See the provided files.

Subtasks

Note: Bundled testing is used.

For $30\%$ of the data, $1\le n\le 10^3$, $1\le m\le 10^3$, $1\le a_i\le n$, $1\le l\le r\le n$.

For $60\%$ of the data, $1\le n\le 10^5$, $1\le m\le 10^5$, $1\le a_i\le n$, $1\le l\le r\le n$.

For an additional $20\%$ of the data, $1 \le a_i \le 2$.

For $100\%$ of the data, $1\le n\le 10^6$, $1\le m\le 10^6$, $1\le a_i\le n$, $1\le l\le r\le n$.

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.