Binary String Copying
Contest: Educational Codeforces Round 152 (Rated for Div. 2)
Difficulty rating: 1600
Tags: binary search, brute force, data structures, hashing, strings

You are given a string $$$s$$$ consisting of $$$n$$$ characters 0 and/or 1.

You make $$$m$$$ copies of this string, let the $$$i$$$-th copy be the string $$$t_i$$$. Then you perform exactly one operation on each of the copies: for the $$$i$$$-th copy, you sort its substring $$$[l_i; r_i]$$$ (the substring from the $$$l_i$$$-th character to the $$$r_i$$$-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation.

Your task is to calculate the number of different strings among $$$t_1, t_2, \ldots, t_m$$$. Note that the initial string $$$s$$$ should be counted only if at least one of the copies stays the same after the operation.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the length of $$$s$$$ and the number of copies, respectively.

The second line contains $$$n$$$ characters 0 and/or 1 — the string $$$s$$$.

Then $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the description of the operation applied to the $$$i$$$-th copy.

The sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$. The sum of $$$m$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output format:
Print one integer — the number of different strings among $$$t_1, t_2, \ldots, t_m$$$.

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

Note:
Consider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted:

1. 101100 $$$\rightarrow$$$ 011100;
2. 101100 $$$\rightarrow$$$ 011100;
3. 101100 $$$\rightarrow$$$ 101100;
4. 101100 $$$\rightarrow$$$ 101100;
5. 101100 $$$\rightarrow$$$ 000111.

There are three different strings among $$$t_1, t_2, t_3, t_4, t_5$$$: 000111, 011100 and 101100.

Consider the second example:

1. 100111 $$$\rightarrow$$$ 100111;
2. 100111 $$$\rightarrow$$$ 001111;
3. 100111 $$$\rightarrow$$$ 001111;
4. 100111 $$$\rightarrow$$$ 010111.

There are three different strings among $$$t_1, t_2, t_3, t_4$$$: 001111, 010111 and 100111.

Example input:
3
6 5
101100
1 2
1 3
2 4
5 5
1 6
6 4
100111
2 2
1 4
1 3
1 2
1 1
0
1 1

Example output:
3
3
1

Editorial:
We can see that each modified copy is determined by only two integers $$$lb$$$ and $$$rb$$$ — the first position at which the character has changed and the last such position. If we can find such numbers for each of the copies, the number of different pairs will be the answer to the problem.
Let $$$lf_i$$$ be the position of the nearest character 0 at the position $$$i$$$ or to the left of it, and $$$rg_i$$$ be the position of the nearest character 1 at the position $$$i$$$ or to the right of it. If the first character of the string $$$s$$$ is 1 then $$$lf_0 = -1$$$, otherwise $$$lf_0 = 0$$$. And if the last character of the string $$$s$$$ is 0, then $$$rg_{n - 1} = n$$$, otherwise $$$rg_{n - 1} = n - 1$$$. The values $$$lf$$$ and $$$rg$$$ can be calculated using simple dynamic programming, $$$lf$$$ is calculated from left to right, and $$$rg$$$ — from right to left. Then the numbers $$$lb$$$ and $$$rb$$$ we need are equal to $$$rg_l$$$ and $$$lf_r$$$, respectively. If $$$rg_l > lf_r$$$, then the changed segment is degenerate (and this means that the string does not change at all). We can define some special segment for this type of strings, for example, $$$(-1, -1)$$$. Otherwise, the segment $$$(rg_l, lf_r)$$$ of the string will change.
Time complexity: $$$O(n \log{n})$$$.