zoukankan      html  css  js  c++  java
  • cf-914D-线段树

    http://codeforces.com/contest/914/problem/D

     题目大意是给出一个数列,进行两种操作,一个是将位置i的数置为x,另一个操作是询问[l,r]内的数的gcd是不是x,我们可以至多更改这个区间内的一个数。

    线段树维护区间gcd,询问的时候统计有多少个数不是x的倍数,当数量大于1的时候就可以直接return了,因为答案显然已经是NO了,没这一句的话会T。

      

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 #define LL long long  
     4 #define ULL unsigned long long 
     5 const int maxn=500050;
     6 int gcd(int a,int b){
     7     return b==0?a:gcd(b,a%b);
     8 }
     9 struct sg_tree{
    10     #define mid ((L+R)>>1)
    11     #define lc (id<<1)
    12     #define rc (id<<1|1)
    13 
    14     int g[maxn<<2],N;
    15     void build(int id,int L,int R){
    16         if(L==R){
    17             scanf("%d",&g[id]);
    18             return;
    19         }
    20         build(lc,L,mid);
    21         build(rc,mid+1,R);
    22         g[id]=gcd(g[lc],g[rc]);
    23     }
    24 
    25     void change(int id,int L,int R,int tar,int v){
    26         if(L==R){
    27             g[id]=v;
    28             return;
    29         }
    30         if(tar<=mid) change(lc,L,mid,tar,v);
    31         else change(rc,mid+1,R,tar,v);
    32         g[id]=gcd(g[lc],g[rc]);
    33     }
    34 
    35     void ask(int id,int L,int R,int l,int r,int x,int &tot){
    36         if(tot>1) return; 
    37         if(L==R){
    38             if(g[id]%x!=0) tot++;
    39             return;
    40         }
    41         if(l<=mid){
    42             if(g[lc]%x!=0){
    43                 ask(lc,L,mid,l,r,x,tot);
    44             }
    45         }
    46         if(r>mid){
    47             if(g[rc]%x!=0){
    48                 ask(rc,mid+1,R,l,r,x,tot);
    49             }
    50         }
    51     }
    52 }ac;
    53 int main(){
    54     scanf("%d",&ac.N);
    55     ac.build(1,1,ac.N);
    56     int op,m,l,r,x,i;
    57     scanf("%d",&m);
    58     while(m--){
    59         scanf("%d",&op);
    60         if(op==1){
    61             int ok=0;
    62             scanf("%d%d%d",&l,&r,&x);
    63             ac.ask(1,1,ac.N,l,r,x,ok);
    64             ok<=1?puts("YES"):puts("NO");
    65         }
    66         else{
    67             scanf("%d%d",&i,&x);
    68             ac.change(1,1,ac.N,i,x);
    69         }
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    Unity3D-光照贴图技术
    登岳麓山
    第一个OC程序
    Unity3D之碰撞体,刚体
    TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement
    QQ互联登录回调路径错误redirect uri is illegal(100010)
    Quartz.Net使用
    C# 文件相关操作
    微信扫码支付模式一和模式二的区别
    ankhSVN安装后,VS2010使用
  • 原文地址:https://www.cnblogs.com/zzqc/p/9897344.html
Copyright © 2011-2022 走看看