I thought a little water, O (N2) can still be, after all, only 3 test case. I don't want to die.
1#include <bits/stdc++.h>2 #defineMAX (x, y) (() > (y)? (X): (Y))3 #defineMIN (x, Y) ((×) < (y)? (X): (Y))4 #definePII pair<int,int>5 #defineINF 0x7f7f7f7f6 #defineLL Long Long7 using namespacestd;8 Const intn=10005;9 structnodeTen { One intL, R, ans; A }que[n]; - structNode1 - { the intll, RR, GCD; - Node1 () {}; -Node1 (intllintRrintGCD): LL (LL), RR (RR), GCD (GCD) {}; -}reg[n][ -]; + - int_GCD (intAintb) {returnb = =0? A: _GCD (b, a%b);} + A intSeq[n], N, Q, pre[n]; at intCnt[n]; - voidpre_cal () - { -memset (CNT,0,sizeof(CNT)); -reg[n][0]=node1 (N,n,seq[n]);//one last special treatment -cnt[n]++; in for(inti=n-1; i>=1; i--) - { to intR=i, gcd=Seq[i], tmp; + for(intj=0; j<cnt[i+1]; J + +) - { theNode1 &t=reg[i+1][j]; *tmp=_gcd (GCD, T.GCD); $ if(TMP!=GCD) reg[i][cnt[i]++]=Node1 (i, R, GCD);Panax NotoginsengGcd=tmp; -R=T.RR; the } +reg[i][cnt[i]++]=Node1 (i, R, GCD); A } the } + - voidcal () $ { $ for(intI=1; i<=n; i++) - { -pre[i-1]=0; thepre[i]=Seq[i]; - intp=0;Wuyi for(intj=i+1; j<=n; J + +) the { - if(J>REG[I][P].RR) p++; Wupre[j]=REG[I][P].GCD; -pre[j]+=pre[j-1]; About } $ for(intj=1; j<=q; J + +) - if(I>=que[j]. L && i<=Que[j]. R) -QUE[J].ANS+=PRE[QUE[J]. r]-pre[i-1]; - } A for(intI=1; i<=q; i++) printf ("%d\n", Que[i].ans); + } the intMain () - { $ //freopen ("Input.txt", "R", stdin); the intT, L, R; theCin>>T; the while(t--) the { -scanf"%d", &n); in for(intI=1; i<=n; i++) scanf ("%d", &seq[i]); the thescanf"%d", &q); About for(intI=1; i<=q; i++) the { thescanf"%d%d", &que[i]. L, &Que[i]. R); theque[i].ans=0; + } - pre_cal (); the cal ();Bayi } the the return 0; -}
tle Code
HDU 5381 The sum of gcd (tricks, MO team algorithm)