QOJ.ac

QOJ

Time Limit: 3 s Memory Limit: 512 MB Total points: 100

#9973. 魔法少女网站第二部

统计

给一个长为 $n$ 的排列 $a_1,\dots, a_n$,有 $m$ 次询问,每次询问区间 $[l, r]$ 内,排序后相邻的数在原序列中的位置的差的绝对值之和。

输入格式

第一行两个数表示 $n, m$;

之后一行 $n$ 个数依次表示序列 $a$ 中的元素;

之后 $m$ 行,每行两个数 $l, r$ 表示一次查询。

输出格式

对于每次询问,输出一行一个数表示答案。

样例数据

样例输入

5 2
5 4 2 3 1
3 4
2 5

样例输出

1
5

样例解释

第一个询问,$2,3$ 排序后为 $2,3$,在原序列中的位置为 $3,4$,相邻元素在原序列中位置差的绝对值之和为 $|3 - 4| = 1$。

第二个询问,$4, 2, 3, 1$ 排序后为 $1, 2, 3, 4$,在原序列中的位置为 $5, 3, 4, 2$,相邻元素在原序列中位置差的绝对值之和为 $|5 - 3| + |3 - 4| + |4 - 2| = 5$。

子任务

Idea:?,Solution:ccz181078,Code:nalemy,Data:ccz181078

对于 $100\%$ 的数据,满足 $1 \leq n, m \leq 2\times 10^6$,$1 \leq a_i \leq n$,$a_i$ 互不相同,$1 \leq l \leq r \leq n$,所有数值为整数。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.