Hdu 5869 Interval different gcd number (tree-like array)

Source: Internet
Author: User
Tags gcd greatest common divisor

Different GCD subarray Query

Time limit:6000/3000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): 221 Accepted Su Bmission (s): 58

problem DescriptionThis was a simple problem. The teacher gives Bob a list of problems about GCD (greatest Common Divisor). After studying some of them, Bob thinks that GCD are so interesting. One day, he comes up and a new problem about GCD. Easy as it looks, Bob cannot figure it out himself. Now he turns to the problem:
  
Given an arrayaOfNPositive integersa 1, a2 ,? an−1, an ; A subarray ofais defined as a continuous interval betweena1 andaN . In other words,AI,ai+1,< Span id= "mathjax-span-56" class= "Mo"? , Aj−1,a j is a subarray ofa, for1≤i≤j≤N . For a query in the form (l,R< Span id= "mathjax-span-87" class= "Mo" >) , tell the number of different GCDs contributed by all subarrays of the Interval [l,r .
    InputThere is several tests, process till the end of input.
  
For each test, the first line consists of integersNandQ, denoting the length of the array and the number of queries, respectively.NPositive integers is listed in the second line, followed byQLines each containing, integersL,R For a query.

Can assume that

1≤N,Q≤100000

1≤ai≤1000000  Outputfor each query, output the answer on one line. Sample Input5 to 3 4 6 for 5 Sample Output666

/*hdu 5869 Interval different gcd number (tree array) problem: give you a group of numbers, and then a Q query. Q. How many (different) solve are in total for all interval gcd in [l,r]: It feels like a line tree/tree array. Because there are many problems that are enumerated from small to large processing of the query R.  In this case, only maintenance [1,i] is required to start with the set record generated by the GCD and then recursion, timed out. Because the interval gcd is monotonic. (i-1->1) and I interval gcd are decremented. And with the RMQ can be O (1) of the query [I,J]GCD value. If the enumeration [1,i-1] feels very troublesome. So we skip the same part of the middle GCD value with two points, That is, the interval gcd value of the query and I is the leftmost endpoint of X. Because of the need for different values, it reminds me of the number of different numbers in [L,r] using a line segment tree (forgetting which one is the ZZ). For I, first find the position closest to I so that the value of GCD is x. Then compare it with the previous position.  Maintain this position to the right as much as possible. Assume that the gcd of [3,i-1] GCD is 3,[2,i] is 3. Then add 1 to position 3. Because as long as [L,i] contains this point, then there will be a value of 3. hhh-2016-09-10 19:01:57*/#include <algorithm> #include <iostream># Include <cstdlib> #include <stdio.h> #include <cstring> #include <vector> #include <math.h > #include <queue> #include <set> #include <map> #define LL long longusing namespace Std;const int MAXN    = 100100;int a[maxn];map<ll,int>mp;struct node{int l,r; int id;}    P[maxn];bool tocmp (node A,node b) {if (A.R! = B.R) return A.R < B.R; if (a.l! = B.L) return A.L < B.L;} ll GCD (ll A,ll b) {if (b==0) RetuRN A; else return GCD (b,a%b);} int lowbit (int x) {return x& (-X);}    ll out[maxn];ll siz[maxn];int n;void Add (int x,ll val) {if (x <= 0) return;        while (x <= N) {siz[x] + = val;    x + = Lowbit (x);    }}LL sum (int x) {if (x <=0) return 0;    ll cnt = 0;        while (x > 0) {cnt + siz[x];    X-= Lowbit (x); } return CNT;}    int dp[maxn][40];int m[maxn];int RMQ (int x,int y) {int t = m[y-x+1]; Return GCD (dp[x][t],dp[y-(1<<t) +1][t]);}    void inirmq (int n,int c[]) {m[0] =-1;        for (int i = 1; I <= n; i++) {M[i] = ((i& (i-1)) = = 0)? M[i-1]+1:m[i-1];    Dp[i][0] = C[i]; } for (int j = 1; J <= M[n]; j + +) {for (int i = 1; i+ (1<<j)-1 <= N; i++) dp[i][j] = GCD    (Dp[i][j-1],dp[i+ (1<< (j-1))][j-1]);    }}void init () {mp.clear ();    memset (siz,0,sizeof (siz)); INIRMQ (n,a);}    int main () {int qry; while (scanf ("%d", &n)! = EOF) {scanf ("%d", &AMp;qry);        for (int i = 1; i<=n; i++) {scanf ("%d", &a[i]);        } init ();        for (int i = 0; i < qry; i++) {scanf ("%d", &AMP;P[I].L), scanf ("%d", &AMP;P[I].R), p[i].id = i;        } int ta = 0;        Sort (p, p+qry, tocmp);            for (int i = 1; I <= n; i++) {int thea = a[i];            int j = i;                while (J >= 1) {int tmid = j;                int L = 1,r = j;                    while (L <= r) {int mid = (l+r) >> 1;                        if (L = = R && RMQ (mid,i) = = Thea) {Tmid = mid;                    Break                    } if (RMQ (mid,i) = = Thea) r = Mid-1,tmid = Mid;                else L = mid+1;                } if (!mp[thea]) Add (j,1); else if (mp[thEA] < J && Mp[thea]) {Add (mp[thea],-1);                Add (j,1);                } Mp[thea] = j;                j = tmid-1;            if (J >= 1) Thea = RMQ (j,i); } while (Ta < qry && P[TA].R = i) {out[p[ta].id] = SUM (P[TA].R)-SUM (p[t                A].L-1);            ta++;    }} for (int i = 0; i < qry; i++) printf ("%i64d\n", Out[i]); } return 0;}

  

Hdu 5869 Interval different gcd number (tree-like array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.