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.
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.
For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise.
3 2 6 3 4 1 1 2 2 1 1 3 3 2 1 9 1 1 3 2
YES YES NO
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
NO YES NO YES
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.
#include<bits/stdc++.h>
using namespace std;
const int N = 500005;
struct node{
int l,r;
int gcd;
}tree[N*4];
int n,m,arr[N];
vector<int>G;
int Gcd(int a,int b){ //取最大公约数
while(b){
int c=a%b;
a=b;
b=c;
}
return a;
}
int check(int root,int x){
if(tree[root].l==tree[root].r) return 1;
int mid=(tree[root].l+tree[root].r)/2;
if(tree[root*2].gcd%x&&tree[root*2+1].gcd%x) return 0;
if(tree[root*2].l==tree[root*2+1].r){
if(tree[root*2].gcd%x==0||tree[root*2+1].gcd%x==0) return 1;
else return 0;
}
if(tree[root*2].gcd%x!=0) return check(root*2,x);
else return check(root*2+1,x);
}
void pushUp(int root){
tree[root].gcd=Gcd(tree[root*2].gcd,tree[root*2+1].gcd);
}
void build(int root,int left,int right){
tree[root].l=left;
tree[root].r=right;
if(left==right){
tree[root].gcd=arr[left];
return;
}
int mid=(left+right)/2;
build(root*2,left,mid);
build(root*2+1,mid+1,right);
pushUp(root);
}
void update(int root,int pos,int data){
if(tree[root].l==tree[root].r){
tree[root].gcd=data;
return;
}
int mid=(tree[root].l+tree[root].r)/2;
if(pos<=mid) update(root*2,pos,data);
else update(root*2+1,pos,data);
pushUp(root);
}
void query(int root,int left,int right){
if(tree[root].l>=left&&tree[root].r<=right){
G.push_back(root);
return;
}
if(left>tree[root].r||right<tree[root].l) return;
int mid=(tree[root].l+tree[root].r)/2;
if(mid>=left) query(root*2,left,right);
if(mid<right) query(root*2+1,left,right);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&arr[i]);
build(1,1,n);
scanf("%d",&m);
while(m--){
int op;
scanf("%d",&op);
if(op==2){
int pos,data;
scanf("%d%d",&pos,&data);
update(1,pos,data);
}
else{
int l,r,x;
scanf("%d%d%d",&l,&r,&x);
G.clear();
query(1,l,r);
int cnt=0,pos;
for(int i=0;i<G.size();i++){
if(tree[G[i]].gcd%x!=0){
cnt++;
pos=G[i];
}
}
if(cnt==0) printf("YES\n");
else if(cnt>1)printf("NO\n");
else{
if(check(pos,x)) printf("YES\n");
else printf("NO\n");
}
}
}
return 0;
}