CodeForces - 914D -Bash and a Tough Math Puzzle (线段树+点更新)

题目链接:http://codeforces.com/problemset/problem/914/D

题目:

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 lrx — Bash guesses that the gcd of the range [l, r] is x. Report if this guess is almost correct.
  • 2 iy — 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 lrx (1 ≤ l ≤ r ≤ n, 1 ≤ x ≤ 109).
  • 2 iy (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.

 

题目大意:判断x是否是这个区间内的最大公约数,若改变区间其中一个元素后最大公约数是x也符合。

有两种操作,1 i j k, 判断 i 到 j 的区间内的最大公约数是否是x。

2 i j ,将第 i 个元素改为 j.

思路:单点更新,很简单。判断x是否为最大公约数时,若不需改变就符合,很容易,若是不符合则需要进行一下递归。

用线段树做区间gcd,然后如果要查询[l,r],就先判断[l,mid]和[mid+1,r]这两半区间是否最多有一个不是x的倍数,如果两个都不是x的倍数,那么就不可能改变为x;如果两个都是x的倍数,那么这一整段的gcd肯定可以是x;如果有一个不是x的倍数,那么就递归处理这一段。

void getsum(int ans,int l,int r,int x)//sum表示的是,区间内不是x的倍数的数的个数
{
    if(sum > 1) return;
    if(f[ans].left==f[ans].right )
    {
        sum++;
        return;
    }
    int mid=(f[ans].left +f[ans].right )>>1;
    if(l<=mid&&f[ans<<1].ggcd%x!=0)
        getsum(ans<<1,l,r,x);
    if(r>mid&&f[ans<<1|1].ggcd%x!=0)
        getsum(ans<<1|1,l,r,x);
}

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
typedef  long long ll;
int const INF=5e5+5;
int n,m;
int a[INF];
struct node{
	int right;
	int left;
	int ggcd;
}f[4*INF];
int sum;//sum表示的是,区间内不是x的倍数的数的个数
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
void pushup(int ans)
{
    f[ans].ggcd  = gcd(f[ans<<1].ggcd ,f[ans<<1|1].ggcd );
}
void build(int ans,int l,int r)
{
	f[ans].left =l;f[ans].right =r;
    if (l == r)
    {
        f[ans].ggcd =a[l];
        return;
    }
    int mid=(l + r)>>1;
    build(ans<<1,l,mid);
    build(ans<<1|1,mid+1,r);
    pushup(ans);
}
void update(int ans,int x,int d)
{
    if(f[ans].left == f[ans].right )
    {
        f[ans].ggcd =d;
        return;
    }
    int mid=(f[ans].left +f[ans].right )>>1;
    if(x<=mid)
        update(ans<<1,x,d);
    else
        update(ans<<1|1,x,d);
    pushup(ans);
}
void getsum(int ans,int l,int r,int x)
{
    if(sum > 1) return;//剪枝 
    if(f[ans].left==f[ans].right )
    {
        sum++;
        return;
    }
    int mid=(f[ans].left +f[ans].right )>>1;
    if(l<=mid&&f[ans<<1].ggcd%x!=0)
        getsum(ans<<1,l,r,x);
    if(r>mid&&f[ans<<1|1].ggcd%x!=0)
        getsum(ans<<1|1,l,r,x);
}
int main()
{
    int i,j,k,val;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    	scanf("%d",&a[i]);
	}
    build(1,1,n);
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&val);
        if(val==1)
        {
            scanf("%d%d%d",&i,&j,&k);
            sum = 0;
            getsum(1,i,j,k);
            if(sum <= 1)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
        {
            scanf("%d%d",&i,&j);
            update(1,i,j);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值