Under construction

Still being built. Things may change

Helter Skelter

Time limit2sMemory limit128 MB

Summary
Given the run lengths of a binary string that starts with 0, answer for each query whether some substring has exactly a zeros and b ones.
Level

Hard8 of 10

Topics
Two pointers, Prefix sum, Math
Solved
No attempts yet

Problem

A non-empty string is called a binary string if it consists only of the characters '0' and '1'. A substring s[l…r]s[l \ldots r] (1≤l≤r≤∣s∣)(1 \le l \le r \le |s|) of a string s=s1s2…s∣s∣s = s_1 s_2 \ldots s_{|s|} is the string slsl+1…srs_l s_{l+1} \ldots s_r.

Professor Zhang has a long binary string ss that starts with '0'. He wants to know whether ss has a substring with exactly aa zeros and exactly bb ones, where aa and bb are two given integers.

The string is very long, so it is compressed as follows.

  • Split the string into runs of equal consecutive characters.
  • Any two adjacent runs consist of different characters. Write the length of each run to represent the string.

For example, the runs of the binary string 00101100011110111101001111111 are {00,1,0,11,000,1111,0,1111,0,1,00,1111111}\{00, 1, 0, 11, 000, 1111, 0, 1111, 0, 1, 00, 1111111\}, so it is compressed into {2,1,1,2,3,4,1,4,1,1,2,7}\{2, 1, 1, 2, 3, 4, 1, 4, 1, 1, 2, 7\}.

Input

The first line contains an integer TT, the number of test cases. For each test case, the first line contains two integers nn and mm (1≤n≤10001 \le n \le 1000, 1≤m≤5⋅1051 \le m \le 5 \cdot 10^5), the number of runs and the number of queries. The next line contains nn integers x1,x2,…,xnx_1, x_2, \ldots, x_n (1≤xi≤1061 \le x_i \le 10^6), the length of each run.

Each of the following mm lines contains two integers aia_i and bib_i (0≤ai,bi≤∣s∣0 \le a_i, b_i \le |s|, 1≤ai+bi≤∣s∣1 \le a_i + b_i \le |s|). For each query, determine whether ss has a substring with exactly aia_i zeros and exactly bib_i ones.

There are at most 200 test cases, and the total input size is at most 20 mebibytes. The sum of mm over all test cases is at most 2⋅1062 \cdot 10^6.

Output

For each test case, print a binary string of length mm. The ii-th digit is '1' if the answer to the ii-th query is yes, and '0' otherwise.

Examples1

  1. Example 1

    Input
    3
    2 3
    3 4
    3 0
    3 4
    1 2
    3 4
    1 2 3
    5 1
    4 2
    1 3
    3 2
    12 10
    2 1 1 2 3 4 1 4 1 1 2 7
    2 1
    2 2
    2 3
    2 4
    2 5
    4 1
    4 2
    4 3
    4 4
    4 5
    
    Expected output
    111
    0101
    1111101111