cf914d 线段树+gcd

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;

#define lson i<<1
#define rson (i<<1)|1

int n,q;
int a[500005];
struct node
{
    int l,r;
    int gcd;
}tr[500005*5];

int fgcd(int a,int b)
{
    if(b==0)
        return a;
    else
        return fgcd(b,a%b);
}

node up(int i)
{
    node ntr;
    ntr.l=tr[lson].l;ntr.r=tr[rson].r;
    ntr.gcd=fgcd(tr[lson].gcd,tr[rson].gcd);
    return ntr;
}

void build(int i,int l,int r)
{
    tr[i].l=l;tr[i].r=r;
    if(l==r)
    {
        tr[i].gcd=a[l];
    } else
    {
        int mid=(l+r)/2;
        build(lson,l,mid);
        build(rson,mid+1,r);
        tr[i]=up(i);
    }
}

void update(int i,int l,int r,int L,int R,int y)
{
    if(l==L && r==R)
    {
        tr[i].gcd=y;
    }
    else
    {
        int mid=(l+r)/2;
        if(R<=mid)
            update(lson,l,mid,L,R,y);
        else if(L>mid)
            update(rson,mid+1,r,L,R,y);
        else
        {
            update(lson,l,mid,L,mid,y);
            update(rson,mid+1,r,mid+1,R,y);
        }

        tr[i]=up(i);
    }
}
int ansf;
bool query(int i,int l,int r,int L,int R,int x)
{
    if(ansf==0)
        return false;
    if(l==L && r==R)
    {
        if(tr[i].gcd%x==0)
            return true;
        if(l!=r && tr[lson].gcd%x!=0 && tr[rson].gcd%x!=0)
        {
            ansf=0;
            return false;
        }
    }
    if(l==r)
        if(tr[i].gcd%x==0)
            return true;
        else
            return false;

    int mid=(l+r)/2;
    if(R<=mid)
    {
        return query(lson,l,mid,L,R,x);
    }
    else if(L>mid)
    {
        return query(rson,mid+1,r,L,R,x);
    }
    else
    {
        bool ans1=query(lson,l,mid,L,mid,x);
        bool ans2=query(rson,mid+1,r,mid+1,R,x);
        if(ans1==false && ans2==false)
            ansf=0;
        if(ans1==false || ans2==false)
            return false;
        if(ans1==true && ans2==true)
            return true;
    }
}
int main() {

    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%d",&q);
        build(1,1,n);
        while(q--)
        {
            int f;
            scanf("%d",&f);
            if(f==1)
            {
                int l,r,x;
                scanf("%d%d%d",&l,&r,&x);
                ansf=1;
                query(1,1,n,l,r,x);
                if(ansf==1)
                    printf("YES\n");
                else
                    printf("NO\n");
            } else
            {
                int i,y;
                scanf("%d%d",&i,&y);
                update(1,1,n,i,i,y);
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值