标签:
3 2 2 1 4 1 2 1 3 6 4 3 6 1 2 5 3 1 3 4 6 4 4 2 6 0 0
2 1 1 3 1 2
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6 using namespace std; 7 const int maxn = 200010; 8 struct QU { 9 int L,R,id; 10 bool operator<(const QU &rhs)const { 11 return L < rhs.L; 12 } 13 } Q[maxn]; 14 int C[maxn],L[maxn],R[maxn],nxt[maxn],d[maxn],ans[maxn],n,m; 15 vector<int>fac[maxn]; 16 vector<int>Ls[maxn]; 17 void init() { 18 for(int i = 2; i <= 200000; ++i) 19 for(int j = i; j <= 200000; j += i) 20 fac[j].push_back(i); 21 } 22 int sum(int i,int ret = 0) { 23 while(i > 0) { 24 ret += C[i]; 25 i -= i&-i; 26 } 27 return ret; 28 } 29 void add(int i,int val) { 30 while(i <= n) { 31 C[i] += val; 32 i += i&-i; 33 } 34 } 35 void solve() { 36 for(int i = 0; i <= n; ++i) Ls[i].clear(); 37 sort(Q,Q + m); 38 memset(C,0,sizeof C); 39 memset(L,0,sizeof L); 40 memset(R,0,sizeof R); 41 memset(nxt,0,sizeof nxt); 42 for(int i = 1; i <= n; ++i) { 43 for(int j = 0; j < fac[d[i]].size(); ++j) { 44 int p = fac[d[i]][j]; 45 if(!nxt[p]){ 46 nxt[p] = i; 47 continue; 48 } 49 if(!R[nxt[p]]) R[nxt[p]] = i; 50 if(L[i] < nxt[p]) L[i] = nxt[p]; 51 nxt[p] = i; 52 } 53 Ls[L[i]].push_back(i); 54 } 55 for(int i = 0,cur = 0; i <= n; ++i){ 56 while(Q[cur].L == i){ 57 ans[Q[cur].id] = sum(Q[cur].R) - sum(Q[cur].L - 1); 58 ++cur; 59 } 60 for(int j = 0; j < Ls[i].size(); ++j){ 61 add(Ls[i][j],1); 62 if(R[Ls[i][j]]) add(R[Ls[i][j]],-1); 63 } 64 if(R[i]) add(R[i],1); 65 } 66 for(int i = 0; i < m; ++i) 67 printf("%d\n",ans[i]); 68 } 69 int main() { 70 init(); 71 while(scanf("%d%d",&n,&m),n||m) { 72 for(int i = 1; i <= n; ++i) scanf("%d", d + i); 73 for(int i = 0; i < m; ++i) { 74 scanf("%d%d",&Q[i].L,&Q[i].R); 75 Q[i].id = i; 76 } 77 solve(); 78 } 79 return 0; 80 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4848654.html