Codeforces914D Bash and a Tough Math Puzzle 线段树维护gcd

题意:给你一个序列,有两个操作:1、把某个数改为另一个数。2、询问你某一区间内的所有数,能否只修改其中一个数(但不用修改),使得该区间所有数的最大公约数gcd为k,k在每次询问时给出。
思路:单点修改不必说,对于询问:若只修改一个数使得序列符合要求,那么我们需要找有多少数不符合要求,那怎样才算符合要求呢?
我这样说:
若询问的区间的最大公约数是k的倍数(即gcd%k==0),那么所有的数(都是k的倍数)一定符合要求。修改的话把任意一个数改成k即可。
若询问的区间的最大公约数不是k的倍数(即gcd%k!=0),那么至少有一个数不是k的倍数。若所询问区间只有一个数不是k的倍数,那正好把这个数改了,就符合要求了,相反若多于两个数不是k的倍数,那就不符合要求。所以我们的query函数就是寻找不符合要求的数有几个,一旦超过两个,那就妥妥的不符合要求。
下面是AC代码。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
#define inf 0x3f3f3f3f
#define cl(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=5e5+10;
int n,q,cnt;//cnt记录有多少个坏点(不符合要求的点 
int a[maxn];
struct node{
	int l,r,g;
}t[maxn<<2];
int gcd(int a,int b){
	if(b==0) return a;
	return gcd(b,a%b);
}
void pushup(int k){
	t[k].g=gcd(t[k<<1].g,t[k<<1|1].g);
}
void build(int k,int l,int r){
	t[k].l=l,t[k].r=r;
	if(l==r){
		t[k].g=a[l];
	}else{
		int mid=(l+r)>>1;
		build(k<<1,l,mid);
		build(k<<1|1,mid+1,r);
		pushup(k);
	}
}
void updata(int k,int p,int v){
	if(t[k].l==t[k].r){
		t[k].g=v;
	}else{
		int mid=(t[k].l+t[k].r)>>1;
		if(p<=mid) updata(k<<1,p,v);
		if(mid<p) updata(k<<1|1,p,v);
		pushup(k);
	}
}
void query(int k,int l,int r,int g){
	if(cnt>1) return;//直接剪枝  已经多余两个了不用再问了
	if(t[k].l==t[k].r){//每当搜到一个不符合要求的叶结点 cnt就++。
		cnt++;
	}else{
		int mid=(t[k].l+t[k].r)>>1;
		if(l<=mid&&(t[k<<1].g%g)) query(k<<1,l,r,g);//若左子树的最大公约数不符合条件,就去搜搜左子树有几个
		if(mid<r&&(t[k<<1|1].g%g)) query(k<<1|1,l,r,g);//右子树同理
	}
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	build(1,1,n);
	scanf("%d",&q);
	while(q--){
		int op;
		scanf("%d",&op);
		if(op==1){
			int l,r,x;
			scanf("%d%d%d",&l,&r,&x);
			cnt=0;
			query(1,l,r,x);
			if(cnt>1) printf("NO\n");
			else printf("YES\n");
		}else{
			int i,y;
			scanf("%d%d",&i,&y);
			updata(1,i,y);
		}
	}
	return 0;
}

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值