2019-ACM-ICPC- Xuzhou station network game - I. query- + tree dimensional array partial ordering

2019-ACM-ICPC- Xuzhou station network game - I. query- + tree dimensional array partial ordering


【Problem Description】

You give a \ ([1, n] \ ) is arranged, the query \ ([l, r] \ ) how much of the interval \ ((i, j) \ ) satisfies \ (l \ le i <j \ R & lt Le \) , and \ (min (P_i, p_j) = GCD (P_i, p_j) \) .

【Solution】

All interrogation zone by the right end of small to large, for each of the sorted query, the \ ([1, r] \ ) in all of the array satisfy the condition inserted into the tree, and then to the query interval size. ( \ ([. 1, R & lt] \) all that satisfy the condition number as \ ([1, i) \ ) in \ (a [i] \) divisors or multiples thereof, where \ (i \ in [1 , r] \) )

Pre-emphasis, the number of permutations for each \ (P_i \) , multiple enumerate, find the location of the number of occurrences of \ (POS \) , if the \ (POS> I \) , then \ (G [POS] .push \ _back (i) \) , or \ (G [i] .push \ _back (POS) \) . That \ (g [x] \) is stored is \ ([1, x) \ ) in (a [x] \) \ position where the divisor and multiples.


【Code】

/*
 * @Author: Simon 
 * @Date: 2019-09-10 21:25:49 
 * @Last Modified by: Simon
 * @Last Modified time: 2019-09-10 22:06:20
 */
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 200005
int tree[maxn],ans[maxn];
vector<int>g[maxn]; //[1,x)中a[x]的约数或倍数所在的位置
vector<pair<int,int> >q[maxn]; //右端点为r的所有查询
int pos[maxn],a[maxn];
inline int lowbit(int x){
    return x&(-x);
}
inline void update(int x,int val){
    for(int i=x;i<maxn;i+=lowbit(i)){
        tree[i]+=val;
    }
}
inline int query(int x){
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i)){
        ans+=tree[i];
    }
    return ans;
}
Int main(){
#ifndef ONLINE_JUDGE
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];pos[a[i]]=i;
    }
    for(int i=1;i<=n;i++){
        for(int j=a[i]*2;j<=n;j+=a[i]){
            int x=i,y=pos[j];
            if(x<y) swap(x,y);
            g[x].push_back(y);
        }
    }
    for(int i=1;i<=m;i++){
        int l,r;cin>>l>>r;
        q[r].push_back({l,i});//按右端点分类
    }
    for(int i=1;i<=n;i++){
        for(auto v:g[i]) update(v,1); //将小于i的满足条件的数都插入树状数组中
        for(auto v:q[i]) ans[v.second]=query(i)-query(v.first-1); //查询
    }
    for(int i=1;i<=m;i++) cout<<ans[i]<<endl;
#ifndef ONLINE_JUDGE
    cout<<endl;system("pause");
#endif
    return 0;
}

Guess you like

Origin www.cnblogs.com/--Simon/p/11506065.html
Recommended