HDU 5869 Different GCD subarray Query offline + tree-like array

Source: Internet
Author: User

Different GCD Subarray Query



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), 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 31 3 4 6 93 52 51 5

Sample Output666

Test Instructions :

The sequence of length n, the M query interval [L, R], the number of different GCD values of all the sub-segments within the interval.

Exercises

Fixed right Endpoint

Preprocessing different GCD values that extend to the left of each point

This value will not exceed log a.

And then the question becomes, ask you how many different GCD values are in a range, and the values are few (ask questions about how many colors a range has)

Tree-like array maintenance is available.

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<vector>using namespacestd;#pragmaComment (linker, "/stack:102400000,102400000")#defineLS i<<1#defineRS ls | 1#defineMid ((LL+RR) >>1)#definePII pair<int,int>#defineMP Make_pairtypedefLong LongLL;Const Long LongINF =1e18;Const DoublePi = ACOs (-1.0);Const intN = 1e6+Ten, M = 1e2+ One, mod = 1e9+7, INF =2e9;intN,q,a[n],ans[n];intgcdintAintb) {returnb = =0? A:GCD (b, a%b);} Vector<pii>G[n];structqq{intL,r,id; BOOL operator< (ConstQQ &a)Const {             returnA.R >R; }}q[n];intC[n],vis[n];voidUpdateintXintc) { for(intI =x; i < N; i+=i& (-i)) c[i] + =C;}intAskintx) {ints =0 ;  for(inti = x; I i-= I & (i)) s + =C[i]; returns;}intMain () { while(SCANF ("%d%d", &n,&q)! =EOF) {             for(inti =1; I <= N; ++i) scanf ("%d",&A[i]);  for(inti =0; I <= N; ++i) g[i].clear ();  for(inti =1; I <= N; ++i) {intx =A[i]; inty =i;  for(intj =0; J < g[i-1].size (); ++j) {intres = GCD (x,g[i-1][j].first); if(X! =Res)                                {G[i].push_back (MP (x, y)); X=Res; Y= g[i-1][j].second;            }} g[i].push_back (MP (x, y)); } memset (C,0,sizeof(C)); memset (Vis,0,sizeof(VIS));  for(inti =1; I <= Q; ++i) {scanf ("%d%d", &AMP;Q[I].L,&AMP;Q[I].R), q[i].id =i;} Sort (Q+1, q+q+1);  for(intR =0, i =1; I <= Q; ++i) { while(R <Q[I].R) {R++;  for(intj =0; J < G[r].size (); ++j) {intres =G[r][j].first; intids =G[r][j].second; if(Vis[res]) update (vis[res],-1); Vis[res]=IDs; Update (Vis[res],1); }} Ans[q[i].id]= Ask (R)-Ask (q[i].l-1); }             for(inti =1; I <= Q; ++i) cout<<ans[i]<<Endl; }        return 0;}

HDU 5869 Different GCD subarray Query offline + 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.