2019B.md (2505B)
1 Source: https://codeforces.com/problemset/problem/2019/B 2 3 B. All Pairs Segments 4 5 time limit per test 6 7 1.5 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 [Shirobon - FOX](https://soundcloud.com/shirobon/fox?in=mart_207/sets/fav) 22 23 ⠀ 24 25 You are given $n$ points on the $x$ axis, at increasing positive integer coordinates $x_1 \lt x_2 \lt \ldots \lt x_n$. 26 27 For each pair $(i, j)$ with $1 \leq i \lt j \leq n$, you draw the segment $[x_i, x_j]$. The segments are closed, i.e., a segment $[a, b]$ contains the points $a, a+1, \ldots, b$. 28 29 You are given $q$ queries. In the $i$-th query, you are given a positive integer $k_i$, and you have to determine how many points with integer coordinates are contained in exactly $k_i$ segments. 30 31 Input 32 33 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. 34 35 The first line of each test case contains two integers $n$, $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$) — the number of points and the number of queries. 36 37 The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 \lt x_2 \lt \ldots \lt x_n \leq 10^9$) — the coordinates of the $n$ points. 38 39 The third line of each test case contains $q$ integers $k_1, k_2, \ldots, k_q$ ($1 \leq k_i \leq 10^{18}$) — the parameters of the $q$ queries. 40 41 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$. 42 43 Output 44 45 For each test case, output a single line with $q$ integers: the $i$-th integer is the answer to the $i$-th query. 46 47 Example 48 49 Input 50 51 ``` 52 3 53 54 2 2 55 56 101 200 57 58 2 1 59 60 6 15 61 62 1 2 3 5 6 7 63 64 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 65 66 5 8 67 68 254618033 265675151 461318786 557391198 848083778 69 70 6 9 15 10 6 9 4 4294967300 71 ``` 72 73 Output 74 75 ``` 76 0 100 77 0 0 0 0 2 0 0 0 3 0 2 0 0 0 0 78 291716045 0 0 0 291716045 0 301749698 0 79 ``` 80 81 Note 82 83 In the first example, you only draw the segment $[101, 200]$. No point is contained in exactly $2$ segments, and the $100$ points $101, 102, \ldots, 200$ are contained in exactly $1$ segment. 84 85 In the second example, you draw $15$ segments: $[1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [2, 3], [2, 5], [2, 6], [2, 7], [3, 5], [3, 6], [3, 7], [5, 6], [5, 7], [6, 7]$. Points $1, 7$ are contained in exactly $5$ segments; points $2, 4, 6$ are contained in exactly $9$ segments; points $3, 5$ are contained in exactly $11$ segments.