AtCoder Beginner Contest 452
G
221 子串
221 Substring
AtCoder
⌀ 2255
原题
← F→

221 子串AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

For a sequence $X = (X_1, \dots, X_n)$ of positive integers, we call $X$ a 221 sequence if and only if the length and value are equal for every (length, value) pair in its run-length encoding. More formally, a sequence satisfying the following condition is called a 221 sequence.

对于一个正整数序列 $X = (X_1, \dots, X_n)$,当且仅当它的游程编码中每个(长度,数值)对的长度与数值相等时,我们称 $X$ 是一个 221 序列。更形式化地说,满足以下条件的序列称为 221 序列。AI (CI翻译)

  • For any integer pair $(l, r)$ satisfying $1 \leq l \leq r \leq n$, if all three of the following conditions hold, then $(r-l+1) = X_l$.
  • $l = 1$ or ($l \geq 2$ and $X_{l-1} \neq X_l$)
    • $r = n$ or ($r \leq n-1$ and $X_{r+1} \neq X_r$)
    • $X_l = X_{l+1} = \dots = X_r$
  • 对于任意满足 $1 \leq l \leq r \leq n$ 的整数对 $(l, r)$,如果以下三个条件都成立,那么 $(r-l+1) = X_l$。
  • $l = 1$,或者($l \geq 2$ 且 $X_{l-1} \neq X_l$)
    • $r = n$,或者($r \leq n-1$ 且 $X_{r+1} \neq X_r$)
    • $X_l = X_{l+1} = \dots = X_r$
AI (CI翻译)

For example, $(2,2,3,3,3,1,2,2)$ is a 221 sequence, but $(1,1)$ and $(4,4,1,4,4)$ are not 221 sequences.

例如,$(2,2,3,3,3,1,2,2)$ 是一个 221 序列,但 $(1,1)$ 和 $(4,4,1,4,4)$ 不是 221 序列。AI (CI翻译)

You are given a sequence $A = (A_1, \dots, A_N)$ of positive integers of length $N$. Find the number of distinct 221 sequences that appear as a non-empty contiguous subsequence of $A$.

给定一个长度为 $N$ 的正整数序列 $A = (A_1, \dots, A_N)$。请找出 $A$ 的所有非空连续子序列中,不同的 221 序列的数量。AI (CI翻译)

Even if two subsequences are extracted from different positions, they are counted as one if they are equal as sequences.

即使两个子序列取自不同的位置,只要它们作为序列相等,就只计为一个。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq N \leq 500\,000$
  • $1 \leq A_i \leq 9$
  • All input values are integers.
  • $1 \leq N \leq 500\,000$
  • $1 \leq A_i \leq 9$
  • 输入的所有数值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入按以下格式给出:AI (CI翻译)

N
A_1 A_2 \cdots A_N

N
A_1 A_2 \cdots A_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answer on a single line.

在一行中输出答案。AI (CI翻译)

样例

Input #1
23
2 2 3 3 3 1 1 1 3 3 3 1 2 2 2 1 9 1 4 4 4 4 4
Output #1
14
The 221 sequences that appear as a non-empty contiguous subsequence of $A$ are the following $14$: - $(1)$ - $(1,2,2)$ - $(1,3,3,3)$ - $(1,3,3,3,1)$ - $(1,3,3,3,1,2,2)$ - $(1,4,4,4,4)$ - $(2,2)$ - $(2,2,1)$ - $(2,2,3,3,3)$ - $(2,2,3,3,3,1)$ - $(3,3,3)$ - $(3,3,3,1)$ - $(3,3,3,1,2,2)$ - $(4,4,4,4)$
Input #2
2
6 7
Output #2
0
No 221 sequences appear as a non-empty contiguous subsequence of $A$.
题目信息
题号
abc452_g
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。