Codeforces Round 689 (Div. 2, based on Zed Code Competition) D. Divide and Summarize

Divide and Summarize

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Mike received an array a a a of length n n n as a birthday present and decided to test how pretty it is.

An array would pass the i i i-th prettiness test if there is a way to get an array with a sum of elements totaling s i s_i si, using some number (possibly zero) of slicing operations.

An array slicing operation is conducted in the following way:

  • assume m i d = ⌊ m a x ( a r r a y ) + m i n ( a r r a y ) 2 ⌋ mid = \lfloor\frac{max(array) + min(array)}{2}\rfloor mid=2max(array)+min(array), where m a x max max and m i n min min — are functions that find the maximum and the minimum array elements. In other words, m i d mid mid is the sum of the maximum and the minimum element of a r r a y array array divided by 2 2 2 rounded down.
  • Then the array is split into two parts l e f t \mathit{left} left and r i g h t right right. The l e f t \mathit{left} left array contains all elements which are less than or equal m i d mid mid, and the r i g h t right right array contains all elements which are greater than m i d mid mid. Elements in l e f t \mathit{left} left and r i g h t right right keep their relative order from a r r a y array array.
  • During the third step we choose which of the l e f t \mathit{left} left and r i g h t right right arrays we want to keep. The chosen array replaces the current one and the other is permanently discarded.

You need to help Mike find out the results of q q q prettiness tests.

Note that you test the prettiness of the array a a a, so you start each prettiness test with the primordial (initial) array a a a. Thus, the first slice (if required) is always performed on the array a a a.

Input

Each test contains one or more test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100).

The first line of each test case contains two integers n n n and q q q ( 1 ≤ n , q ≤ 1 0 5 ) (1 \le n, q \le 10^5) (1n,q105) — the length of the array a a a and the total number of prettiness tests.

The second line of each test case contains n n n integers a 1 , a 2 , . . . , a n a_1, a_2, ..., a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 6 ) (1 \le a_i \le 10^6) (1ai106) — the contents of the array a a a.

Next q q q lines of each test case contain a single integer s i s_i si ( 1 ≤ s i ≤ 1 0 9 ) (1 \le s_i \le 10^9) (1si109) — the sum of elements which Mike wants to get in the i i i-th test.

It is guaranteed that the sum of n n n and the sum of q q q does not exceed 1 0 5 10^5 105 ( ∑ n , ∑ q ≤ 1 0 5 \sum n, \sum q \le 10^5 n,q105).

Output

Print q q q lines, each containing either a “Yes” if the corresponding prettiness test is passed and “No” in the opposite case.

Example

i n p u t \tt input input
2
5 5
1 2 3 4 5
1
8
9
12
6
5 5
3 1 3 1 3
1
2
3
9
11
o u t p u t \tt output output
Yes
No
Yes
No
Yes
No
Yes
No
Yes
Yes

Note

Explanation of the first test case:

  1. We can get an array with the sum s 1 = 1 s_1 = 1 s1=1 in the following way:

    1.1 a = [ 1 , 2 , 3 , 4 , 5 ] a = [1, 2, 3, 4, 5] a=[1,2,3,4,5], m i d = 1 + 5 2 = 3 mid = \frac{1+5}{2} = 3 mid=21+5=3, l e f t = [ 1 , 2 , 3 ] \mathit{left} = [1, 2, 3] left=[1,2,3], r i g h t = [ 4 , 5 ] right = [4, 5] right=[4,5]. We choose to keep the l e f t \mathit{left} left array.

    1.2 a = [ 1 , 2 , 3 ] a = [1, 2, 3] a=[1,2,3], m i d = 1 + 3 2 = 2 mid = \frac{1+3}{2} = 2 mid=21+3=2, l e f t = [ 1 , 2 ] \mathit{left} = [1, 2] left=[1,2], r i g h t = [ 3 ] right = [3] right=[3]. We choose to keep the l e f t \mathit{left} left array.

    1.3 a = [ 1 , 2 ] a = [1, 2] a=[1,2], m i d = 1 + 2 2 = 1 mid = \frac{1+2}{2} = 1 mid=21+2=1, l e f t = [ 1 ] \mathit{left} = [1] left=[1], r i g h t = [ 2 ] right = [2] right=[2]. We choose to keep the l e f t \mathit{left} left array with the sum equalling 1 1 1.

  2. It can be demonstrated that an array with the sum s 2 = 8 s_2 = 8 s2=8 is impossible to generate.

  3. An array with the sum s 3 = 9 s_3 = 9 s3=9 can be generated in the following way:

    3.1 a = [ 1 , 2 , 3 , 4 , 5 ] a = [1, 2, 3, 4, 5] a=[1,2,3,4,5], m i d = 1 + 5 2 = 3 mid = \frac{1+5}{2} = 3 mid=21+5=3, l e f t = [ 1 , 2 , 3 ] \mathit{left} = [1, 2, 3] left=[1,2,3], r i g h t = [ 4 , 5 ] right = [4, 5] right=[4,5]. We choose to keep the r i g h t right right array with the sum equalling 9 9 9.

  4. It can be demonstrated that an array with the sum s 4 = 12 s_4 = 12 s4=12 is impossible to generate.

  5. We can get an array with the sum s 5 = 6 s_5 = 6 s5=6 in the following way:

    5.1 a = [ 1 , 2 , 3 , 4 , 5 ] a = [1, 2, 3, 4, 5] a=[1,2,3,4,5], m i d = 1 + 5 2 = 3 mid = \frac{1+5}{2} = 3 mid=21+5=3, l e f t = [ 1 , 2 , 3 ] \mathit{left} = [1, 2, 3] left=[1,2,3], r i g h t = [ 4 , 5 ] right = [4, 5] right=[4,5]. We choose to keep the l e f t \mathit{left} left with the sum equalling 6 6 6.

