#abc294e. E - 2xN Grid

E - 2xN Grid

Score : 500500 points

问题描述

我们有一个包含2行和LL列的网格。令(i,j)(i,j)表示从上数第ii行(其中i{1,2}i\in\lbrace1,2\rbrace)且从左数第jj列(1jL1\leq j\leq L)的方格。在(i,j)(i,j)上写有一个整数xi,jx _ {i,j}

找出满足x1,j=x2,jx _ {1,j}=x _ {2,j}的整数jj的数量。

这里,对xi,jx _ {i,j}的描述以运行长度压缩的形式给出:分别将(x1,1,x1,2,,x1,L)(x _ {1,1},x _ {1,2},\ldots,x _ {1,L})(x2,1,x2,2,,x2,L)(x _ {2,1},x _ {2,2},\ldots,x _ {2,L})压缩为长度分别为N1N _ 1N2N _ 2的序列:$((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1}))$和$((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2}))$。

这里,序列AA的运行长度压缩是指通过以下方式获得的一系列元素值viv _ i和正整数lil _ i构成的对(vi,li)(v _ i,l _ i)序列。

  1. 在每对相邻不同元素之间分割AA
  2. 对于分割后的每个子序列B1,B2,,BkB _ 1,B _ 2,\ldots,B _ k,令viv _ iBiB _ i中的元素,lil _ iBiB _ i的长度。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a grid with 22 rows and LL columns. Let (i,j)(i,j) denote the square at the ii-th row from the top (i{1,2})(i\in\lbrace1,2\rbrace) and jj-th column from the left (1jL)(1\leq j\leq L). (i,j)(i,j) has an integer xi,jx _ {i,j} written on it.

Find the number of integers jj such that x1,j=x2,jx _ {1,j}=x _ {2,j}.

Here, the description of xi,jx _ {i,j} is given to you as the run-length compressions of (x1,1,x1,2,,x1,L)(x _ {1,1},x _ {1,2},\ldots,x _ {1,L}) and (x2,1,x2,2,,x2,L)(x _ {2,1},x _ {2,2},\ldots,x _ {2,L}) into sequences of lengths N1N _ 1 and N2N _ 2, respectively: $((v _ {1,1},l _ {1,1}),\ldots,(v _ {1,N _ 1},l _ {1,N _ 1}))$ and $((v _ {2,1},l _ {2,1}),\ldots,(v _ {2,N _ 2},l _ {2,N _ 2}))$.

Here, the run-length compression of a sequence AA is a sequence of pairs (vi,li)(v _ i,l _ i) of an element viv _ i of AA and a positive integer lil _ i obtained as follows.

  1. Split AA between each pair of different adjacent elements.
  2. For each sequence B1,B2,,BkB _ 1,B _ 2,\ldots,B _ k after the split, let viv _ i be the element of BiB _ i and lil _ i be the length of BiB _ i.

Constraints

  • 1L10121\leq L\leq 10 ^ {12}
  • 1N1,N21051\leq N _ 1,N _ 2\leq 10 ^ 5
  • $1\leq v _ {i,j}\leq 10 ^ 9\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)$
  • $1\leq l _ {i,j}\leq L\ (i\in\lbrace1,2\rbrace,1\leq j\leq N _ i)$
  • $v _ {i,j}\neq v _ {i,j+1}\ (i\in\lbrace1,2\rbrace,1\leq j\lt N _ i)$
  • $l _ {i,1}+l _ {i,2}+\cdots+l _ {i,N _ i}=L\ (i\in\lbrace1,2\rbrace)$
  • All values in the input are integers.

Input

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

LL N1N _ 1 N2N _ 2

v1,1v _ {1,1} l1,1l _ {1,1}

v1,2v _ {1,2} l1,2l _ {1,2}

\vdots

v1,N1v _ {1,N _ 1} l1,N1l _ {1,N _ 1}

v2,1v _ {2,1} l2,1l _ {2,1}

v2,2v _ {2,2} l2,2l _ {2,2}

\vdots

v2,N2v _ {2,N _ 2} l2,N2l _ {2,N _ 2}

Output

Print a single line containing the answer.

Sample Input 1

8 4 3
1 2
3 2
2 3
3 1
1 4
2 1
3 3

Sample Output 1

4

The grid is shown below.

We have four integers jj such that x1,j=x2,jx _ {1,j}=x _ {2,j}: j=1,2,5,8j=1,2,5,8. Thus, you should print 44.

Sample Input 2

10000000000 1 1
1 10000000000
1 10000000000

Sample Output 2

10000000000

Note that the answer may not fit into a 3232-bit integer.

Sample Input 3

1000 4 7
19 79
33 463
19 178
33 280
19 255
33 92
34 25
19 96
12 11
19 490
33 31

Sample Output 3

380

update @ 2024/3/10 12:14:42