您的位置:首页 > 运维架构 > Shell

Codeforces Round #458 D. Bash and a Tough Math Puzzle(线段树)

2018-02-05 11:19 357 查看
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.

题意:n个数的数列,对它有下面两种操作

1. 猜 l ~ r 这部分数的最大公因数为x,如果x不是最大公因数,但只改变l ~ r中的一个数就能让x是最大公因数,那么可以认为是猜中了;

2. 把某一个数 i 的值变为 y。

用线段树来做,做更改时直接做,每个点存gcd,然后查询时,我们判断区间不满足时就判断到点,并让cnt++,如果cnt大于1直接return(只能有一个数字不是x的倍数)。

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
#define rson rt<<1|1,m+1,r
#define lson rt<<1,l,m
using namespace std;
const int N=5e5+10;
int arr
;
ll tree[N<<2],gc[N<<2],cnt;
int gcd(int a,int b)
{
if(a<b)
swap(a,b);
return b==0?a:gcd(b,a%b);
}
void build(int rt,int l,int r)
{
//cout<<l<<'<'<<r<<endl;
if(l==r)
{

tree[rt]=arr[l];
//cout<<rt<<' '<<tree[rt]<<endl;
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
tree[rt]=gcd(tree[rt<<1],tree[rt<<1|1]);
}
void update(int in,int ch,int rt,int l,int r)
{
if(l==r)
{
tree[rt]=ch;
return;
}
int m=(l+r)>>1;
if(in<=m)
update(in,ch,lson);
else
update(in,ch,rson);
tree[rt]=gcd(tree[rt<<1],tree[rt<<1|1]);
}
void fi(int L,int R,int x,int rt,int l,int r)
{
if(cnt>1)
return ;
if(l==r)
{
cnt++;
return;
}
int m=(l+r)>>1;
if(L<=m&&tree[rt<<1]%x!=0)
fi(L,R,x,lson);
if(R>m&&tree[rt<<1|1]%x!=0)
fi(L,R,x,rson);
}
int main()
{
int n,q;
cin>>n;
rep(i,1,n+1)
cin>>arr[i];
build(1,1,n);
cin>>q;
while(q--)
{
int t;
cin>>t;
if(t==1)
{
int l,r,x;
cin>>l>>r>>x;
cnt=0;
fi(l,r,x,1,1,n);
if(cnt<=1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else
{
int i,y;
cin>>i>>y;
update(i,y,1,1,n);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