Sequence and Queries 9

For each query range [i,j] and value k, count ordered pairs (p,q) from that range with A[p]*B[q] <= k.

Hard9Divide and conquerSegment treeSortingDynamic programmingNo attempts yetTime limit6sMemory limit512 MB

Problem

Two sequences of length NN are given: A1,A2,,ANA_1, A_2, \dots, A_N and B1,B2,,BNB_1, B_2, \dots, B_N. Write a program that processes the following query.

  • i j k: print the number of pairs (p,q)(p, q) such that ipji \le p \le j, iqji \le q \le j, and Ap×BqkA_p \times B_q \le k.

The indices pp and qq are chosen independently, so a pair with p=qp = q also counts.

Input

The first line contains the size of the sequences, NN (1N1000001 \le N \le 100000).

The second line contains A1,A2,,ANA_1, A_2, \dots, A_N. (1Ai1000001 \le A_i \le 100000)

The third line contains B1,B2,,BNB_1, B_2, \dots, B_N. (1Bi1000001 \le B_i \le 100000)

The fourth line contains the number of queries, MM (1M1000001 \le M \le 100000).

Each of the next MM lines contains one query ii, jj, kk. (1ijN1 \le i \le j \le N, 1k1000001 \le k \le 100000)

Output

For each query, print the answer on its own line, in the order the queries are given.