Analysis: (written by someone else)
For all (L, r) intervals, fixed right interval, all (Li, R) will only have a log of a different GCD value,
You can nlogn all the different gcd intervals so that the interval is Nlogn, and then for an inquiry to be processed offline,
Record the last occurrence of each different gcd using a method similar to the number of the enquiry interval, and then maintain it with a tree array
Note: I see this code will, but his nlogn pretreatment I won't, I will nlog^2n
DP[I][J] stands for the right end of I, extending the GCD of 2^j points (including i) to the left, and then, because such gcd satisfies the decrement, it is possible to find the interval in two points
Code:
/*runid:678021userid:96655submit time:2016-04-19 23:44:20language:c++length:2378 Bytes.Result:Accepted*/#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespaceStd;typedefLong LongLL;Const intinf=0x3f3f3f3f;Const intn=1e4+5;intt,n,m,dp[n][ -];structask{intL,r,id; BOOL operator< (ConstAsk &RHS)Const{ returnr<RHS.R; }}p[n*Ten];structseg{intl,r,v; BOOL operator< (ConstSeg &RHS)Const{ returnr<RHS.R; }}seg[ **N];intErfen (intPosintv) { intL=1, r=POS; while(l<R) { intMid= (l+r) >>1; intLen=pos-mid+1; intnow=pos,cur=-1; for(intI= -; i>=0;--i) { if(len& (1<<i)) { if(cur==-1) cur=Dp[now][i]; ElseCur=__GCD (Cur,dp[now][i]); now-=(1<<i); } } if(CUR<V) l=mid+1; ElseR=mid; } return(l+r) >>1;}inthash[ **n],tot,mat[ **N];intres[n*Ten];intC[n];voidAddintXintt) { for(inti=x;i<=n;i+=i& (-i)) C[i]+=t;}int Get(intx) { intans=0; if(x==0)return 0; for(intI=x;i>0;i-=i& (-i)) ans+=C[i]; returnans;}intMain () {scanf ("%d",&T); while(t--) {scanf ("%d%d",&n,&m); for(intI=1; i<=n;++i) scanf ("%d", &dp[i][0]); for(intI=1; i<=m;++i) scanf ("%d%d", &P[I].L,&P[I].R), p[i].id=i; for(intk=1;(1<<K) <=n;++k) for(intI=n;i>1;--i) { intj=i-(1<<K) +1; if(j<1) Break; J=i-(1<< (K-1)); DP[I][K]=__GCD (dp[i][k-1],dp[j][k-1]); } intCnt=0; tot=0; for(intI=1; i<=n;++i) { intlast=-1; for(intJ=i;j>0;--j) { inttmp=dp[j][0]; if(last!=-1) tmp=__GCD (tmp,last); Last=tmp; ++CNT; SEG[CNT].L=j,seg[cnt].r=i,seg[cnt].v=Last ; hash[++tot]=Last ; J=Erfen (i,last); }} sort (hash+1, hash+1+tot); Tot=unique (hash+1, hash+1+tot)-hash-1; Sort (P+1, p+1+m); Sort (seg+1, seg+1+CNT); Memset (Mat,0,sizeof(MAT)); Memset (c,0,sizeof(c)); intnow=1; for(intI=1; i<=m;++i) { for(; now<=cnt&&seg[now].r<=p[i].r;++Now ) { intPos=lower_bound (hash+1, hash+1+TOT,SEG[NOW].V)-Hash; if(seg[now].l>Mat[pos]) { if(Mat[pos]) Add (mat[pos],-1); Add (SEG[NOW].L,1); Mat[pos]=SEG[NOW].L; }} Res[p[i].id]=Get(P[I].R)-Get(p[i].l-1); } for(intI=1; i<=m;++i) printf ("%d\n", Res[i]); } return 0;}
View Code
FZU2224 an exciting GCD problem interval GCD preprocessing + Tree array