Sky株式会社プログラミングコンテスト2026(AtCoder Beginner Contest 455)
G
平衡子数组
Balanced Subarrays
AtCoder
⌀ 3092
原题
← D→

平衡子数组AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a sequence $A$ of $N$ positive integers. Among the $\frac{N(N+1)}{2}$ non-empty (contiguous) subarrays of $A$, we call a sequence $X$ a balanced sequence if and only if it satisfies the following condition:

给定一个由 $N$ 个正整数组成的序列 $A$。 在 $A$ 的 $\frac{N(N+1)}{2}$ 个非空连续子数组中,如果一个序列 $X$ 满足以下条件,则称其为平衡序列:AI (CI翻译)

  • Every integer appearing in $X$ appears the same number of times in $X$.
  • $X$ 中出现的每个整数出现次数都相同。
AI (CI翻译)

For each $k=1,2,\dots,K$, find the following two values:

对于每个 $k=1,2,\dots,K$,求以下两个值:AI (CI翻译)

  • The number of balanced sequences in which each integer appears exactly $B_k$ times.
  • The number of balanced sequences in which exactly $B_k$ distinct integers appear.
  • 每个整数恰好出现 $B_k$ 次的平衡序列数量。
  • 恰好出现 $B_k$ 个不同整数的平衡序列数量。
AI (CI翻译)

Count two subarrays separately if they are taken from different positions in $A$, even if they are identical as sequences.

即使两个子数组作为序列完全相同,只要它们在 $A$ 中的起始位置不同,也要将它们分别计数。AI (CI翻译)

Solve $T$ test cases per input.

请解决输入中的 $T$ 组测试用例。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \leq T \leq 2 \times 10^5$
  • $1 \leq N \leq 2 \times 10^5$
  • $1 \leq K \leq \min(N,10)$
  • $1 \leq A_i \leq N$
  • $1 \leq B_k \leq N$
  • $B_1,B_2,\dots,B_K$ are pairwise distinct.
  • The sum of $N$ over all test cases is at most $2 \times 10^5$.
  • All input values are integers.
  • $1 \leq T \leq 2 \times 10^5$
  • $1 \leq N \leq 2 \times 10^5$
  • $1 \leq K \leq \min(N,10)$
  • $1 \leq A_i \leq N$
  • $1 \leq B_k \leq N$
  • $B_1,B_2,\dots,B_K$ 两两不同。
  • 所有测试用例的 $N$ 之和不超过 $2 \times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

输入从标准输入中读取,格式如下:AI (CI翻译)

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

Each case is given in the following format:

每组测试用例的格式如下:AI (CI翻译)

N K
A_1 A_2 \dots A_N
B_1 B_2 \dots B_K

N K
A_1 A_2 \dots A_N
B_1 B_2 \dots B_K
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answers in the following format:

请按以下格式输出答案:AI (CI翻译)

\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
AI (CI翻译)

For each case, letting $c_{k,1}$ be the number of balanced sequences in which each integer appears exactly $B_k$ times, and $c_{k,2}$ be the number of balanced sequences in which exactly $B_k$ distinct integers appear, output in the following format:

对于每组测试用例,令 $c_{k,1}$ 表示每个整数恰好出现 $B_k$ 次的平衡序列数量,令 $c_{k,2}$ 表示恰好出现 $B_k$ 个不同整数的平衡序列数量,请按以下格式输出:AI (CI翻译)

c_{1,1} c_{1,2}
c_{2,1} c_{2,2}
\vdots
c_{K,1} c_{K,2}

c_{1,1} c_{1,2}
c_{2,1} c_{2,2}
\vdots
c_{K,1} c_{K,2}
AI (CI翻译)

样例

Input #1
3
4 2
1 2 1 2
2 1
1 1
1
1
7 7
1 5 5 1 5 1 2
1 2 3 4 5 6 7
Output #1
1 4
7 4
1 1
13 8
3 8
1 1
0 0
0 0
0 0
0 0
For the first test case, the pairs $(l,r)$ such that the subarray of $A$ from the $l$-th through the $r$-th element is a balanced sequence are $(1,1),(1,2),(1,4),(2,2),(2,3),(3,3),(3,4),(4,4)$, giving eight instances in total. Among these, the ones in which each integer appears exactly twice are $(1,4)$, giving one instance, and the ones in which exactly two distinct integers appear are $(1,2),(1,4),(2,3),(3,4)$, giving four instances. Thus, for $k=1$, output `1 4`. Also, the ones in which each integer appears exactly once are $(1,1),(1,2),(2,2),(2,3),(3,3),(3,4),(4,4)$, giving seven instances, and the ones in which exactly one distinct integer appears are $(1,1),(2,2),(3,3),(4,4)$, giving four instances. Thus, for $k=2$, output `7 4`.
题目信息

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