Total passenger 41391.query- garlic dimensional partial order + Fenwick tree (pre-out to meet the conditions gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019 Nian Xuzhou network game)

query

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 rli<jr 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(1n105), m(1 \le m \le 10^5)m(1m105) 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(1lirin), 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).

Sample input

3 2
1 2 3
1 3
2 3

Sample Output

2
0

 

Meaning of the questions are well understood. + Partial order is a two-dimensional array of tree.

His teammates when the game code written ugly, learn about other people's code and write well.

Reference links: Query (2019 Xuzhou network game) (+ Fenwick tree a little thought)

 

Code:

. 1  // I. Partial Order + tree-dimensional array
 2  // pretreatment out all multiples of advance, then in sequence, and then inserting the first query 
. 3 #include <bits / STDC ++ H.>
 . 4  the using  namespace STD;
 . 5 typedef Long  Long LL;
 . 6  #define Pb push_back
 . 7  #define MP the make_pair
 . 8  #define PII pair <int, int>
 . 9  const  int MAXN 1E5 + = 10 ;
 10  
. 11  int A [MAXN], POS [MAXN], Tree [MAXN], ANS [MAXN];
 12 is  int n-, m;
 13 is Vector < int > gcd[maxn];
14 vector<pii> op[maxn];
15 
16 int lowbit(int x)
17 {
18     return x&(-x);
19 }
20 
21 void add(int x,int val)
22 {
23     for(int i=x;i<=n;i+=lowbit(i)){
24         tree[i]+=val;
25     }
26 }
27 
28 int query(int n)
29 {
30     int ans=0;
31     for(int i=n;i>0;i-=lowbit(i)){
32         ans+=tree[i];
33     }
34     return ans;
35 }
36 
37 int main()
38 {
39     scanf("%d%d",&n,&m);
40     for(int i=1;i<=n;i++){
41         scanf("%d",&a[i]);
42         pos[a[i]]=i;//记录下标
43     }
44     for(int i=1;i<=n;i++){
45         for(int j=a[i]*2;j<=n;j+=a[i]){
46             gcd[max(i,pos[j])].pb(min(i,pos[j]));//预处理
47         }
48     }
49     for(int i=1;i<=m;i++){
50         int l,r;
51         scanf("%d%d", & L, & R & lt);
 52 is          OP [R & lt] .pb (MP (L, I)); // save the query 
53 is      }
 54 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 55          for (Auto IT : GCD [I]) {
 56 is              the Add (IT, . 1 ); // first insert 
57 is          }
 58          for (Auto IT: OP [I]) {
 59              ANS [it.second] = Query (I) -query (IT .first- . 1 ); // query 
60          }
 61 is      }
 62 is      for ( int I = . 1;i<=m;i++){
63         printf("%d\n",ans[i]);
64     }
65     return 0;
66 }

 

Guess you like

Origin www.cnblogs.com/ZERO-/p/11488127.html
Recommended