#P3560. Count Nice Subchains
Count Nice Subchains
Count Nice Subchains
Little Bytie loves to play with colorful chains. He considers a contiguous fragment of a chain nice if it contains exactly the required number of links for some specific colors and no links of any other colors.
You are given an integer sequence \(a_1, a_2,\dots,a_n\) of length \(n\) and \(m\) conditions. Each condition consists of a color \(c_i\) and a required count \(l_i\). A contiguous subarray (segment) of \(a\) is called nice if for every condition (color \(c_i\)) the color \(c_i\) appears exactly \(l_i\) times, and no other colors appear in that segment.
Your task is to count the number of nice fragments.
Note: Since the segments cannot contain any extra colors and the counts for condition colors are fixed, the length \(L\) of every nice subarray is \(L = \sum_{i=1}^{m} l_i\).
inputFormat
The input is given in the following order:
- The first line contains two integers \(n\) and \(m\), the length of the sequence and the number of conditions respectively.
- The second line contains \(m\) integers: \(l_1, l_2, \dots, l_m\), where each \(l_i\) is the required count for color \(c_i\).
- The third line contains \(m\) integers: \(c_1, c_2, \dots, c_m\), representing the colors for which the conditions are applied.
- The fourth line contains \(n\) integers: \(a_1, a_2, \dots, a_n\), the colors of the links in the chain.
It is guaranteed that all integers are positive.
outputFormat
Output a single integer representing the number of nice subchains (contiguous subarrays) that meet the conditions.
sample
5 2
1 2
1 2
1 2 2 1 2
3