HDU 4777 Rabbit Kingdom --容斥原理+树状数组
题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个。
解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了。
那么怎么求区间内与区间其他某个数不互质的数的个数(记为cnt)呢? 我们用L[i],R[i]表示在整个序列中左边与 i 最近的与 i 不互质的数的位置,R[i]表示右边的,L[i],R[i]我们可以正反扫一遍顺便分解因子,用个pos[]记录很方便地求出。那么区间内的cnt为L[i]或R[i]在区间内的 i 的个数。
令事件 A:i 的 L[i]在区间内 B:i 的 R[i]在区间内, 则答案为
即 cnt = |A|+|B|-|A∩B|
那么
事件A 记为 [L[i],i] 在区间内
事件B 记为 [i,R[i] 在区间内
事件|A∩B| 记为 [L[i],R[i]]在区间内
用三个vector分别存下三种区间。
解决区间内有多少个区间可以用离线树状数组做。
注意: sort 结构体vector时务必在结构体中内嵌比较函数,手写cmp函数再 sort(v.begin(),v.end(),cmp) 会超时。我也不知道为啥。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
#define N 200007 struct node{
int l,r,ind;
node(int _l,int _r,int _ind):l(_l),r(_r),ind(_ind){}
node(){}
bool operator<(const node &B)const{ return r<B.r; }
}Q[N];
vector<node> AB[];
int ans[][N],T[N];
int a[N],pos[N],maxi,L[N],R[N],n,m,c[N]; int cmp(node ka,node kb) { return ka.r < kb.r; }
int lowbit(int x) { return x&-x; } void modify(int x)
{
if(x <= ) return;
while(x <= n) { c[x]++; x += lowbit(x); }
} int getsum(int x)
{
int res = ;
while(x > ) { res += c[x]; x -= lowbit(x); }
return res;
} void init()
{
int i,j;
for(i=;i<=maxi;i++) pos[i] = ;
for(i=;i<=n;i++)
{
int tmp = a[i];
for(j=;j*j<=tmp;j++)
{
if(tmp%j == )
{
L[i] = max(L[i],pos[j]);
pos[j] = i;
while(tmp%j == ) tmp/=j;
}
}
if(tmp != )
{
L[i]=max(L[i],pos[tmp]);
pos[tmp]=i;
}
}
for(i=;i<=maxi;i++) pos[i] = n+;
for(i=n;i>=;i--)
{
int tmp = a[i];
for(j=;j*j<=tmp;j++)
{
if(tmp%j == )
{
R[i] = min(R[i],pos[j]);
pos[j] = i;
while(tmp%j == ) tmp/=j;
}
}
if(tmp != )
{
R[i]=min(R[i],pos[tmp]);
pos[tmp]=i;
}
}
} void GET(int k)
{
memset(c,,sizeof(c));
int i,j = ;
for(i=;i<=m;i++)
{
int L = Q[i].l;
int R = Q[i].r;
int ind = Q[i].ind;
while(j < n && AB[k][j].r <= R)
modify(AB[k][j].l), j++;
ans[k][ind] = getsum(R)-getsum(L-);
}
} int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF && n+m)
{
maxi = ;
memset(ans,,sizeof(ans));
for(i=;i<;i++) AB[i].clear();
for(i=;i<=n;i++)
{
scanf("%d",&a[i]), maxi = max(maxi,a[i]);
L[i] = , R[i] = n+;
}
init();
for(i=;i<=m;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].ind = i;
T[i] = Q[i].r-Q[i].l+;
}
sort(Q+,Q+m+);
for(i=;i<=n;i++)
{
AB[].push_back(node(L[i],i,)); //A : L[i]在区间内的数的个数
AB[].push_back(node(i,R[i],)); //B : R[i]在区间内的数的个数
AB[].push_back(node(L[i],R[i],)); //A交B
}
for(i=;i<;i++)
{
sort(AB[i].begin(),AB[i].end());
GET(i);
}
for(i=;i<=m;i++)
printf("%d\n",T[i]-ans[][i]-ans[][i]+ans[][i]); //容斥原理
}
return ;
}
HDU 4777 Rabbit Kingdom --容斥原理+树状数组的更多相关文章
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- HDU 4947 GCD Array 容斥原理+树状数组
GCD Array Time Limit: 11000/5500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 4777 Rabbit Kingdom 树状数组
分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...
- HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 5862 Counting Intersections (树状数组)
Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...
- hdu 5592 ZYB's Game 树状数组
ZYB's Game Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=55 ...
随机推荐
- 从" ThinkPHP 开发规范 "看 PHP 的命名规范和开发建议
稍稍水一篇博客,摘抄自Think PHP 的开发规范,很有引导性,我们可以将这些规范实践到原生 PHP 中. 命名规范 使用ThinkPHP开发的过程中应该尽量遵循下列命名规范: 类文件都是以.cla ...
- TCP中close和shutdown之间的区别
该图片截取自<<IP高效编程-改善网络编程的44个技巧>>,第17个技巧. 如果想验证可以写个简单的网络程序,分别用close和shutdown来断开连接,然后用tcpdum ...
- cl_gui_cfw=>dispatch
将已经触发的EVENT发送给他们各自的EVENT HANDLER,以便让这些事件得到响应. 根据返回值可以判断是否发送成功. CALL METHOD cl_gui_cfw=>dispatch ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q97-Q99)
04 }Which code segment should you add at line 03?A. currentItem["ClassificationMetadata"] ...
- Android 带清除功能的输入框控件EditText
1.效果图 2.源码下载 http://download.csdn.net/detail/yanzi2015/8864603 3.相关博客 http://www.cnblogs.com/to ...
- iOSQuartz2D-02-绘制炫酷的下载进度条
效果图 实现思路 要实现绘图,通常需要自定义一个UIView的子类,重写父类的- (void)drawRect:(CGRect)rect方法,在该方法中实现绘图操作 若想显示下载进度,只需要实例化自定 ...
- MAC OS Finder 中快速定位指定路径
在看一些 tip 文章的时候,时不时会有需要进到某某目录替换文件之类的步骤.如果碰上这个目录层次够多,一层一层的点击既麻烦又容易出错,有什么快捷的办法呢? 快捷键:Shift + Command + ...
- Erlang 进程被抢占的条件——一个进程长时霸占调度器的极端示例
最近研究 binary 的实现和各种操作对应的 beam 虚拟机汇编指令,发现有一些指令序列是不可重入的,比如说有的指令构造一个上下文(也就是某种全局状态),然后下一条指令会对这个上下文做操作(具体的 ...
- JavaScript Patterns 2.10 Naming Conventions
1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the word ...
- 深入PHP内核之in_array
无意中看到一段代码 1.a.php <?php $y="12"; $x = array(); for($j=0;$j<50000;$j++){ $x[]= " ...