This page is still under construction.

Parts of this page are still being built. What you see may change.

Three Arrays

Time limit2sMemory limit256 MB

Summary
Given three sorted arrays and a distance d, count triples of elements (one from each array) whose pairwise differences all stay within d.
Level

Medium6 of 10

Topics
Two pointers, Sorting, Brute force, Array
Solved
No attempts yet

Problem

You are given three arrays: aa with nan_a elements, bb with nbn_b elements, and cc with ncn_c elements. All three arrays are sorted in non-decreasing order. That is, ai≤ai+1a_i \le a_{i + 1} for every ii with 1≤i<na1 \le i < n_a, bj≤bj+1b_j \le b_{j + 1} for every jj with 1≤j<nb1 \le j < n_b, and ck≤ck+1c_k \le c_{k + 1} for every kk with 1≤k<nc1 \le k < n_c.

Count the triples (i,j,k)(i, j, k) that satisfy ∣ai−bj∣≤d|a_i - b_j| \le d, ∣ai−ck∣≤d|a_i - c_k| \le d, and ∣bj−ck∣≤d|b_j - c_k| \le d.

Input

The input contains one or more test cases. Each test case consists of four lines.

The first line of each test case contains four integers: dd, nan_a, nbn_b, and ncn_c (1≤d≤1091 \le d \le 10^9, 1≤na,nb,nc≤5⋅1051 \le n_a, n_b, n_c \le 5 \cdot 10^5).

The second line contains nan_a integers a1,a2,…,anaa_1, a_2, \ldots, a_{n_a}: the array aa (−109≤ai≤109-10^9 \le a_i \le 10^9).

The third line contains nbn_b integers b1,b2,…,bnbb_1, b_2, \ldots, b_{n_b}: the array bb (−109≤bi≤109-10^9 \le b_i \le 10^9).

The fourth line contains ncn_c integers c1,c2,…,cncc_1, c_2, \ldots, c_{n_c}: the array cc (−109≤ci≤109-10^9 \le c_i \le 10^9).

All arrays are sorted in non-decreasing order. The total sum of nan_a over all test cases does not exceed 5⋅1055 \cdot 10^5. The total sum of nbn_b over all test cases does not exceed 5⋅1055 \cdot 10^5. The total sum of ncn_c over all test cases does not exceed 5⋅1055 \cdot 10^5. The test cases follow one another without any special separators.

Output

For each test case, print a single integer: the number of triples (i,j,k)(i, j, k) such that ∣ai−bj∣≤d|a_i - b_j| \le d, ∣ai−ck∣≤d|a_i - c_k| \le d, and ∣bj−ck∣≤d|b_j - c_k| \le d.

Examples1

  1. Example 1

    Input
    1 3 3 3
    1 2 3
    1 2 3
    1 2 3
    1 6 6 6
    1 1 2 2 3 3
    2 2 3 3 4 4
    3 3 4 4 5 5
    
    Expected output
    15
    56