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 changeat 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 qqueries 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 1are 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.

题意:给你一个长为n的序列,然后q次查询:

(1)可否改变一个数使得查询的区间[l,r]gcd为x

(2)将位置i的数变为y

题解:裸的线段树单点更新+查询,对于每次查询只用看有多少个子区间的gcd不是x即可,我们知道假如满足题意的话,一定只会递归到一个点上,故记一下数量即可,而更新就是裸的单点更新了。

注:查询的时候最好不要暴力查,最好加一下判定条件优化一下,比如说此时的不符合条件子区间已经不止一个了,呢就没必要往下查询了。。

Codecraft-18 and Codeforces Round #458 (D)-Bash and a Tough Math Puzzle(线段树)_#include

这就是区别。。。2333

#include<stdio.h>
#include<string.h>
#define maxn 5000050
int sum[maxn],n,q;
int gcd(int a,int b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}
void build(int id,int l,int r)
{
	if(l==r)
	{
		scanf("%d",&sum[id]);
		return;
	}
	int mid=(l+r)/2;
	build(id*2,l,mid);
	build(id*2+1,mid+1,r);
	sum[id]=gcd(sum[id*2],sum[id*2+1]);
}
void update(int id,int l,int r,int k,int val)
{
	if(l==r)
	{
		sum[id]=val;
		return;
	}
	int mid=(l+r)/2;
	if(k<=mid)
		update(id*2,l,mid,k,val);
	else
		update(id*2+1,mid+1,r,k,val);
	sum[id]=gcd(sum[id*2],sum[id*2+1]);
}
int query(int id,int l,int r,int L,int R,int val)
{
	if(l==r) return 1;
	int res=0,mid=(l+r)/2;
	if(L<=mid && sum[id*2]%val!=0)
		res+=query(id*2,l,mid,L,R,val);
	if(R>mid && sum[id*2+1]%val!=0 && res<=1)
		res+=query(id*2+1,mid+1,r,L,R,val);
	return res;
}
int main(void)
{
	int t,l,r,x,y;
	scanf("%d",&n);
	build(1,1,n);
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d",&t);
		if(t==1)
		{
			scanf("%d%d%d",&l,&r,&x);
			if(query(1,1,n,l,r,x)<=1)
				printf("YES\n");
			else
				printf("NO\n");
		}
		else
		{
			scanf("%d%d",&x,&y);
			update(1,1,n,x,y);
		}
	}
	return 0;
}