D. Ones and Twos

D. Ones and Twos

You are given a 1-indexed array a of length n where each element is 1 or 2.

Process q queries of the following two types:

  • "1 s": check if there exists a subarray of a whose sum equals to s.
  • "2 i v": change ai to v.

An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

Input

Each test contains multiple test cases. The first line contains a single integer t (1t104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n and q (1n,q105) — the length of array a and the number of queries.

The second line of each test case contains n integers a1,a2,,an (ai is 1 or 2) — the elements of array a.

Each of the following q lines of each test case contains some integers. The first integer op is either 1 or 2.

  • If op is 1, it is followed by one integer s (1s2n).
  • If op is 2, it is followed by two integers i and v (1in, v is 1 or 2).

It is guaranteed that the sum of n and the sum of q over all test cases both do not exceed 105.

Output

For each query with op=1, output "YES" in one line if there exists a subarray of a whose sum is equals to s, otherwise output "NO".

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Example

input

2
5 5
2 1 2 1 2
1 5
1 6
1 7
2 4 2
1 7
3 2
2 2 2
1 6
1 5

output

YES
YES
NO
YES
YES
NO

Note

Consider the first example:

The answer for the first query is "YES" because a1+a2+a3=2+1+2=5.

The answer for the second query is "YES" because a1+a2+a3+a4=2+1+2+1=6.

The answer for the third query is "NO" because we cannot find any subarray of a whose sum is 7.

After the fourth query, the array a becomes [2,1,2,2,2].

The answer for the fifth query is "YES" because a2+a3+a4+a5=1+2+2+2=7.

 

解题思路

  昨晚最后几分钟乱猜了个做法过的,性质观察不出来是真做不了。

  用 s[l,r] 来表示子数组 [l,r] 的和。最关键的性质是如果存在某个子数组的和 s[l,r]2,那么一定存在和为 s[l,r]2 的子数组,这是因为

  1. 如果 al=2,那么有子数组 [l+1,r] 的和为 s[l+1,r]=s[l,r]2
  2. 如果 ar=2,那么有子数组 [l,r1] 的和为 s[l,r1]=s[l,r]2
  3. 如果 al=ar=1,那么有子数组 [l+1,r1] 的和为 s[l+1,r1]=s[l,r]2

  因此对于询问 1 中的 v,首先如果 v>s[1,n] 那么一定无解。否则我们检查 s[1,n]v 的奇偶性是否相同,相同的话则一定有解,这是因为可以通过不断减 2(不改变奇偶性)来得到 v。否则奇偶性不同,如果数组 a 中全是 2,那么显然无解(无法改变奇偶性)。否则 a 中至少存在一个 1,找到最靠左的 1 的位置 l 以及最靠右的位置 r,则 s[l+1,n]s[1,r1]v 的奇偶性相同。如果 v>max{s[l+1,n],s[1,r1]} 也是无解的,否则可以通过不断减 2 来得到 v

  AC 代码如下,时间复杂度为 O(nlogn)

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

typedef long long LL;

const int N = 1e5 + 10;

int a[N];

void solve() {
    int n, m;
    scanf("%d %d", &n, &m);
    set<int> st;    // 维护所有1的下标
    int s = 0;    // 整个数组a的和
    for (int i = 1; i <= n; i++) {
        scanf("%d", a + i);
        s += a[i];
        if (a[i] == 1) st.insert(i);
    }
    while (m--) {
        int op, x, y;
        scanf("%d %d", &op, &x);
        if (op == 1) {
            if (x > s) {    // x比整个数组大,无解
                printf("NO\n");
            }
            else {
                if ((s - x) % 2 == 0) {    // x与s的奇偶性相同
                    printf("YES\n");
                }
                else if (st.empty()) {    // x与s的奇偶性不同,且a中不存在1
                    printf("NO\n");
                }
                else {
                    int l = *st.begin(), r = *st.rbegin();    // 找到最左边的1和最右边的1
                    if (x <= max(s - l * 2 + 1, s - (n - r + 1) * 2) + 1) printf("YES\n");
                    else printf("NO\n");
                }
            }
        }
        else {
            scanf("%d", &y);
            if (a[x] == 1) st.erase(x);
            s += y - a[x];
            a[x] = y;
            if (a[x] == 1) st.insert(x);
        }
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  CodeTON Round 7 (Div. 1 + Div. 2, Rated, Prizes!) Editorial:https://codeforces.com/blog/entry/122172

posted @   onlyblues  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-11-26 C. Almost All Multiples
2021-11-26 关于KMP算法中模式串的移动不会产生漏解的证明
Web Analytics
点击右上角即可分享
微信分享提示