zoukankan      html  css  js  c++  java
  • codeforces 301 D. Yaroslav and Divisors 离线+树状数组

    codeforces 301 D. Yaroslav and Divisors 离线+树状数组

    题意:

    1~n 的一个排列,m次查询,问区间里有多少对 a[i]|a[j]
    (今年某网络赛出了这个原题)

    思路:

    离线,先枚举所有答案,得到N个“答案对” <L,R>,这个复杂度显然是n + n/2 + n/3...,之后问题就变成了查询L,R有多少对<L,R>在里面,这是一个二维问题,对某一维度排序就可以将问题降维。之后树状数组搞一下就好了。

    代码:

    #include <bits/stdc++.h>
    #define LL long long
    #define pii pair<int,int>
    #define PB push_back
    #define X first
    #define Y second
    using namespace std;
    const int maxn = 3e5;
    vector<pair<int,int>> V[maxn];
    int sum[maxn],a[maxn],b[maxn],ans[maxn];
    int t,n,m,x,L,R;
    int lower_bit(int x){
        return x & (-x);
    }
    void add(int x,int y){
        while(x<maxn){
            sum[x]+=y;
            x+=lower_bit(x);
        }
    }
    int get_sum(int x){
        int ret=0;
        while(x>0){
            ret+=sum[x];
            x-=lower_bit(x);
        }
        return ret;
    }
    
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[a[i]]=i;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(a[i]*j>n)break;
                int L=i,R=b[a[i]*j];
                if(L>R)swap(L,R);
                V[L].PB({R,0});
            }
        }
        for(int i=1;i<=m;i++){
            scanf("%d%d",&L,&R);
            V[L].PB({R,i});
        }
        for(int i=1;i<=n;i++)
            sort(V[i].begin(),V[i].end());
        for(int i=n;i>=1;i--){
            for(auto o:V[i]){
                int l=i,r=o.X,op=o.Y;
                if(op==0){
                    add(r,1);
                }
                else{
                    ans[op]=get_sum(r);
                }
            }
        }
        for(int i=1;i<=m;i++){
            cout<<ans[i]<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    AL&&PY&&PWNTOOLS&&C语言
    shellshock—CVE-2014-6271
    2019Hackergame-Shell骇客
    python&C&Assembly 常见函数(持续更新)
    Linux常见的Shell命令
    服务器常见错误代码500、501、502、503、504、505
    vscode常用快捷键总结
    如何用VSCode愉快的写Python
    Git 安装配置及基本操作
    Robot Framework用法全集
  • 原文地址:https://www.cnblogs.com/zhangxianlong/p/11492674.html
Copyright © 2011-2022 走看看