CF1849C.Binary String Copying

传统题 时间 2000 ms 内存 256 MiB 8 尝试 10 已通过 2 标签

Binary String Copying

You are given a string ss consisting of nn characters 0 and/or 1.

You make mm copies of this string, let the ii-th copy be the string tit_i. Then you perform exactly one operation on each of the copies: for the ii-th copy, you sort its substring [li;ri][l_i; r_i] (the substring from the lil_i-th character to the rir_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 t1,t2,,tmt_1, t_2, \ldots, t_m. Note that the initial string ss should be counted only if at least one of the copies stays the same after the operation.

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of test cases.

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

The second line contains nn characters 0 and/or 1 — the string ss.

Then mm lines follow. The ii-th of them contains two integers lil_i and rir_i (1lirin1 \le l_i \le r_i \le n) — the description of the operation applied to the ii-th copy.

The sum of nn over all test cases doesn't exceed 21052 \cdot 10^5. The sum of mm over all test cases doesn't exceed 21052 \cdot 10^5.

Output

Print one integer — the number of different strings among t1,t2,,tmt_1, t_2, \ldots, t_m.

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 t1,t2,t3,t4,t5t_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 t1,t2,t3,t4t_1, t_2, t_3, t_4: 001111, 010111 and 100111.

Samples

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
3
3
1

在线编程 IDE

建议全屏模式获得最佳体验