cf914D(线段树+思维)

Bashanda喜欢猜测数组不同区间的最大公约数(GCD),并希望改变最多一个元素使GCD接近他的猜测值。此问题通过构建区间树解决查询和更新操作,实现了高效的求解。

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

看到可以允许改一个数我就懵逼了..那我是不是得统计所有情况?感觉完全没思路..

然后汪聚聚提出一种思路..算出能被x整除的数的个数..然后就可以处理误差了...(真是好巧妙的思路蛙orz!!)

实际操作中只要处理不能被x整除的数,维护gcd,然后如果2个分支都不能整除就可以退出了,如果只有一个那么就要探到叶子,所以一次操作复杂度为logn




#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 500005
#define nm 2000005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}

int n,m,_y,_x,_t;
int gcd(int x,int y){return y==0?x:gcd(y,x%y);}

struct info{
	int s,size;
	info operator+(const info&o){
		info f;
		f.s=gcd(s,o.s);
		f.size=size+o.size;
		return f;
	}
}T[4*NM];

void build(int i,int x,int y){
	int t=x+y>>1;
	if(x==y){op.s=read();op.size=1;return;}
	build(i<<1,x,t);build(i<<1|1,t+1,y);
	op=ls+rs;
}

void mod(int i,int x,int y){
	int t=x+y>>1;
	if(x==y){op.s=_y;return;}
	if(_x<=t)mod(i<<1,x,t);
	else mod(i<<1|1,t+1,y);
	op=ls+rs;
}

int query(int i,int x,int y){
	int t=x+y>>1;
	if(_y<x||y<_x)return 0;
	if(_x<=x&&y<=_y){
		if(op.s%_t==0)return 0;
		if(x==y)return 1;
		if(ls.s%_t==0)return query(i<<1|1,t+1,y);
		if(rs.s%_t==0)return query(i<<1,x,t);
		return 2;
	}
	return query(i<<1,x,t)+query(i<<1|1,t+1,y);
}

int main(){
	//freopen("data.in","r",stdin);
	n=read();
	build(1,1,n);
	m=read();
	while(m--){
		_t=read();_x=read();_y=read();
		if(_t==1){
			_t=read();
			_t=query(1,1,n);
			puts(_t<2?"YES":"NO");
		}else mod(1,1,n);
	}
}



D. Bash and a Tough Math Puzzle
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bash likes playing with arrays. He has an array a1, a2, ... an of n integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.

Suppose he guesses that the gcd of the elements in the range [l, r] of a is x. He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is x after making the change. Note that when he guesses, he doesn't actually change the array — he just wonders if the gcd of the segment can be made x. Apart from this, he also sometimes makes changes to the array itself.

Since he can't figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process q queries of one of the following forms:

  • 1 l r x — Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct.
  • 2 i y — Bash sets ai to y.

Note: The array is 1-indexed.

Input

The first line contains an integer n (1 ≤ n ≤ 5·105)  — the size of the array.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109)  — the elements of the array.

The third line contains an integer q (1 ≤ q ≤ 4·105)  — the number of queries.

The next q lines describe the queries and may have one of the following forms:

  • 1 l r x (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ 109).
  • 2 i y (1 ≤ i ≤ n, 1 ≤ y ≤ 109).

Guaranteed, that there is at least one query of first type.

Output

For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise.

Examples
Input
3
2 6 3
4
1 1 2 2
1 1 3 3
2 1 9
1 1 3 2
Output
YES
YES
NO
Input
5
1 2 3 4 5
6
1 1 4 2
2 3 6
1 1 4 2
1 1 5 2
2 5 10
1 1 5 2
Output
NO
YES
NO
YES
Note

In the first sample, the array initially is {2, 6, 3}.

For query 1, the first two numbers already have their gcd as 2.

For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array.

After query 3, the array is now {9, 6, 3}.

For query 4, no matter which element you change, you cannot get the gcd of the range to be 2.


### 关于C++线段树实现中的错误分析 在线段树的实现过程中,可能会遇到多种类型的错误。以下是常见的几种原因及其可能引发的问题: #### 1. **未初始化变量** 如果在构建线段树之前没有对数组或其他存储结构进行清零处理,则可能导致残留数据干扰计算结果[^3]。 例如,在某些情况下,`sum` 或 `lazy` 数组如果没有被正确初始化为零,后续的操作会受到先前数据的影响。 ```cpp // 初始化操作 memset(sum, 0, sizeof(sum)); memset(lazy, 0, sizeof(lazy)); ``` #### 2. **懒惰标记(Lazy Propagation)问题** 当使用带延迟更新的线段树时,忘记下传懒惰标记或者错误地实现了 `push_down` 函数,都会导致查询结果不准确[^2]。 ```cpp void push_down(int p, int l, int r) { if (lazy[p]) { int mid = (l + r) >> 1; lazy[left(p)] += lazy[p]; sum[left(p)] += (mid - l + 1) * lazy[p]; lazy[right(p)] += lazy[p]; sum[right(p)] += (r - mid) * lazy[p]; lazy[p] = 0; // 清除当前节点的懒惰标记 } } ``` #### 3. **边界条件处理不当** 对于区间的划分和递归终止条件,如果不小心设置错误,可能会导致无限递归或错过目标区间[^4]。 例如,在以下代码片段中,如果 `if(tree[i].l > X || tree[i].r < X)` 条件写错,就无法正常退出递归。 ```cpp void find(int i) { if (tree[i].l > X || tree[i].r < X) return ; ans += tree[i].cnt; find(i * 2); find(i * 2 + 1); } ``` #### 4. **内存分配不足** 由于线段树通常需要两倍甚至四倍的空间来存储节点信息,因此如果数组大小定义过小,就会发生越界访问。 建议按照实际需求合理规划空间大小,比如将数组容量设为 `N << 2`。 ```cpp const int MAX_N = 1e6 + 5; int segTree[MAX_N << 2]; // 开辟足够的空间 ``` #### 5. **逻辑错误** 有时算法本身的逻辑存在缺陷,例如在解决特定题目时未能充分考虑特殊情况。以 CF527C Glass Carving 为例,若未维护好最大矩形面积的变化过程,则难以得出正确答案[^5]。 --- ### 总结 综上所述,C++线段树实现可能出现的错误主要包括但不限于以上几点。开发者应仔细检查每一处细节,并通过调试工具验证程序行为是否符合预期。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值