HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)
Rabbit Kingdom
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40 Accepted Submission(s): 20
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.
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.
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
1
1
3
1
2
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] .
关键是在预处理,每个数预处理出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 = ;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i % prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ;prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp % prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp % prime[i] == )
{
factor[fatCnt][]++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
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 == )return;
while(i <= n)
{
c[i] += val;
i += lowbit(i);
}
}
int sum(int i)
{
int s = ;
while(i > )
{
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][];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
getPrime();
while(scanf("%d%d",&n,&m) == )
{
if(n == && m == )break;
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
for(int i = ;i < m;i++)
{
node[i].input();
node[i].index = i;
}
for(int i = ;i < MAXN;i++)b[i] = n+;
for(int i = n;i >= ;i--)
{
getFactors(a[i]);
R[i] = n+;
pp[i][] = fatCnt;
for(int j = ;j < fatCnt;j++)
{
R[i] = min(R[i],b[factor[j][]]);
b[factor[j][]] = i;
pp[i][j+] = factor[j][];
}
}
for(int i = ;i < MAXN;i++)b[i] = ;
for(int i = ;i <= n;i++)
{
//getFactors(a[i]);
L[i] = ;
fatCnt = pp[i][];
for(int j = ;j < fatCnt;j++)
{
factor[j][] = pp[i][j+];
L[i] = max(L[i],b[factor[j][]]);
b[factor[j][]] = i;
}
}
sort(node,node+m,cmp);
memset(c,,sizeof(c));
for(int i = ; i <= n+;i++)
{
c[i] = ;
vec[i].clear();
}
for(int i = ;i <= n;i++)vec[R[i]].push_back(i);
int id = ;
for(int i = ;i < m;i++)
{
while(id <= n && id <= node[i].r)
{
add(L[id],);
int sz = vec[id].size();
for(int j = ;j < sz;j++)
{
int v = vec[id][j];
add(L[v],-);
add(v,);
}
id++;
}
ans[node[i].index] = sum(node[i].r) - sum(node[i].l-);
ans[node[i].index] = node[i].r - node[i].l + - ans[node[i].index];
}
for(int i = ;i < m;i++)printf("%d\n",ans[i]); }
return ;
}
HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)的更多相关文章
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- HDU 4777 Rabbit Kingdom
素因子分解,树状数组.$ACM/ICPC$ $2013$杭州区域赛$H$题. 首先需要处理出数字$a[i]$左边最远到$L[i]$,右边最远到$R[i]$区间内所有数字都与$a[i]$互质. 那么对于 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
Gems Fight! Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others)T ...
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...
- HDU 4777 Rabbit Kingdom 树状数组
分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...
随机推荐
- Android官方数据绑定框架DataBinding
数据绑定框架给我们带来了更大的方便性,以前我们可能需要在Activity里写很多的findViewById,烦人的代码也增加了我们代码的耦合性,现在我们马上就可以抛弃那么多的findViewById. ...
- [Golang] 一个简易代理池
晚上写了一个代理池,就是在一个代理网站上爬取代理ip和端口以及测试是否可用.接下来可能考虑扩展成一个比较大的 golang实现的代理池. 简易版代码: package main import ( &q ...
- Flex debug版本浏览器选定问题
原来都用IE进行调试的,今天安装了火狐浏览器,结果出现调试器找不到的错误,如下图 需要做下面设置 "窗口"—>"首选参数",下图位置勾选项改为IE,问题就 ...
- Ggoogle Protocol Buffer的使用 (基于C++语言)
首先说明的是Protocol Buffle是灵活高效的.它的一个很好的优点(很重要的,我认为)就是后向兼容性--当我们扩展了了.proto文件后,我们照样可以用它来读取之前生成的文件. 之前已经写了关 ...
- NPM 使用介绍
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
- D3.js 更自由的条形图
一.添加一个矩形 //Width and height var w = 500; var h = 100; var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, ...
- C++ Primer 第九章 顺序容器
由于书籍上写的已经很经典了,故大部分用图片的形式来阐述概念,代码纯手打进行验证. 1.顺序容器类型:vector.deque.list.forword_list.array.string. 2.顺序容 ...
- 前端AJAX传递数组给Springmvc接收处理
前端传递数组后端(Spring)来接收并处理: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- Request获取URL地址相应方法
以项目为BBS为例,以下代码置于modify.jsp: 1.request.getLocalName(): akiradunn 2.request.getServerName(): localhost ...
- JavaWeb基础: 获取资源文件
Web工程在编译构建完毕以后,需要部署到Tomcat上运行,资源的硬盘路径也会随着改变.要想对资源文件进行读写操作需要获取其硬盘地址,在Web工程中通常通过ServletContext/ClassLo ...