Explanation of the second test case:

  1. It can be demonstrated that an array with the sum s 1 = 1 s_1 = 1 s1=1 is imposssible to generate.

  2. We can get an array with the sum s 2 = 2 s_2 = 2 s2=2 in the following way:

    2.1 a = [ 3 , 1 , 3 , 1 , 3 ] a = [3, 1, 3, 1, 3] a=[3,1,3,1,3], m i d = 1 + 3 2 = 2 mid = \frac{1+3}{2} = 2 mid=21+3=2, l e f t = [ 1 , 1 ] \mathit{left} = [1, 1] left=[1,1], r i g h t = [ 3 , 3 , 3 ] right = [3, 3, 3] right=[3,3,3]. We choose to keep the l e f t \mathit{left} left array with the sum equalling 2 2 2.

  3. It can be demonstrated that an array with the sum s 3 = 3 s_3 = 3 s3=3 is imposssible to generate.

  4. We can get an array with the sum s 4 = 9 s_4 = 9 s4=9 in the following way:

    4.1 a = [ 3 , 1 , 3 , 1 , 3 ] a = [3, 1, 3, 1, 3] a=[3,1,3,1,3], m i d = 1 + 3 2 = 2 mid = \frac{1+3}{2} = 2 mid=21+3=2, l e f t = [ 1 , 1 ] \mathit{left} = [1, 1] left=[1,1], r i g h t = [ 3 , 3 , 3 ] right = [3, 3, 3] right=[3,3,3]. We choose to keep the r i g h t right right array with the sum equalling 9 9 9.

  5. We can get an array with the sum s 5 = 11 s_5 = 11 s5=11 with zero slicing operations, because array sum is equal to 11 11 11.

Tutorial

由题意得每次分割操作与数组 a a a 元素的顺序没有任何关系,所以可以在最开始直接对数组 a a a 进行排序,排序过后也能更直观地得到当前数组的最大值和最小值

可以花费 O ( n log ⁡ n ) \mathcal O(n \log n) O(nlogn) 的时间复杂度来对 a a a 数组进行递归操作,根据左右区间和前缀和数组,可以很轻易得到区间内的和,由于数组 a a a 也是有序的,也可以通过二分查找以轻易得到切割位置,每次切割完都将区间和放到 s e t \tt set set 或者 m a p \tt map map 中,最后对于每次询问直接进行查找即可

此解法时间复杂度为 O ( n log ⁡ n + q ) \mathcal O(n \log n + q) O(nlogn+q)

Solution

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define int long long

void solve() {
    int n, q;
    cin >> n >> q;
    set<int> s;
    vector<int> a(n + 1), pre(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        pre[i] = a[i] + pre[i - 1];
    }
    ranges::sort(a);
    partial_sum(a.begin(), a.end(), pre.begin());
    
    function<void(int, int)> dfs = [&](int left, int right) {
        if (left > right) {
            return;
        }
        s.insert(pre[right] - pre[left - 1]);
        if (a[left] == a[right]) {
            return;
        }
        int mid = (a[left] + a[right]) >> 1;
        mid = ranges::upper_bound(a, mid) - a.begin() - 1;
        dfs(left, mid);
        dfs(mid + 1, right);
    };
    
    dfs(1, n);
    while (q--) {
        int target;
        cin >> target;
        cout << (s.count(target) ? "Yes" : "No") << endl;
    }
}

signed main() {
    int Test; cin >> Test; while (Test--)
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值