Test instructions: Set a number I of the mass of the number is F (i), now give you an interval [l~r], the Max (F[i],f[j]) data range: 10^6
Analysis:
Preprocessing all F[i],o (NLGN), 10^6 does not time out, and then queries with O (7), the query cannot use O (n), because there are multiple queries that time out.
Interval problems reduce query time complexity is mostly similar to an interval and with two prefixes and subtract the way, the prefix and can be computed at the time of preprocessing, then the interval query is two prefixes and subtract on the line. such as Sum[l~r]=sum[r]-sum[l-1], and so on for example before what I can not remember for a moment, think up Add.
Code:
#include <iostream> #include <cstring> #include <cstdio> #include < Set> #include <vector>using namespace std;int t,vis[1000005];int l,r;int sum[1000005][10];int cnt[1000005]; void Init () {memset (vis,0,sizeof (Vis)), memset (sum,0,sizeof (sum)), memset (cnt,0,sizeof (CNT)); for (int i=2;i<= 1000000;i++) {if (!vis[i]) {for (int j=i;j<=1000000;j+=i) {vis[j]=1;cnt[j]++;}}} for (int i=2;i<=1000000;i++) {for (int j=1;j<8;j++) {if (J==cnt[i]) Sum[i][j]=sum[i-1][j]+1;else sum[i][j]=sum[ I-1][J];}}} int main () {init (); scanf ("%d", &t), while (t--) {scanf ("%d%d", &l,&r); int A[10];int Ans;memset (a,0,sizeof (A )); for (int i=1;i<8;i++) {a[i]=sum[r][i]-sum[l-1][i];} if (a[7]>=2) Ans=7;else if (a[6]>=2) Ans=6;else if (a[5]>=2) Ans=5;else if (a[4]>=2) ans=4;else if (a[3]>=2| | a[6]!=0&&a[3]!=0) Ans=3;else if (a[6]!=0&&a[4]!=0| | a[6]!=0&&a[2]!=0| | a[4]!=0&&a[2]!=0| | a[2]>=2) Ans=2;else ans=1;printf ("%d\n", ans);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
! HDU 5317 The maximum value of the GCD of a number of two numbers in an interval-preprocessing