CF997E.Good Subsegments
NOI/NOI+/CTSC
通过率:0%
时间限制:7.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
A permutation p of length n is a sequence p1,p2,…,pn consisting of n distinct integers, each of which from 1 to n (1≤pi≤n) .
Let's call the subsegment [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,…,pr.
For example, good segments of permutation [1,3,2,5,4] are:
- [1,1],
- [1,3],
- [1,5],
- [2,2],
- [2,3],
- [2,5],
- [3,3],
- [4,4],
- [4,5],
- [5,5].
You are given a permutation p1,p2,…,pn.
You need to answer q 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] for some given segment [l…r], such that l≤x≤y≤r.
长度为 n 的一个排列 p 是由 n 个互不相同的整数组成的序列 p1,p2,…,pn,其中每个数均在 1 到 n 之间(即 1≤pi≤n)。
我们称排列的一个子段 [l,r] 是好的,当且仅当该子段中所有从其最小值到最大值之间的整数,均在 pl,pl+1,…,pr 中出现。
例如,排列 [1,3,2,5,4] 的所有好子段为:
- [1,1],
- [1,3],
- [1,5],
- [2,2],
- [2,3],
- [2,5],
- [3,3],
- [4,4],
- [4,5],
- [5,5]。
给定一个排列 p1,p2,…,pn。
你需要回答 q 个查询,每个查询的形式为:求给定排列子段中的好子段个数。
换言之,对每个查询,你需要计算满足 l≤x≤y≤r 的好子段 [x…y] 的个数,其中 [l…r] 是给定的子段。
输入格式
The first line contains a single integer n (1≤n≤120000) — the number of elements in the permutation.
The second line contains n distinct integers p1,p2,…,pn separated by spaces (1≤pi≤n).
The third line contains an integer q (1≤q≤120000) — number of queries.
The following q lines describe queries, each line contains a pair of integers l, r separated by space (1≤l≤r≤n).
第一行包含一个整数 n(1≤n≤120000)—— 表示排列中元素的个数。
第二行包含 n 个互不相同的整数 p1,p2,…,pn,以空格分隔(1≤pi≤n)。
第三行包含一个整数 q(1≤q≤120000)—— 表示查询的个数。
接下来的 q 行描述各次查询,每行包含一对以空格分隔的整数 l、r(1≤l≤r≤n)。
输出格式
Print a q lines, i-th of them should contain a number of good subsegments of a segment, given in the i-th query.
输出 q 行,其中第 i 行应包含第 i 个查询中所给定线段的“好”子线段的数量。
输入输出样例
输入#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测评打分。不知道怎么写?