#P3560. [POI 2013] LAN-Colorful Chain

[POI 2013] LAN-Colorful Chain

Description

Little Bytie loves to play with colorful chains. Each chain consists of a certain number of colorful links. Bytie finds a contiguous fragment of a chain nice if it contains exactly lil_i links of color cic_i for every ii in [1,m][1, m], and moreover contains no links of any other colors. The appeal of a chain is the number of its contiguous fragments that are nice.

Given a sequence of length nn representing the colors of the chain and mm conditions (each condition gives a color cic_i and a required count lil_i), count how many contiguous substrings are nice, that is:

  • For every cic_i among the mm conditions, the substring contains exactly lil_i occurrences of cic_i.
  • For any color not listed among the mm conditions, the substring contains none of it.

Input Format

  • The first line contains two integers nn and mm (1mn1061 \le m \le n \le 10^6). These are the length of the chain and the number of conditions.
  • The second line contains mm integers l1,l2,,lml_1, l_2, \dots, l_m (1lin1 \le l_i \le n).
  • The third line contains mm integers c1,c2,,cmc_1, c_2, \dots, c_m (1cin1 \le c_i \le n). These define that a nice fragment must contain exactly lil_i links of color cic_i for each ii and no links of any other colors.
  • The fourth line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n), the colors of successive links of the chain.

Output Format

Print a single integer, the number of nice contiguous fragments in the chain.

7 3
2 1 1
1 2 3
4 2 1 3 1 2 5

2

Hint

Constraints:

For 100%100\% of the testdata, 1mn1061 \le m \le n \le 10^6 and 1ai,li,cin1 \le a_i, l_i, c_i \le n.

Translated by ChatGPT 5