CF997E.Good Subsegments

普及/提高-

通过率:0%

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 ( 1pin1 \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 [xy][x \dots y] for some given segment [lr][l \dots r] , such that lxyrl \leq x \leq y \leq r .

输入格式

The first line contains a single integer nn ( 1n1200001 \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 ( 1pin1 \leq p_i \leq n ).

The third line contains an integer qq ( 1q1200001 \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 ( 1lrn1 \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.

输入输出样例

  • 输入#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
    
首页