D. Ones and Twos

发布时间 2023-11-26 21:20:56作者: onlyblues

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$^{\dagger}$ of $a$ whose sum equals to $s$.
  • "2 i v": change $a_i$ to $v$.

$^{\dagger}$ 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$ ($1 \le t \le 10^4$) — 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$ ($1\le n,q\le 10^5$) — the length of array $a$ and the number of queries.

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($a_i$ 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 $\mathrm{op}$ is either $1$ or $2$.

  • If $\mathrm{op}$ is $1$, it is followed by one integer $s$ ($1 \leq s \leq 2n$).
  • If $\mathrm{op}$ is $2$, it is followed by two integers $i$ and $v$ ($1 \leq i \leq n$, $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 $10^5$.

Output

For each query with $\mathrm{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 $a_1+a_2+a_3=2+1+2=5$.

The answer for the second query is "YES" because $a_1+a_2+a_3+a_4=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 $a_2+a_3+a_4+a_5=1+2+2+2=7$.

 

解题思路

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

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

  1. 如果 $a_l = 2$,那么有子数组 $[l+1,r]$ 的和为 $s[l+1,r] = s[l,r]-2$。
  2. 如果 $a_r = 2$,那么有子数组 $[l,r-1]$ 的和为 $s[l,r-1] = s[l,r]-2$。
  3. 如果 $a_l = a_r = 1$,那么有子数组 $[l+1,r-1]$ 的和为 $s[l+1,r-1] = 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,r-1]$ 与 $v$ 的奇偶性相同。如果 $v > \max\{ s[l+1,n], s[1,r-1] \}$ 也是无解的,否则可以通过不断减 $2$ 来得到 $v$。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#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