Under construction

Still being built. Things may change

Sequence and Queries 9

Time limit6sMemory limit512 MB

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

Hard9 of 10

Topics
Divide and conquer, Segment tree, Sorting, Dynamic programming
Solved
No attempts yet

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 i≤p≤ji \le p \le j, i≤q≤ji \le q \le j, and Ap×Bq≤kA_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 (1≤N≤1000001 \le N \le 100000).

The second line contains A1,A2,…,ANA_1, A_2, \dots, A_N. (1≤Ai≤1000001 \le A_i \le 100000)

The third line contains B1,B2,…,BNB_1, B_2, \dots, B_N. (1≤Bi≤1000001 \le B_i \le 100000)

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

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

Output

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

Examples4

  1. Example 1

    Input
    5
    1 1 1 1 1
    1 1 1 1 1
    1
    1 5 1
    
    Expected output
    25
    
  2. Example 2

    Input
    4
    1 2 3 4
    4 3 2 1
    5
    1 4 4
    1 4 1
    2 3 6
    1 1 100000
    4 4 3
    
    Expected output
    8
    1
    3
    1
    0
    
  3. Example 3

    Input
    1
    100000
    1
    3
    1 1 100000
    1 1 99999
    1 1 1
    
    Expected output
    1
    0
    0
    
  4. Example 4

    Input
    3
    100000 100000 1
    100000 1 100000
    4
    1 3 100000
    1 3 99999
    1 2 100000
    2 3 100000
    
    Expected output
    5
    1
    2
    3