zoukankan      html  css  js  c++  java
  • CodeForces

    题面
    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.
    ...

    题意
    给定一个数组,有两种操作,第一种修改某个值,第二种查询区间内,如果任意修改某个值,区间所有值的(gcd)可不可以等于(x).

    思路
    所有数(gcd)等于(x),可以推出所有数能整除(x).
    如果区间内(长度为(len)) 有(len-1)个数字都能整除(x),那么只需把剩下的那个数改成(x)就行了.

    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    
    #define fuck(x) cerr<<#x<<" = "<<x<<endl;
    #define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
    #define lson l,mid,ls
    #define rson mid+1,r,rs
    #define ls (rt<<1)
    #define rs ((rt<<1)|1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int loveisblue = 486;
    const int maxn = 500086;
    const int maxm = 100086;
    const int inf = 0x3f3f3f3f;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    
    int gcd[maxn<<2];
    int a[maxn];
    void build(int l,int r,int rt){
        if(l==r){
            gcd[rt]=a[l];
            return;
        }
        int mid = (l+r)>>1;
        build(lson);
        build(rson);
        gcd[rt]=__gcd(gcd[ls],gcd[rs]);
    }
    
    void update(int l,int r,int rt,int pos,int val){
        if(l==r){
            gcd[rt]=val;
            return;
        }
        int mid = (l+r)>>1;
        if(pos<=mid)update(lson,pos,val);
        else update(rson,pos,val);
        gcd[rt]=__gcd(gcd[ls],gcd[rs]);
    }
    
    int query(int l,int r,int rt,int L,int R,int val){
        int  ans = 0;
    
        if(L<=l&&R>=r&&gcd[rt]%val==0){
            return 0;
        }
        if(l==r){
            return 1;
        }
        int mid = (l+r)>>1;
        if(L<=mid){
            ans+=query(lson,L,R,val);
        }
        if(ans>=2){return ans;}
        if(R>mid){
            ans+= query(rson,L,R,val);
        }
        return  ans;
    
    }
    
    int main() {
        ios::sync_with_stdio(true);
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
    #endif
    
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        build(1,n,1);
        int q;
        scanf("%d",&q);
        while (q--){
            int type;
            scanf("%d",&type);
            if(type==1){
                int l,r;
                scanf("%d%d",&l,&r);
                int p;
                scanf("%d",&p);
                if(query(1,n,1,l,r,p)<=1){
                    printf("YES
    ");
                }else{
                    printf("NO
    ");
                }
            }else{
                int pos,val;
                scanf("%d",&pos);
                scanf("%d",&val);
                update(1,n,1,pos,val);
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    sqlserver 中数据类型bit的使用 Followyour
    如何在保证睡眠的情况把各种事情做好
    求职具备八大素质
    提高效率的5条黄金法则
    随笔摘录:世上只有一件东西,能始终经受住生活的冲击:一颗宁静的心。
    Hadoop中文文档 (0.19.0)
    两个微型的Map/Reduce框架: FileMap(FM)和BashReduce
    Hadoop的商业化支持
    Yahoo的Hadoop版本
    Map/Reduce and Queues for MySQL Using Gearman
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/11455197.html
Copyright © 2011-2022 走看看