codeforces 914D(线段树)

题意:

1单点更新

2查询[l,r]区间的值最多改变一个能不能使得区间的gcd等于给出的x,但是此改变不改变原来的值。


思路:线段树维护gcd,很简单。查询的时候可以维护一个cnt的值,表示为当一个点的值不整除x的时候,对数进行改变的个数。当cnt>1的时候,直接结束函数。

#include<bits/stdc++.h>

using namespace std;
const int maxn = 5e5 + 10;
typedef long long ll;
#define clr(x,y) memset(x,y,sizeof x)
#define INF 0x3f3f3f3f
typedef pair<int,int> P;
const ll Mod = 1e9 + 7;


int gcd(int x,int y)
{
    return y ? gcd(y,x % y) : x;
}
int a[maxn];
int tree[maxn << 2];
void build(int l,int r,int rt)
{
    if(l == r)
    {
        tree[rt] = a[l];return ;
    }
    int mid = (l + r) >> 1;
    build(l,mid,rt << 1);build(mid + 1,r,rt << 1|1);
    tree[rt] = gcd(tree[rt << 1],tree[rt << 1|1]);
}
void update(int pos,int x,int l,int r,int rt)
{
    if(l == r)
    {
        tree[rt] = x;return ;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid)
        update(pos,x,l,mid,rt << 1);
    else update(pos,x,mid + 1,r,rt << 1|1);
    tree[rt] = gcd(tree[rt << 1],tree[rt << 1|1]);
}
int cnt;

void fun(int l,int r,int x,int rt)
{
    if(cnt > 1)return;
    if(l == r)
    {
        if(tree[rt] % x)
            cnt ++;
        return ;
    }
    int mid = (l + r) >> 1;
    if(tree[rt << 1] % x)
    fun(l,mid,x,rt << 1);
    if(tree[rt << 1|1] % x)
    fun(mid + 1,r,x,rt << 1|1);
}
int query(int L,int R,int x,int l,int r,int rt)
{
    if(cnt > 1)
        return x;
    if(L <= l && R >= r)
    {
        if(tree[rt] % x)
        {
            fun(l,r,x,rt);
            return x;
        }
        return tree[rt];
    }
    int mid = (l + r) >> 1;
    int ret = 0;
    if(L <= mid)
        ret = gcd(ret,query(L,R,x,l,mid,rt <<1));
    if(R >= mid + 1)
        ret = gcd(ret,query(L,R,x,  mid + 1,r,rt << 1|1));
    return ret;
}
int main()
{
    int n;
    while( ~ scanf("%d",&n))
    {
        for(int i = 1;i <= n;i ++)
            scanf("%d",&a[i]);
        build(1,n,1);
        int m ;scanf("%d",&m);
        for(int i = 1;i <= m;i ++)
        {
            int type;scanf("%d",&type);
            if(type == 1)
            {
                int l,r,x;scanf("%d%d%d",&l,&r,&x);
                cnt = 0;
                query(l,r,x,1,n,1);
                if(cnt <= 1)
                    puts("YES");
                else puts("NO");
            }
            else
            {
                int x,y;scanf("%d%d",&x,&y);
                if(a[x] == y)continue;
                a[x] = y;
                update(x,y,1,n,1);
            }
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值