P17830 [CSP-J 2026] Card Game / card

Background

When submitting your answer on Luogu, file input/output is not needed.

Description

Little Z is playing a solitaire card game. He has a deck of special cards, $n$ in total, whose values from top to bottom are $a_1,a_2,\ldots,a_n$. The value of each card is an integer between $1$ and $m$, and every value appears at least once. Little Z will play $n$ independent rounds of the game. Before each round, he puts all cards back into the deck in their original order and clears the table. In round $i$ $(1\le i\le n)$, he skips the first $i-1$ cards and plays cards $i$ through $n$ one by one, whose values are $a_i,a_{i+1},\ldots,a_n$. The cards on the table always form a row from left to right. When playing a card with value $k$: - If there is no card with value $k$ on the table, place this card at the right end; - Otherwise, remove the card with value $k$ on the table together with all cards to its right, then place this card at the right end. The removed cards are not used again in this round. By these rules, at any moment there is at most one card of each value on the table. The round ends after the last card is played. For example, if the values of the cards on the table from left to right are $1,4,2$, then after playing a card with value $3$, the values from left to right become $1,4,2,3$. Next, when playing a card with value $4$, the original card with value $4$ on the table and all cards to its right (with values $4,2,3$) are removed, and the newly played $4$ is placed at the right end, so the values from left to right become $1,4$. You need to help Little Z find, for each $i=1,2,\ldots,m$, in how many of the $n$ rounds a card with value $i$ remains on the table when the round ends.

Input Format

Read the input from the file `card.in`. The first line of the input contains two positive integers $n,m$, denoting the number of cards and the number of distinct values, respectively. The second line contains $n$ positive integers $a_1,a_2,\ldots,a_n$, the values of Little Z's cards from top to bottom.

Output Format

Write the output to the file `card.out`. Output one line containing $m$ non-negative integers, where the $i$-th integer $(1\le i\le m)$ is the number of rounds, among all $n$ rounds, in which a card with value $i$ remains on the table when the round ends.

Explanation/Hint

### Sample 1 Explanation In round $1$, Little Z plays cards with values $1,2,1,3$ in order: 1. After playing the first card (value $1$), there is only one card with value $1$ on the table; 2. After playing the second card (value $2$), the values on the table from left to right are $1,2$; 3. After playing the third card (value $1$), the two cards with values $1,2$ are removed first, then the newly played $1$ is placed, so there is only one card with value $1$ on the table; 4. After playing the fourth card (value $3$), the values on the table from left to right are $1,3$. So when round $1$ ends, the values on the table from left to right are $1,3$. In round $2$, Little Z plays cards with values $2,1,3$ in order, and the values on the table from left to right at the end are $2,1,3$. In round $3$, Little Z plays cards with values $1,3$ in order, and the values on the table from left to right at the end are $1,3$. In round $4$, Little Z plays only one card with value $3$, and there is only one card with value $3$ on the table at the end. Therefore, among all $4$ rounds: - In $3$ rounds a card with value $1$ remains on the table at the end, namely rounds $1,2,3$; - In $1$ round a card with value $2$ remains on the table at the end, namely round $2$; - In $4$ rounds a card with value $3$ remains on the table at the end, namely all $4$ rounds. ### Sample 3 See `card/3.in` and `card/3.ans` in the problem directory. This sample satisfies the constraints of test points $4\sim 6$. ### Sample 4 See `card/4.in` and `card/4.ans` in the problem directory. This sample satisfies the constraints of test points $7\sim 9$. ### Sample 5 See `card/5.in` and `card/5.ans` in the problem directory. This sample satisfies the constraints of test points $15\sim 17$. ### Sample 6 See `card/6.in` and `card/6.ans` in the problem directory. This sample satisfies the constraints of test points $21,22$. ### Constraints For all testdata: - $2\le n\le 5\times 10^5$, $1\le m\le 3\times 10^5$; - For all $i$ $(1\le i\le n)$, $1\le a_i\le m$; - Every value appears at least once, that is, for all $i$ $(1\le i\le m)$, there exists $j$ $(1\le j\le n)$ such that $a_j=i$. ::cute-table{tuack} | Test Point | $n\le$ | $m\le$ | Special Properties | | :---: | :---: | :---: | :---: | | $1\sim 3$ | $5$ | $2$ | None | | $4\sim 6$ | $500$ | $200$ | None | | $7\sim 9$ | $5000$ | $3000$ | A | | $10\sim 12$ | $5000$ | $3000$ | None | | $13,14$ | $2\times 10^5$ | $2$ | None | | $15\sim 17$ | $2\times 10^5$ | $3000$ | B | | $18,19$ | $2\times 10^5$ | $3000$ | None | | $20$ | $2\times 10^5$ | $10^5$ | A | | $21,22$ | $2\times 10^5$ | $10^5$ | None | | $23\sim 25$ | $5\times 10^5$ | $3\times 10^5$ | None | Special property A: $a_1\le a_2\le\cdots\le a_n$. Special property B: There are at most $4999$ indices $i$ $(1\le i\le n-1)$ satisfying $a_i\ne a_{i+1}$. Translated by QwenWork