D. Ones and Twos
D. Ones and Twos
You are given a -indexed array of length where each element is or .
Process queries of the following two types:
- "1 s": check if there exists a subarray of whose sum equals to .
- "2 i v": change to .
An array is a subarray of an array if can be obtained from 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 () — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers and () — the length of array and the number of queries.
The second line of each test case contains integers ( is or ) — the elements of array .
Each of the following lines of each test case contains some integers. The first integer is either or .
- If is , it is followed by one integer ().
- If is , it is followed by two integers and (, is or ).
It is guaranteed that the sum of and the sum of over all test cases both do not exceed .
Output
For each query with , output "YES" in one line if there exists a subarray of whose sum is equals to , 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 .
The answer for the second query is "YES" because .
The answer for the third query is "NO" because we cannot find any subarray of whose sum is .
After the fourth query, the array becomes .
The answer for the fifth query is "YES" because .
解题思路
昨晚最后几分钟乱猜了个做法过的,性质观察不出来是真做不了。
用 来表示子数组 的和。最关键的性质是如果存在某个子数组的和 ,那么一定存在和为 的子数组,这是因为
- 如果 ,那么有子数组 的和为 。
- 如果 ,那么有子数组 的和为 。
- 如果 ,那么有子数组 的和为 。
因此对于询问 中的 ,首先如果 那么一定无解。否则我们检查 和 的奇偶性是否相同,相同的话则一定有解,这是因为可以通过不断减 (不改变奇偶性)来得到 。否则奇偶性不同,如果数组 中全是 ,那么显然无解(无法改变奇偶性)。否则 中至少存在一个 ,找到最靠左的 的位置 以及最靠右的位置 ,则 和 与 的奇偶性相同。如果 也是无解的,否则可以通过不断减 来得到 。
AC 代码如下,时间复杂度为 :
#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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17857982.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-11-26 C. Almost All Multiples
2021-11-26 关于KMP算法中模式串的移动不会产生漏解的证明