codeforce 914-D. Bash and a Tough Math Puzzle(线段树)

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 xafter 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
Copy
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
Copy
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个数,q个操作

操作一:求[l,r]的gcd是否为x,是输出YES,否输出NO(如果可以改变[l,r]中的一个数使得gcd为x,依然输出YES)

操作二:将x位置的数改为y


分析:操作二很好搞直接单点更新就行了,那么只用考虑操作一;要使区间的gcd为x,这个区间内的所有数一定是x的倍数,因此只需要找区间内不是x倍数的数字有多少个就行了,如果>1个则是NO

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+500;
int t[N<<2];
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&t[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    t[rt]=__gcd(t[rt<<1],t[rt<<1|1]);
}
void update(int pos,int l,int r,int rt,int num)
{
    if(l==r)
    {
        t[rt]=num;
        return;
    }
    int m=(l+r)>>1;
    if(pos<=m)
        update(pos,l,m,rt<<1,num);
    else
        update(pos,m+1,r,rt<<1|1,num);
    t[rt]=__gcd(t[rt<<1],t[rt<<1|1]);
}
int query(int ql,int qr,int l,int r,int rt,int num)
{
    if(l==r)return 1;
    int m=(l+r)>>1,ans=0;
    if(ql<=m&&t[rt<<1]%num!=0)
        ans+=query(ql,qr,l,m,rt<<1,num);
    if(qr>m&&t[rt<<1|1]%num!=0&&ans<=1)//这里<=1剪个枝可以节省很多时间
        ans+=query(ql,qr,m+1,r,rt<<1|1,num);
    return ans;
}

int main()
{
    int n,q,op,l,r,x,y;
    scanf("%d",&n);
    build(1,n,1);
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d%d%d",&l,&r,&x);
                if(query(l,r,1,n,1,x)<=1)
                    puts("YES");
                else puts("NO");

        }
        else
        {
            scanf("%d%d",&x,&y);
            update(x,1,n,1,y);
        }
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章