Given a permutation pp of length nn, you are asked to answer mm queries, each query can be represented as a pair (l ,r )(l,r), you need to find the number of pair(i ,j)(i,j) such that l \le i < j \le rl≤i<j≤r and \min(p_i,p_j) = \gcd(p_i,p_j )min(pi,pj)=gcd(pi,pj).
Input
There is two integers n(1 \le n \le 10^5)n(1≤n≤105), m(1 \le m \le 10^5)m(1≤m≤105) in the first line, denoting the length of pp and the number of queries.
In the second line, there is a permutation of length nn, denoting the given permutation pp. It is guaranteed that pp is a permutation of length nn.
For the next mm lines, each line contains two integer l_ili and r_i(1 \le l_i \le r_i \le n)ri(1≤li≤ri≤n), denoting each query.
Output
For each query, print a single line containing only one integer which denotes the number of pair(i,j)(i,j).
样例输入复制
3 2 1 2 3 1 3 2 3
样例输出复制
2 0
#include <bits/stdc++.h> #define lowbit(x) x&(-x) using namespace std; typedef long long ll; const int maxn = 3e6 + 10; const ll mod = 1e9 + 7; struct BIT { int n, c[maxn]; inline void init(int _n_) { memset(c, 0, sizeof(c)); n = _n_ + 10; } inline void update(int pos, int val) { while (pos <= n) { c[pos] += val; pos += lowbit(pos); } } inline int query(int pos) { int res = 0; while (pos) { res += c[pos]; pos -= lowbit(pos); } return res; } } bit; int res[maxn]; struct event { int op, l, r, id; bool operator<(const event &cur) const { if (l != cur.l)return l < cur.l; return op < cur.op; } inline void precess() { if (op == 0) { bit.update(r, 1); } else { int w = bit.query(r); if (op == 1) { res[id] += w; } else { res[id] -= w; } } } }; int n, m; int o[maxn], pos[maxn]; vector<event> e; int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif scanf("%d%d", &n, &m); for (register int i = 1; i <= n; ++i) { scanf("%d", &o[i]); pos[o[i]] = i; } bit.init(n); for (register int i = 1; i <= n; ++i) { for (register int j = i + i; j <= n; j += i) { e.emplace_back(event{0, pos[i], pos[j], 0}); } } for (register int i = 1, l, r; i <= m; ++i) { scanf("%d%d", &l, &r); --l; e.emplace_back(event{1, l, l, i}); e.emplace_back(event{1, r, r, i}); e.emplace_back(event{2, l, r, i}); e.emplace_back(event{2, r, l, i}); } sort(e.begin(), e.end()); for (auto &cur:e) { cur.precess(); } for (register int i = 1; i <= m; ++i) { printf("%d\n", res[i]); } return 0; }