Subpermutations

Description

You are given an array of A consisting of N integers, indexed from 0 to N1. It is known that every element of A is an integer between 1 and N. A subarray (l,r) (0lr<N) of the array A is defined as the array [A[l],A[l+1],,A[r]].

An array b consisting of m integers indexed from 0 to m1 is a permutation if it contains all the integers from 1 to m in any order. In other words, for all 0i<j<m, b[i]b[j] and for all 1xm, there exists 0im1 such that b[i]=x.

Your task is to find the number of subarrays of A that are permutations.

Implementation Details

You should implement the following procedure.

long long count_subperm(int N, std::vector<int> A)
  • N: The length of the array A.
  • A: An array of integers consisting of N integers.
  • This procedure should return one integer containing the number of subarrays of A that are a permutation.
  • This procedure is called exactly once for each test case.

Constraints

  • 1N5000000
  • 1A[i]N for each i such that 0i<N

Subtasks

Subtask Score Additional Constraints
1 8 N1000000; Each integer from 1 to N appears exactly once in A
2 12 N5000
3 31 N200000; A[i]50 for each i such that 0i<N
4 25 N200000; There is exactly one element in A that is equal to 1
5 11 N200000
6 13 No additional constraints.

Examples

Example 1

Consider the following call.

count_subperm(3, [3, 1, 2])	

For this example, the subarrays of A that are permutations are as follows.

  1. The subarray (1,1), which is the array [1]. It has only one distinct element and contains all the integers from 1 to 1.
  2. The subarray (1,2), which is the array [1,2]. It has two distinct elements and contains all the integers from 1 to 2.
  3. The subarray (0,2), which is the array [3,1,2]. It has three distinct elements and contains all the integers from 1 to 3.

Therefore, the procedure should return 3.

Example 2

Consider the following call.

count_subperm(5, [3, 2, 1, 2, 3])	

For this example, the subarrays of A that are permutations are as follows.

  1. The subarray (2,2), which is the array [1]. It has only one distinct element and contains all the integers from 1 to 1.
  2. The subarray (2,3), which is the array [1,2]. It has two distinct elements and contains all the integers from 1 to 2.
  3. The subarray (1,2), which is the array [2,1]. It has two distinct elements and contains all the integers from 1 to 2.
  4. The subarray (0,2), which is the array [3,2,1]. It has three distinct elements and contains all the integers from 1 to 3.
  5. The subarray (2,4), which is the array [1,2,3]. It has three distinct elements and contains all the integers from 1 to 3.

Therefore, the procedure should return 5.

Example 3

Consider the following call.

count_subperm(7, [2, 1, 3, 1, 2, 3, 4])	

For this example, the subarrays of A that are permutations are as follows.

  1. The subarray (1,1), which is the array [1]. It has only one distinct element and contains all the integers from 1 to 1.
  2. The subarray (3,3), which is the array [1]. It has only one distinct element and contains all the integers from 1 to 1.
  3. The subarray (0,1), which is the array [2,1]. It has two distinct elements and contains all the integers from 1 to 2.
  4. The subarray (3,4), which is the array [1,2]. It has two distinct elements and contains all the integers from 1 to 2.
  5. The subarray (0,2), which is the array [2,1,3]. It has three distinct elements and contains all the integers from 1 to 3.
  6. The subarray (2,4), which is the array [3,1,2]. It has three distinct elements and contains all the integers from 1 to 3.
  7. The subarray (3,5), which is the array [1,2,3]. It has three distinct elements and contains all the integers from 1 to 3.
  8. The subarray (3,6), which is the array [1,2,3,4]. It has four distinct elements and contains all the integers from 1 to 4.

Therefore, the procedure should return 8.

Sample Grader

Input Format:

N
A[0] A[1] ... A[N - 1]

Output Format:

C

Here, C is the integer returned by count_subperm.



Submitting to 'Subpermutations'


You're not logged in! Click here to login


Submitting to 'Subpermutations'


You're not logged in! Click here to login


Submitting .cpp to 'Subpermutations'


You're not logged in! Click here to login

Time Limit: 2 Seconds
Memory Limit: 1024MB
Your best score: 0
Source: SEATST 2025 (Day 1)

Subtask Score
1 8
2 12
3 31
4 25
5 11
6 13
7 0