Concatenation with intersection
Contest: Codeforces Round 622 (Div. 2)
Difficulty rating: 2700
Tags: data structures, hashing, strings, two pointers

Vasya had three strings $$$a$$$, $$$b$$$ and $$$s$$$, which consist of lowercase English letters. The lengths of strings $$$a$$$ and $$$b$$$ are equal to $$$n$$$, the length of the string $$$s$$$ is equal to $$$m$$$.

Vasya decided to choose a substring of the string $$$a$$$, then choose a substring of the string $$$b$$$ and concatenate them. Formally, he chooses a segment $$$[l_1, r_1]$$$ ($$$1 \leq l_1 \leq r_1 \leq n$$$) and a segment $$$[l_2, r_2]$$$ ($$$1 \leq l_2 \leq r_2 \leq n$$$), and after concatenation he obtains a string $$$a[l_1, r_1] + b[l_2, r_2] = a_{l_1} a_{l_1 + 1} \ldots a_{r_1} b_{l_2} b_{l_2 + 1} \ldots b_{r_2}$$$.

Now, Vasya is interested in counting number of ways to choose those segments adhering to the following conditions:

- segments $$$[l_1, r_1]$$$ and $$$[l_2, r_2]$$$ have non-empty intersection, i.e. there exists at least one integer $$$x$$$, such that $$$l_1 \leq x \leq r_1$$$ and $$$l_2 \leq x \leq r_2$$$;
- the string $$$a[l_1, r_1] + b[l_2, r_2]$$$ is equal to the string $$$s$$$.

Input format:
The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 500\,000, 2 \leq m \leq 2 \cdot n$$$) — the length of strings $$$a$$$ and $$$b$$$ and the length of the string $$$s$$$.

The next three lines contain strings $$$a$$$, $$$b$$$ and $$$s$$$, respectively. The length of the strings $$$a$$$ and $$$b$$$ is $$$n$$$, while the length of the string $$$s$$$ is $$$m$$$.

All strings consist of lowercase English letters.

Output format:
Print one integer — the number of ways to choose a pair of segments, which satisfy Vasya's conditions.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
Let's list all the pairs of segments that Vasya could choose in the first example:

1. $$$[2, 2]$$$ and $$$[2, 5]$$$;
2. $$$[1, 2]$$$ and $$$[2, 4]$$$;
3. $$$[5, 5]$$$ and $$$[2, 5]$$$;
4. $$$[5, 6]$$$ and $$$[3, 5]$$$;

Example input:
6 5
aabbaa
baaaab
aaaaa

Example output:
4

Example input:
5 4
azaza
zazaz
azaz

Example output:
11

Editorial:
For all $$$1 \leq i \leq n$$$ let's define $$$fa_i$$$ as the length of the longest common prefix of strings $$$a[i, n]$$$ and $$$s[1, m - 1]$$$, $$$fb_i$$$ as the length of the longest common suffix of strings $$$b[1, i]$$$ and $$$s[2, m]$$$. Values of $$$fa$$$ can be easily found from $$$z$$$-function of the string "$$$s\#a$$$", values of $$$fb$$$ from $$$z$$$-function of the string "$$$\overline{s}\#\overline{b}$$$" (here $$$\overline{s}$$$ is defined as reversed string $$$s$$$).
Let's fix $$$l_1$$$ and $$$r_2$$$. Let's note, that $$$l_1 \leq r_2$$$, because segments have non-empty intersection. Also, the sum of lengths of segments is equal to $$$m$$$ and they have non-empty intersection, so $$$r_2 \leq l_1 + m - 2$$$. It's easy to see, that segments will have non-empty intersection if and only if $$$l_1 \leq r_2 \leq l_1 + m - 2$$$.
Let's note, that if for fixed $$$l_1$$$ and $$$r_2$$$ innequalities are true, the number of segments with such $$$l_1$$$ and $$$r_2$$$ is equal to $$$\max{(fa_{l_1} + fb_{r_2} - m + 1, 0)}$$$. So, the answer to the problem is equal to $$$\sum\limits_{1 \leq l_1 \leq r_2 \leq \min{(l_1 + m - 2, n)}} {\max{(fa_{l_1} + fb_{r_2} - m + 1, 0)}} = \sum\limits_{l_1 = 1}^{n} {\sum\limits_{r_2 = l_1}^{\min{(l_1 + m - 2, n)}} {\max{(fa_{l_1} + fb_{r_2} - m + 1, 0)}}}$$$.
Let's make two Fenwick trees on arrays of size $$$m$$$. Let's iterate $$$l_1$$$ from $$$1$$$ to $$$n$$$. For $$$r_2 \in [l_1, \min{(l_1 + m - 2, n)}]$$$ let's add in the position $$$m - 1 - fb_{r_2}$$$ of the first tree the number $$$1$$$, in the position $$$m - 1 - fb_{r_2}$$$ of the second tree the number $$$fb_{r_2}$$$. After that the sum for fixed $$$l_1$$$ can be easily found from sums in Fenwick trees on prefixes $$$i \leq fa_{l_1}$$$.
The total complexity is $$$O(n \log{n})$$$.