2013 Asia Hangzhou Regional Contest H
Rabbit Kingdom
Time Limit: 6000/3000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 561 Accepted Submission(s): 167
又是一道看解题报告很久,没点反应的题!
就是这么做吧,预处理因子,对这些区间按l排序,然后预处理每个点的左右互质范围lf[i],ri[i];
然后,预处理求第i个点左边界也就是lf[x]对应的x存进vector<int>v[maxn];
然后从1 to n一个一个的添加判断是不是到了要求的范围区间求和;然后对每一个i对应的右边界ri[i]<=n因为左边的点在下次求和已经出边界了,左边对应的右边就不用-1了所以加回来,
然后再对每一个左边界为i的点v[i][j from 1 to size]都+1因为与左边的都互质,右边到ri[v[i][j]]就不互质了,也就是同时包括的时候就不算了所以就在v[i][j]的右边界如果<=n的话,
就也ri[v[i][j]]-1 ,最后直接输出答案,离线算法!
Problem Description
Long long ago, there was an ancient rabbit kingdom in the forest. Every rabbit in this kingdom was not cute but totally pugnacious, so the kingdom was in chaos in season and out of season. n rabbits were numbered form 1 to n. All rabbits' weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime. Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others. Please note that a rabbit would not fight with himself.
Input
The input consists of several test cases. The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries. The following line contains n integers, and the i-th integer Wi indicates the weight of the i-th rabbit. Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison. (1 <= n, m, Wi <= 200000, 1 <= L <= R <= n) The input ends with n = 0 and m = 0.
Output
For every query, output one line indicating the answer.
Sample Input
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
Sample Output
2
1
1
3
1
2
Hint
In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .Source
Recommend
1 #include <cstdio> 2 #include <vector> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 #define maxn 200010 7 int n, m, k, x, t; 8 struct node{ int l, r, pos; }lx[maxn]; 9 bool cmp(node a, node b){ return a.l < b.l; } 10 vector<int>vt[maxn], v[maxn]; 11 int a[maxn], c[maxn], lf[maxn], ri[maxn]; 12 int pre[maxn], ans[maxn]; 13 void add(int x, int d){ 14 while (x <= n){ 15 c[x] += d; 16 x += x&-x; 17 } 18 } 19 int sum(int x){ 20 int res = 0; 21 while (x > 0){ 22 res += c[x]; 23 x -= x&-x; 24 } 25 return res; 26 } 27 void solve(){ 28 memset(pre, -1, sizeof pre); 29 for (int i = 1; i <= n; i++){ 30 int near = 0; 31 for (int j = 0; j < vt[a[i]].size(); j++){ 32 int v = vt[a[i]][j]; 33 if (pre[v] != -1)near = max(near, pre[v]); 34 pre[v] = i; 35 } 36 lf[i] = near; 37 } 38 memset(pre, -1, sizeof pre); 39 for (int i = n; i >= 1; i--){ 40 int near = n + 1; 41 for (int j = 0; j < vt[a[i]].size(); j++){ 42 int v = vt[a[i]][j]; 43 if (pre[v] != -1)near = min(near, pre[v]); 44 pre[v] = i; 45 } 46 ri[i] = near; 47 } 48 for (int i = 1; i <= n; i++){ 49 if (lf[i])v[lf[i]].push_back(i); 50 else{ 51 add(i, 1); 52 if (ri[i] <= n)add(ri[i], -1); 53 } 54 } 55 k = 0; 56 for (int i = 1; i <= n; i++){ 57 while (lx[k].l == i){ ans[lx[k].pos] = sum(lx[k].r) - sum(lx[k].l - 1); k++; } 58 if (ri[i] <= n)add(ri[i], 1); 59 for (int j = 0; j < v[i].size(); j++){ 60 add(v[i][j], 1); 61 if (ri[v[i][j]] <= n)add(ri[v[i][j]], -1); 62 } 63 } 64 } 65 int main(){ 66 for (int i = 2; i <= 200000;i++) 67 for (int j = i; j <= 200000; j += i)vt[j].push_back(i); 68 while (~scanf("%d%d", &n, &m) && (n + m)){ 69 memset(c, 0, sizeof c); 70 for (int i = 1; i <= n; i++)scanf("%d", &a[i]); 71 for (int i = 0; i <= n; i++)v[i].clear(); 72 for (int i = 0; i < m; i++)scanf("%d%d", &lx[i].l, &lx[i].r), lx[i].pos = i; 73 sort(lx, lx + m, cmp); 74 solve(); 75 for (int i = 0; i < m; i++)printf("%d\n", ans[i]); 76 } 77 return 0; 78 }
浙公网安备 33010602011771号