query
I don't treat tree arrays very well. Tree arrays work well when considering offline + region summation.
Title:
Give a permutation ppp, give QQQQ questions, and each time ask [l,r][l,r][l,r] the number of pairs satisfying GCD (i, j) == min (i, j) (i < j) GCD (i, j) == min (i, j) (i < j) GCD (i, j) == min (i, j) (i < j) = pairs.
Train of thought:
The query is offline, traversing the array P P P from left to right, traversing to p[i]p[i]p[i], counting the newly emerging pair pairs, and adding the left endpoint to the tree array.
Then process queries about iii with the right endpoint.
Preprocess each pair pair, no more than NlogNlogNlogN.
/* Author : Rshs * Data : 2019-09-18-19.28 */ #include<bits/stdc++.h> using namespace std; #define FI first #define SE second #define LL long long #define MP make_pair #define PII pair<int,int> #define SZ(a) (int)a.size() const double pai = acos(-1); const double eps = 1e-10; const LL mod = 1e9+7; const int MX = 1e6+5; int a[MX]; vector<PII>qu[MX]; int id[MX]; int bit[MX]; int ans[MX]; vector<int>v[MX]; void upd(int p,int c,int nn){ while(p<=nn) bit[p]+=c,p=p+(p&-p); } int qur(int p){ int re=0; while(p>0) re+=bit[p],p=p-(p&-p); return re; } int main(){ int n,m;cin>>n>>m; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++){ int sl,sr;scanf("%d%d",&sl,&sr); qu[sr].push_back(MP(sl,i)); } for(int i=1;i<=n;i++){ for(int j=i*2;j<=n;j=j+i){ v[j].push_back(i);//j factor v[i].push_back(j);//Multiple of j } } for(int i=1;i<=n;i++){ for(auto j:v[a[i]])if(id[j])upd(id[j],1,n); id[a[i]]=i; for(auto z:qu[i]){ ans[z.SE]=( qur(i)-qur(z.FI-1) ); } } for(int i=1;i<=m;i++)cout<<ans[i]<<'\n'; return 0; }