Subpermutations
Description
You are given an array of consisting of integers, indexed from to . It is known that every element of is an integer between and . A subarray of the array is defined as the array .
An array consisting of integers indexed from to is a permutation if it contains all the integers from to in any order. In other words, for all , and for all , there exists such that .
Your task is to find the number of subarrays of that are permutations.
Implementation Details
You should implement the following procedure.
long long count_subperm(int N, std::vector<int> A)
- : The length of the array .
- : An array of integers consisting of integers.
- This procedure should return one integer containing the number of subarrays of that are a permutation.
- This procedure is called exactly once for each test case.
Constraints
Subtasks
Subtask |
Score |
Additional Constraints |
1 |
|
; Each integer from to appears exactly once in |
2 |
|
|
3 |
|
; for each such that |
4 |
|
; There is exactly one element in that is equal to |
5 |
|
|
6 |
|
No additional constraints. |
Examples
Example 1
Consider the following call.
count_subperm(3, [3, 1, 2])
For this example, the subarrays of that are permutations are as follows.
- The subarray , which is the array . It has only one distinct element and contains all the integers from to .
- The subarray , which is the array . It has two distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
Therefore, the procedure should return .
Example 2
Consider the following call.
count_subperm(5, [3, 2, 1, 2, 3])
For this example, the subarrays of that are permutations are as follows.
- The subarray , which is the array . It has only one distinct element and contains all the integers from to .
- The subarray , which is the array . It has two distinct elements and contains all the integers from to .
- The subarray , which is the array . It has two distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
Therefore, the procedure should return .
Example 3
Consider the following call.
count_subperm(7, [2, 1, 3, 1, 2, 3, 4])
For this example, the subarrays of that are permutations are as follows.
- The subarray , which is the array . It has only one distinct element and contains all the integers from to .
- The subarray , which is the array . It has only one distinct element and contains all the integers from to .
- The subarray , which is the array . It has two distinct elements and contains all the integers from to .
- The subarray , which is the array . It has two distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
- The subarray , which is the array . It has three distinct elements and contains all the integers from to .
- The subarray , which is the array . It has four distinct elements and contains all the integers from to .
Therefore, the procedure should return .
Sample Grader
Input Format:
N
A[0] A[1] ... A[N - 1]
Output Format:
C
Here, is the integer returned by count_subperm
.