0
点赞
收藏
分享

微信扫一扫

Divide and Summarize(二分,递归,前缀和)

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

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

uploading.4e448015.gif

正在上传…重新上传取消

An array slicing operation is conducted in the following way:

  • assume mid=⌊max(array)+min(array)2⌋mid=⌊max(array)+min(array)2⌋, where maxmax and minmin — are functions that find the maximum and the minimum array elements. In other words, midmid is the sum of the maximum and the minimum element of arrayarray divided by 22 rounded down.
  • Then the array is split into two parts leftleft and rightright. The leftleft array contains all elements which are less than or equal midmid, and the rightright array contains all elements which are greater than midmid. Elements in leftleft and rightright keep their relative order from arrayarray.
  • During the third step we choose which of the leftleft and rightright 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 qq prettiness tests.

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

Input

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

The first line of each test case contains two integers nn and qq (1≤n,q≤105)(1≤n,q≤105) — the length of the array aa and the total number of prettiness tests.

The second line of each test case contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤106)(1≤ai≤106) — the contents of the array aa.

Next qq lines of each test case contain a single integer sisi (1≤si≤109)(1≤si≤109) — the sum of elements which Mike wants to get in the ii-th test.

It is guaranteed that the sum of nn and the sum of qq does not exceed 105105 (∑n,∑q≤105∑n,∑q≤105).

Output

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

Example

input

Copy

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

output

Copy

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 s1=1s1=1 in the following way:

    1.1 a=[1,2,3,4,5]a=[1,2,3,4,5], mid=1+52=3mid=1+52=3, left=[1,2,3]left=[1,2,3], right=[4,5]right=[4,5]. We choose to keep the leftleft array.

    1.2 a=[1,2,3]a=[1,2,3], mid=1+32=2mid=1+32=2, left=[1,2]left=[1,2], right=[3]right=[3]. We choose to keep the leftleft array.

    1.3 a=[1,2]a=[1,2], mid=1+22=1mid=1+22=1, left=[1]left=[1], right=[2]right=[2]. We choose to keep the leftleft array with the sum equalling 11.

  2. It can be demonstrated that an array with the sum s2=8s2=8 is impossible to generate.
  3. An array with the sum s3=9s3=9 can be generated in the following way:

    3.1 a=[1,2,3,4,5]a=[1,2,3,4,5], mid=1+52=3mid=1+52=3, left=[1,2,3]left=[1,2,3], right=[4,5]right=[4,5]. We choose to keep the rightright array with the sum equalling 99.

  4. It can be demonstrated that an array with the sum s4=12s4=12 is impossible to generate.
  5. We can get an array with the sum s5=6s5=6 in the following way:

    5.1 a=[1,2,3,4,5]a=[1,2,3,4,5], mid=1+52=3mid=1+52=3, left=[1,2,3]left=[1,2,3], right=[4,5]right=[4,5]. We choose to keep the leftleft with the sum equalling 66.

Explanation of the second test case:

  1. It can be demonstrated that an array with the sum s1=1s1=1 is imposssible to generate.
  2. We can get an array with the sum s2=2s2=2 in the following way:

    2.1 a=[3,1,3,1,3]a=[3,1,3,1,3], mid=1+32=2mid=1+32=2, left=[1,1]left=[1,1], right=[3,3,3]right=[3,3,3]. We choose to keep the leftleft array with the sum equalling 22.

  3. It can be demonstrated that an array with the sum s3=3s3=3 is imposssible to generate.
  4. We can get an array with the sum s4=9s4=9 in the following way:

    4.1 a=[3,1,3,1,3]a=[3,1,3,1,3], mid=1+32=2mid=1+32=2, left=[1,1]left=[1,1], right=[3,3,3]right=[3,3,3]. We choose to keep the rightright array with the sum equalling 99.

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

思路:

1,气死,l和i要分开,别混了。同一个思路,不同的实现形式,结果就不一样

2,用map存数,大大减少复杂度,前缀和记录和,二分搜索,递归搜索

代码:

int a[maxj],b[maxj];
int n,q,s;
map<int,int>mp;
void ask(int l,int r){
    bool ff=1;
    int k=a[l];
    if(l>r)return ;
    for(int i=l;i<=r;++i)if(k!=a[i]){//都为一个数的时候,不可再次递归
        ff=0;
        break;
    }
    mp[b[r]-b[l-1]]++;//记载数可行
    if(ff)return ;
    double mid=(a[l]+a[r])/2.0;
    int j;
    for(int i=l;i<=r;++i)
    if(mid>=a[i])j=i;//l和i混了,草,要分阶段调试
    ask(l,j);ask(j+1,r);//二分
}
void solve(){//递归二分,用map存数
    cin>>n>>q;
    mp.clear();
    rep(i,1,n)cin>>a[i];
    sort(a+1,a+1+n);
    rep(i,1,n)b[i]=b[i-1]+a[i];//前缀和
    ask(1,n);
    while(q--){
        cin>>s;
        if(mp[s])cout<<"Yes"<<'\n';
        else cout<<"No"<<'\n';
    }
}
举报

相关推荐

0 条评论