CF997E.Good Subsegments

NOI/NOI+/CTSC

通过率:0%

时间限制:7.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

A permutation pp of length nn is a sequence p1,p2,…,pnp_1, p_2, \ldots, p_n consisting of nn distinct integers, each of which from 11 to nn (1≤pi≤n1 \leq p_i \leq n) .

Let's call the subsegment [l,r][l,r] of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers pl,pl+1,…,prp_l, p_{l+1}, \dots, p_r.

For example, good segments of permutation [1,3,2,5,4][1, 3, 2, 5, 4] are:

  • [1,1][1, 1],
  • [1,3][1, 3],
  • [1,5][1, 5],
  • [2,2][2, 2],
  • [2,3][2, 3],
  • [2,5][2, 5],
  • [3,3][3, 3],
  • [4,4][4, 4],
  • [4,5][4, 5],
  • [5,5][5, 5].

You are given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n.

You need to answer qq queries of the form: find the number of good subsegments of the given segment of permutation.

In other words, to answer one query, you need to calculate the number of good subsegments [x…y][x \dots y] for some given segment [l…r][l \dots r], such that l≤x≤y≤rl \leq x \leq y \leq r.

长度为 nn 的一个排列 pp 是由 nn 个互不相同的整数组成的序列 p1,p2,…,pnp_1, p_2, \ldots, p_n,其中每个数均在 11 到 nn 之间(即 1≤pi≤n1 \leq p_i \leq n)。

我们称排列的一个子段 [l,r][l,r] 是好的,当且仅当该子段中所有从其最小值到最大值之间的整数,均在 pl,pl+1,…,prp_l, p_{l+1}, \dots, p_r 中出现。

例如,排列 [1,3,2,5,4][1, 3, 2, 5, 4] 的所有好子段为:

  • [1,1][1, 1],
  • [1,3][1, 3],
  • [1,5][1, 5],
  • [2,2][2, 2],
  • [2,3][2, 3],
  • [2,5][2, 5],
  • [3,3][3, 3],
  • [4,4][4, 4],
  • [4,5][4, 5],
  • [5,5][5, 5]。

给定一个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n。

你需要回答 qq 个查询,每个查询的形式为:求给定排列子段中的好子段个数。

换言之,对每个查询,你需要计算满足 l≤x≤y≤rl \leq x \leq y \leq r 的好子段 [x…y][x \dots y] 的个数,其中 [l…r][l \dots r] 是给定的子段。

输入格式

The first line contains a single integer nn (1≤n≤1200001 \leq n \leq 120000) — the number of elements in the permutation.

The second line contains nn distinct integers p1,p2,…,pnp_1, p_2, \ldots, p_n separated by spaces (1≤pi≤n1 \leq p_i \leq n).

The third line contains an integer qq (1≤q≤1200001 \leq q \leq 120000) — number of queries.

The following qq lines describe queries, each line contains a pair of integers ll, rr separated by space (1≤l≤r≤n1 \leq l \leq r \leq n).

第一行包含一个整数 nn(1≤n≤1200001 \leq n \leq 120000)—— 表示排列中元素的个数。

第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \ldots, p_n,以空格分隔(1≤pi≤n1 \leq p_i \leq n)。

第三行包含一个整数 qq(1≤q≤1200001 \leq q \leq 120000)—— 表示查询的个数。

接下来的 qq 行描述各次查询,每行包含一对以空格分隔的整数 ll、rr(1≤l≤r≤n1 \leq l \leq r \leq n)。

输出格式

Print a qq lines, ii-th of them should contain a number of good subsegments of a segment, given in the ii-th query.

输出 qq 行,其中第 ii 行应包含第 ii 个查询中所给定线段的“好”子线段的数量。

输入输出样例

  • 输入#1

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

    输出#1

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

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

首页