Duck, Duck, Geese

아직 제출이 없습니다시간 제한20초메모리 제한1024 MB

문제

In the game "Duck, Duck, Goose", all players but one sit on the floor and form a circle. The remaining player walks around the circle calling each player "duck" until they select one sitting player and, while touching their head, call them "goose" instead. At that point, the goose chases the selecting player and our interest in the game fades.

In the new game "Duck, Duck, Geese", the walking player instead chooses a contiguous subset of at least two (but not all) sitting players to be "geese"! Furthermore, each sitting player is wearing a hat. Each hat is one of C\mathbf{C} possible colors, numbered 11 through C\mathbf{C}.

For each color ii, the quantity of selected geese wearing a hat of color ii must be either 00 or between A_i\mathbf{A\_i} and B_i\mathbf{B\_i}, inclusive.

Can you help count the number of choices that fulfill these requirements? Two choices are considered different if there is some player that is included in one choice but not the other.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing two integers N\mathbf{N} and C\mathbf{C}: the number of sitting players and hat colors, respectively. Then, C\mathbf{C} lines follow. The ii-th of these lines contains two integers A_i\mathbf{A\_i} and B_i\mathbf{B\_i}, as explained above. The last line of a test case contains N\mathbf{N} integers P_1,P_2,,P_N\mathbf{P\_1}, \mathbf{P\_2}, \dots, \mathbf{P\_N} representing that the jj-th sitting player in clockwise order (starting from an arbitrary one) is wearing a hat of color P_j\mathbf{P\_j}.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the number of sets of at least 22 and at most N1\mathbf{N}-1 contiguously sitting players that fulfill all the color requirements.

제한

  • 1T1001 \le \mathbf{T} \le 100.
  • 2CN2 \le \mathbf{C} \le \mathbf{N}.
  • 0A_iB_iN0 \le \mathbf{A\_i} \le \mathbf{B\_i} \le \mathbf{N}, for all ii.
  • 1P_jC1 \le \mathbf{P\_j} \le \mathbf{C}, for all jj.

힌트

In Sample Case #1, the total number of players chosen as geese must be 22. There are only three possible ways to select 22 players. The following color configurations are possible: \[1,1]\[1, 1], \[1,2]\[1, 2], and \[2,1]\[2, 1]. The first one has two players wearing hats of color 11, so it is not valid, but the other two are valid. Therefore the answer is 22.

Sample Case #2 is the one illustrated in the statement, with color 11 being yellow and color 22 being blue. The total number of players chosen as geese in this case must be between 22 and 33, because selecting 44 geese would require at least one color to be out of bounds. For cases with 22 geese, the only requirement is that we do not select 22 geese both wearing hats of color 11; all 55 such selections are valid. If choosing 33 geese, the options are \[1,2,1]\[1, 2, 1], \[2,1,2]\[2, 1, 2], \[1,2,2]\[1, 2, 2], \[2,2,1]\[2, 2, 1], or \[2,1,2]\[2, 1, 2]. All but the first one are valid, adding another 44 valid options, for a total of 99.

In Sample Case #3, notice that there can be hat colors that nobody is wearing. In this case, since there is only 11 player wearing hat color 33 and 11 is not in range, the only valid way is to pick 00 players wearing that hat color.