Under construction

Still being built. Things may change

Huge Sequences

Time limit1sMemory limit2048 MB

Summary
Given three arrays, answer queries asking for the sum over all subintervals of a range of the product of AND of a, OR of b, and GCD of c, modulo 2^32.
Level

Hard10 of 10

Topics
Divide and conquer, Bit manipulation, Number theory, Prefix sum
Solved
No attempts yet

Problem

You are given three sequences a1,…,ana_1, \ldots, a_n, b1,…,bnb_1, \ldots, b_n, and c1,…,cnc_1, \ldots, c_n. The value of an interval [ℓ,r][\ell, r] is the product of three factors: the bitwise AND of aℓ,…,ara_\ell, \ldots, a_r, the bitwise OR of bℓ,…,brb_\ell, \ldots, b_r, and the greatest common divisor of cℓ,…,crc_\ell, \ldots, c_r.

There are mm queries. Each query gives an interval [ℓ,r][\ell, r] and asks for the sum of the values of all intervals [ℓ′,r′][\ell', r'] with ℓ≤ℓ′≤r′≤r\ell \le \ell' \le r' \le r. Report each answer modulo 2322^{32}.

Input

The first line contains two integers nn and mm (1≤n≤1061 \le n \le 10^6, 1≤m≤5⋅1061 \le m \le 5 \cdot 10^6).

The second line contains nn integers a1,…,ana_1, \ldots, a_n.

The third line contains nn integers b1,…,bnb_1, \ldots, b_n.

The fourth line contains nn integers c1,…,cnc_1, \ldots, c_n. Every aia_i, bib_i, and cic_i satisfies 1≤ai,bi,ci≤n1 \le a_i, b_i, c_i \le n.

Each of the following mm lines contains two integers ℓ\ell and rr (1≤ℓ≤r≤n1 \le \ell \le r \le n), describing one query.

Output

Print mm lines. The ii-th line contains the answer to the ii-th query modulo 2322^{32}.

Examples2

  1. Example 1

    Input
    5 3
    3 3 1 1 1
    2 1 3 2 2
    4 5 3 4 4
    1 2
    2 5
    4 5
    
    Expected output
    48
    63
    24
    
  2. Example 2

    Input
    1 1
    1
    1
    1
    1 1
    
    Expected output
    1