zoukankan      html  css  js  c++  java
  • [CF] 474 F. Ant colony

    区间重复不会影响GCD,ST表当然是支持的啦,常数这么小。

    学到了三个东西:

    1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围。

    2.ST表更新时存在一个i+(1<<j-1)的下标,存在极大越界隐患,以前数组开的大就无所谓,讲道理边界是要判的。

    3.及时存代码,又蓝屏了qwq

    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<map>
    using namespace std;
    
    inline int rd(){
      int ret=0,f=1;char c;
      while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
      while(isdigit(c))ret=ret*10+c-'0',c=getchar();
      return ret*f;
    }
    int LOG2 (unsigned int x) {
       static const int log_2[256] = {
         0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
         6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
         7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
         8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
       };
       int l = -1;
       while (x >= 256) { l += 8; x >>= 8; }
       return l + log_2[x];
    }
    
    const int MAXN=100005;
    
    int n,m;
    int a[MAXN];
    int f[MAXN][32];
    
    int id;
    map<int,int> M;
    vector<int> V[MAXN];
    
    int gcd(int x,int y){return y?gcd(y,x%y):x;}
    
    int query(int x,int y){
      if(x==y) return f[x][0];
      int len=LOG2(y-x+1);
      return gcd(f[x][len],f[y-(1<<len)+1][len]);
    }
    
    int main(){
      n=rd();
      for(int i=1;i<=n;i++) f[i][0]=a[i]=rd();
      for(int i=1;i<=n;i++){
        if(!M.count(a[i])) M[a[i]]=++id;
        V[M[a[i]]].push_back(i);
      }
      for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<(j-1))<=n;i++)
          f[i][j]=gcd(f[i][j-1],f[i+(1<<(j-1))][j-1]);
      m=rd();
      int x,y,g,tmp,l,r,ans;
      for(int i=1;i<=m;i++){
        x=rd();y=rd();
        g=query(x,y);
        tmp=M[g];
        r=lower_bound(V[tmp].begin(),V[tmp].end(),y)-V[tmp].begin();
        l=lower_bound(V[tmp].begin(),V[tmp].end(),x)-V[tmp].begin();
        if(r+V[tmp].begin()==V[tmp].end()||V[tmp][r]>y) r--;
        cout<<y-x+1-(r-l+1)<<endl;
      }
    
      return 0;
    }

    本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9319961.html

  • 相关阅读:
    client-go和golang源码中的技巧
    使用Prometheus监控snmp
    kubernetes client-go解析
    go 语言的一个赋值操作
    使用Prometheus监控bind9的DNS服务
    验证Prometheus alertmanager邮件发送
    JS 监听浏览器各个标签间的切换
    通过案例理解position:relative和position:absolute
    浏览器的统一指针事件:Pointer Event
    Chrome浏览器读写系统剪切板
  • 原文地址:https://www.cnblogs.com/ghostcai/p/9319961.html
Copyright © 2011-2022 走看看