CF1824D.LuoTianyi and the Function

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

LuoTianyi gives you an array aa of nn integers and the index begins from 11.

Define g(i,j)g(i,j) as follows:

  • g(i,j)g(i,j) is the largest integer xx that satisfies ap:i≤p≤j⊆aq:x≤q≤j{a_p:i\le p\le j}\subseteq{a_q:x\le q\le j} while i≤ji \le j;
  • and g(i,j)=0g(i,j)=0 while i>ji \gt j.

There are qq queries. For each query you are given four integers l,r,x,yl,r,x,y, you need to calculate ∑i=lr∑j=xyg(i,j)\sum\limits_{i=l}^{r}\sum\limits_{j=x}^{y}g(i,j).

洛天依给你一个包含 nn 个整数的数组 aa,数组下标从 11 开始。

定义函数 g(i,j)g(i,j) 如下:

  • 当 i≤ji \le j 时,g(i,j)g(i,j) 是满足 {ap∣i≤p≤j}⊆{aq∣x≤q≤j}\{a_p \mid i\le p\le j\} \subseteq \{a_q \mid x\le q\le j\} 的最大整数 xx;
  • 当 i>ji > j 时,g(i,j)=0g(i,j) = 0。

共有 qq 次查询。每次查询给出四个整数 l,r,x,yl,r,x,y,你需要计算 ∑i=lr∑j=xyg(i,j)\sum\limits_{i=l}^{r}\sum\limits_{j=x}^{y}g(i,j)。

输入格式

The first line contains two integers nn and qq (1≤n,q≤1061\le n,q\le 10^6) — the length of the array aa and the number of queries.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (1≤ai≤n1\le a_i\le n) — the elements of the array aa.

Next qq lines describe a query. The ii-th line contains four integers l,r,x,yl,r,x,y (1≤l≤r≤n,1≤x≤y≤n1\le l\le r\le n, 1\le x\le y\le n) — the integers in the ii-th query.

第一行包含两个整数 nn 和 qq(1≤n,q≤1061\le n,q\le 10^6)—— 分别表示数组 aa 的长度以及查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(1≤ai≤n1\le a_i\le n)—— 数组 aa 的元素。

接下来的 qq 行描述每次查询。第 ii 行包含四个整数 l,r,x,yl,r,x,y(1≤l≤r≤n,1≤x≤y≤n1\le l\le r\le n, 1\le x\le y\le n)—— 第 ii 次查询中的参数。

输出格式

Print qq lines where ii-th line contains one integer — the answer for the ii-th query.

输出 qq 行,其中第 ii 行包含一个整数——即第 ii 个查询的答案。

输入输出样例

  • 输入#1

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

    输出#1

    6
    6
    0
    6
  • 输入#2

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

    输出#2

    4
    2
    6
    4
    80

说明/提示

In the first example:

In the first query, the answer is g(1,4)+g(1,5)=3+3=6g(1,4)+g(1,5)=3+3=6.

x=1,2,3x=1,2,3 can satisfies ap:1≤p≤4⊆aq:x≤q≤4{a_p:1\le p\le 4}\subseteq{a_q:x\le q\le 4}, 33 is the largest integer so g(1,4)=3g(1,4)=3.

In the second query, the answer is g(2,3)+g(3,3)=3+3=6g(2,3)+g(3,3)=3+3=6.

In the third query, the answer is 00, because all i>ji \gt j and g(i,j)=0g(i,j)=0.

In the fourth query, the answer is g(6,6)=6g(6,6)=6.

In the second example:

In the second query, the answer is g(2,3)=2g(2,3)=2.

In the fourth query, the answer is g(1,4)+g(1,5)=2+2=4g(1,4)+g(1,5)=2+2=4.

在第一个例子中:

第一个查询的答案是 g(1,4)+g(1,5)=3+3=6g(1,4)+g(1,5)=3+3=6。

满足 {ap:1≤p≤4}⊆{aq:x≤q≤4}\{a_p:1\le p\le 4\}\subseteq\{a_q:x\le q\le 4\} 的 xx 可取 1,2,31,2,3,其中最大的整数为 33,因此 g(1,4)=3g(1,4)=3。

第二个查询的答案是 g(2,3)+g(3,3)=3+3=6g(2,3)+g(3,3)=3+3=6。

第三个查询的答案是 00,因为对所有 i>ji > j 均有 g(i,j)=0g(i,j)=0。

第四个查询的答案是 g(6,6)=6g(6,6)=6。

在第二个例子中:

第二个查询的答案是 g(2,3)=2g(2,3)=2。

第四个查询的答案是 g(1,4)+g(1,5)=2+2=4g(1,4)+g(1,5)=2+2=4。

输入解题思路,AI测评打分。不知道怎么写?

首页