序列只包含1和2,q次操作,两种类型:(1)询问是否存在区间和为s(2)使a[x] = y

文章讲述了如何使用C++解决一个关于整数数组的问题,涉及区间和计算、使用set优化查询操作,以及处理整数替换对区间和影响的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
void solve(){
	int n, q, i, j;
	cin >> n >> q;
	int sum = 0;
	set<int> S;//1的下标集合
	for(i = 1; i <= n; i++){
		cin >> a[i];
		if(a[i] == 1) S.insert(i);
		sum += a[i];
	}
	while(q--){
		int op, x, y;
		cin >> op;
		if(op == 1){
			cin >> x;
			int lst = 0, rst = 0, cnt, maxx = 0;
			if(S.size()){
				lst = *S.begin(), rst = *S.rbegin();//找最左端的1和最右端的1,看哪个离端点最近,该1的位置到端点的区间和为maxx,那么[1, maxx]的询问都能满足
			  cnt = min(lst - 1, n - rst);
			  maxx = sum - 2 * cnt;
			} 
			if(x <= maxx) cout << "Yes\n";
			else{
				if((x + sum) % 2 == 0 && x <= sum){//大于maxx的话,得小于等于sum且与sum奇偶性相同(只能-2,-2...)
					cout << "Yes\n";
				}
				else cout << "No\n";
			}
		}
		else{
			cin >> x >> y;
			if(a[x] != y){
				a[x] = y;
				if(y == 1){
					S.insert(x);
					sum--;
				}
				else{
					S.erase(x);
					sum++;
				}
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

__night_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值