B2. Biodiverse Subarray

Codeforces
IDCF10291B2
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
_The problem statements of Biodiverse Subsequence and Biodiverse Subarray are nearly identical, save for whether the sampled corals must form a_ subsequence _or_ subarray _of the given sequence. Despite this, the two are very different problems._ Biodiversity is one of the most important properties in any ecosystem, whether it's in a forest, a mangrove, or even the ocean. The Philippines is rich with species of flora and fauna that are endemic to the country. Cindy, an avid scuba diver, has accompanied a group of biologists that ventured to the Verde Island Passage to sample some coral species for their research. Of course, it is important that the samples that they choose properly reflect the diversity of marine life found at the Coral Triangle. They find a row of $N$ corals and identify $K$ distinct species among them. We model this as a sequence of $N$ positive integers $A_1, A_2, \\dots, A_N$. Each of the integers from $1$ to $K$ appears at least once, and these are the only integers to appear in the sequence. A sequence of numbers is called _biodiverse_ if each of the integers from $1$ to $K$ appears at least once each in the sequence. To preserve the properties of the habitat, the researchers must choose a biodiverse _subarray_ of corals to sample from. A subarray is a *contiguous* subsequence of elements $A_l, A_{l + 1}, \\dots A_r$ where $1 <= l <= r <= N$. In order to make a decision on which corals to sample, the researchers ask Cindy to count the number of biodiverse subarrays of $A$. Help Cindy out? The first line of input contains two space-separated integers $N$ and $K$, the number of corals and the number of distinct species, respectively. The second line of input contains $N$ space-separated integers $A_1$, $A_2$, $\\\\cdots$, $A_N$, where $A_i$ is the species of the $i$th coral in the row. Output a single line containing a single integer, the number of biodiverse subarrays of $A$. $1 <= K <= N <= 2 dot.op 10^5$ $1 <= A_i <= K$ for all $i$. There exists an $i$ such that $A_i = k$ for all $k$ from $1$ to $K$. *Subtask 1* (40 points): $N <= 100$ *Subtask 2* (20 points): $N <= 2000$ *Subtask 3* (20 points): $K <= 2$ *Subtask 4* (20 points): No further constraints. The below diagram shows the six biodiverse subarrays that the researchers can choose from in the first sample test case. ## Input The first line of input contains two space-separated integers $N$ and $K$, the number of corals and the number of distinct species, respectively.The second line of input contains $N$ space-separated integers $A_1$, $A_2$, $\\\\cdots$, $A_N$, where $A_i$ is the species of the $i$th coral in the row. ## Output Output a single line containing a single integer, the number of biodiverse subarrays of $A$. [samples] ## Scoring $1 <= K <= N <= 2 dot.op 10^5$$1 <= A_i <= K$ for all $i$.There exists an $i$ such that $A_i = k$ for all $k$ from $1$ to $K$.*Subtask 1* (40 points):$N <= 100$*Subtask 2* (20 points):$N <= 2000$*Subtask 3* (20 points):$K <= 2$*Subtask 4* (20 points):No further constraints. ## Note The below diagram shows the six biodiverse subarrays that the researchers can choose from in the first sample test case. The below diagram shows the three biodiverse subarrays that the researchers can choose from in the second sample test case.
**Definitions** Let $ N, K \in \mathbb{Z}^+ $ with $ 1 \leq K \leq N \leq 2 \cdot 10^5 $. Let $ A = (A_1, A_2, \dots, A_N) $ be a sequence of integers where $ A_i \in \{1, 2, \dots, K\} $, and each integer from $ 1 $ to $ K $ appears at least once in $ A $. A **biodiverse subarray** is a contiguous subarray $ A[l:r] = (A_l, A_{l+1}, \dots, A_r) $ with $ 1 \leq l \leq r \leq N $, such that the set $ \{A_l, A_{l+1}, \dots, A_r\} $ contains all integers from $ 1 $ to $ K $. **Objective** Compute the number of subarrays $ A[l:r] $ that are biodiverse, i.e., contain all $ K $ distinct species.
API Response (JSON)
{
  "problem": {
    "name": "B2. Biodiverse Subarray",
    "description": {
      "content": "_The problem statements of Biodiverse Subsequence and Biodiverse Subarray are nearly identical, save for whether the sampled corals must form a_ subsequence _or_ subarray _of the given sequence. Despi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10291B2"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "_The problem statements of Biodiverse Subsequence and Biodiverse Subarray are nearly identical, save for whether the sampled corals must form a_ subsequence _or_ subarray _of the given sequence. Despi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N, K \\in \\mathbb{Z}^+ $ with $ 1 \\leq K \\leq N \\leq 2 \\cdot 10^5 $.  \nLet $ A = (A_1, A_2, \\dots, A_N) $ be a sequence of integers where $ A_i \\in \\{1, 2, \\dots, K\\} $, and eac...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments