2019icpc Xuzhou network game _I_query

The meaning of problems

Given a sequence, repeatedly asked interval \ ([l, r] \ ) satisfies \ (min (a [i] , a [j]) == gcd (a [i], a [j]) \) the number of \ ((i, j) \ ) number.

analysis

  • In fact, there are several multiples of the interval required for the number.
  • Since the sequence is all arranged, all have several multiples of the number only \ (nlogn \) months, so you can find all the violence of a few, and then ask questions offline, into two partial order, the use of Fenwick tree to solve It can be.
  • Fenwick tree is actually seeking to reverse seeking \ (i <j \ & \ & a [i]> a [j] \) is a two-dimensional partial order, and in this title where demand is \ (l [i] <l [j] \ & \ & r [i]> r [j] \) of the two-dimensional partial order, where \ (l [i], r [i] \) is asked, the dimension of the first sort the method of seeking the number of inversions can be calculated as a tree array.

Code

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+50;
int n,m,l,r,c[N],a[N],p[N],ans[N];
struct node{
    int id,l,r;
    bool operator<(const node& rhs)const{
        if(r==rhs.r){
            return l<rhs.l;
        }else{
            return r<rhs.r;
        }
    }
};
vector<node> ns,ps;
int lowbit(int x){
    return x&(-x);
}
void add(int i,int x){
    while(i<=n){
        c[i]+=x;
        i+=lowbit(i);
    }
}
int sum(int i){
    int ans=0;
    while(i){
        ans+=c[i];
        i-=lowbit(i);
    }
    return ans;
}
int main(){
    // freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        p[a[i]]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=i+i;j<=n;j+=i){
            int a=p[i],b=p[j];
            if(a>b){
                swap(a,b);
            }
            ps.push_back({0,a,b});
        }
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d",&l,&r);
        ns.push_back(node{i,l,r});
    }
    sort(ns.begin(),ns.end());
    sort(ps.begin(),ps.end());
    int j=0;
    int sz=ps.size();
    for(int i=0;i<m;i++){
        while(j<sz && ps[j].r<=ns[i].r){
            add(ps[j].l,1);
            j++;
        }
        ans[ns[i].id]=j-sum(ns[i].l-1);
    }
    for(int i=1;i<=m;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zxcoder/p/11496992.html