• CF474F Ant colony(ST表+二分)


    // Problem: CF474F Ant colony
    // Contest: Luogu
    // URL: https://www.luogu.com.cn/problem/CF474F
    // Memory Limit: 250 MB
    // Time Limit: 1000 ms
    // 
    // Powered by CP Editor (https://cpeditor.org)
    
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
      
    inline void out(ll x){
        if (x < 0) x = ~x + 1, putchar('-');
        if (x > 9) out(x / 10);
        putchar(x % 10 + '0');
    }
      
    inline void write(ll x){
        if (x < 0) x = ~x + 1, putchar('-');
        if (x > 9) write(x / 10);
        putchar(x % 10 + '0');
    }
      
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b,ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a%p ;
            a = a * a %p;
            b >>= 1;
        }
        return res;
    }
    
    const int inf = 0x3f3f3f3f;
    const int maxn=1e5+7,maxm=210000;
    
    ll gcd(ll a,ll b){
    	return b==0?a:gcd(b,a%b);
    }
    
    ll a[maxn],n,dp[maxn][32],b[maxn];
    vector<int>g[maxn]; 
    void init(){
    	rep(j,1,n) dp[j][0]=a[j];
    	for(int i=1;(1<<i)<=n;i++){
            for(int j=1;j+(1<<i)-1<=n;j++){
                 dp[j][i]=gcd(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);
            }
        }
    }
    ll query(int l,int r){
        int k=(int)log2((r-l+1));
        return gcd(dp[l][k],dp[r-(1<<k)+1][k]);
    }
    map<ll,ll>mp;
    
    int main(){
    	n=read;
    	rep(i,1,n) a[i]=read,b[i]=a[i];
    	sort(b+1,b+1+n);
    	int m=unique(b+1,b+1+n)-(b+1);
    	rep(i,1,n){
    		int x=lower_bound(b+1,b+1+m,a[i])-b;
    		mp[a[i]]=x;
    		g[x].push_back(i);
    	}
    	init();
    	int q=read;
    	while(q--){
    		int l=read,r=read;
    		ll t=query(l,r);
    		ll x=mp[t];
    		ll ans=upper_bound(g[x].begin(),g[x].end(),r)-upper_bound(g[x].begin(),g[x].end(),l-1);
    		cout<<r-l+1-ans<<"
    ";
    	}
    	
    	
    	
    	
    	return 0;
    }
    
    
  • 相关阅读:
    .NET的委托和匿名函数应用一例
    C#中,变量前的@符号
    ExtJs中多个form情况下指定某个form使能
    【Python web 开发】个人中心-用户收藏功能
    【Python web 开发】用户个人信息修改
    【Python web 开发】django rest framwork 动态设置serializers
    【Python web 开发】django rest framwork动态设置权限premission
    【fiddler】用fiddler 拦截请求修改response 回包测试
    【Python web 开发】联合唯一索引
    【Python web 开发】用户收藏功能
  • 原文地址:https://www.cnblogs.com/OvOq/p/15089376.html
Copyright © 2020-2023  润新知