HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
2013-11-09 22:33
288 查看
Rabbit Kingdom
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 40 Accepted Submission(s): 20
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For every query, output one line indicating the answer.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
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] .
[align=left]Source[/align]
2013 Asia Hangzhou Regional Contest
关键是在预处理,每个数预处理出L,R区间,表示左右和这个数不互质的位置。
这个只要从左到右和从右到左扫描一遍,分解质因素,找下一个质因素的位置。
然后对于每个查询进行离线处理,按照右端点排序。
遇到i,在L处+1, 遇到R,在i处+1,在L处-1.
/* *********************************************** Author :kuangbin Created Time :2013-11-9 14:38:41 File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1008.cpp ************************************************ */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int MAXN = 200010; int prime[MAXN+1]; void getPrime() { memset(prime,0,sizeof(prime)); for(int i = 2;i <= MAXN;i++) { if(!prime[i])prime[++prime[0]] = i; for(int j = 1;j <= prime[0] && prime[j] <= MAXN/i;j++) { prime[prime[j]*i] = 1; if(i % prime[j] == 0)break; } } } long long factor[100][2]; int fatCnt; int getFactors(long long x) { fatCnt = 0; long long tmp = x; for(int i = 1;prime[i] <= tmp/prime[i];i++) { factor[fatCnt][1] = 0; if(tmp % prime[i] == 0) { factor[fatCnt][0] = prime[i]; while(tmp % prime[i] == 0) { factor[fatCnt][1]++; tmp /= prime[i]; } fatCnt++; } } if(tmp != 1) { factor[fatCnt][0] = tmp; factor[fatCnt++][1] = 1; } return fatCnt; } int L[MAXN],R[MAXN]; int a[MAXN]; int b[MAXN]; int n,m; int lowbit(int x) { return x & (-x); } int c[MAXN]; void add(int i,int val) { if(i == 0)return; while(i <= n) { c[i] += val; i += lowbit(i); } } int sum(int i) { int s = 0; while(i > 0) { s += c[i]; i -= lowbit(i); } return s; } vector<int>vec[MAXN]; struct Node { int l,r; int index; void input() { scanf("%d%d",&l,&r); } }; bool cmp(Node p1,Node p2) { return p1.r < p2.r; } Node node[MAXN]; int ans[MAXN]; int pp[MAXN][15]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); getPrime(); while(scanf("%d%d",&n,&m) == 2) { if(n == 0 && m == 0)break; for(int i = 1;i <= n;i++) scanf("%d",&a[i]); for(int i = 0;i < m;i++) { node[i].input(); node[i].index = i; } for(int i = 1;i < MAXN;i++)b[i] = n+1; for(int i = n;i >= 1;i--) { getFactors(a[i]); R[i] = n+1; pp[i][0] = fatCnt; for(int j = 0;j < fatCnt;j++) { R[i] = min(R[i],b[factor[j][0]]); b[factor[j][0]] = i; pp[i][j+1] = factor[j][0]; } } for(int i = 1;i < MAXN;i++)b[i] = 0; for(int i = 1;i <= n;i++) { //getFactors(a[i]); L[i] = 0; fatCnt = pp[i][0]; for(int j = 0;j < fatCnt;j++) { factor[j][0] = pp[i][j+1]; L[i] = max(L[i],b[factor[j][0]]); b[factor[j][0]] = i; } } sort(node,node+m,cmp); memset(c,0,sizeof(c)); for(int i = 1; i <= n+1;i++) { c[i] = 0; vec[i].clear(); } for(int i = 1;i <= n;i++)vec[R[i]].push_back(i); int id = 1; for(int i = 0;i < m;i++) { while(id <= n && id <= node[i].r) { add(L[id],1); int sz = vec[id].size(); for(int j = 0;j < sz;j++) { int v = vec[id][j]; add(L[v],-1); add(v,1); } id++; } ans[node[i].index] = sum(node[i].r) - sum(node[i].l-1); ans[node[i].index] = node[i].r - node[i].l +1 - ans[node[i].index]; } for(int i = 0;i < m;i++)printf("%d\n",ans[i]); } return 0; }
相关文章推荐
- HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
- HDU 4777 Rabbit Kingdom(树状数组+离线预处理)
- hdu 4777 2013ACM/ICPC杭州现场赛 H题 Rabbit Kingdom
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
- hdu 4776 Ants (2013 杭州 现场赛) 树上预处理 trie树
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
- HDU 4777 Rabbit Kingdom(树状数组+离线处理+尺取法)
- Hdu 4777 Rabbit Kingdom 树状数组
- hdu 4738 2013杭州赛区网络赛 桥+重边+连通判断 ***
- hdu 4777 Rabbit Kingdom (2013acmicpc亚洲区域赛杭州站 H)
- HDU 4778 2013 ACM/ICPC 杭州赛区现场赛 I. Gems Fight!
- hdu 4578 Transformation 2013ACM-ICPC杭州赛区全国邀请赛
- hdu 4777 Rabbit Kingdom(离线+树状数组)
- hdu 4576 robot 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现-1001-robot
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)
- HDU 4777 Rabbit Kingdom 树状数组
- hdu 4739 2013杭州赛区网络赛 寻找平行坐标轴的四边形 **
- hdu 4771 Stealing Harry Potter's Precious (2013 杭州 onsite B题)bfs预处理后,dfs枚举或tsp
- hdu 4777 Rabbit Kingdom 离线树状数组
- hdu 4584 building-bridge 暴力的题,怎么暴力怎么干。。2013 ACM-ICPC杭州赛区全国邀请赛——题目重现