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 ...
随机推荐
- 【poj 3167】Cow Patterns(字符串--KMP匹配+数据结构--树状数组)
题意:给2个数字序列 a 和 b ,问按从小到达排序后,a中的哪些子串与b的名次匹配. a 的长度 N≤100,000,b的长度 M≤25,000,数字的大小 K≤25. 解法:[思考]1.X 暴力. ...
- C# on Visual Studio Code
installation Download .NET Core SDK installer and install it. https://www.microsoft.com/net/download ...
- ahjesus 安装mongodb企业版for ubuntu
导入共匙 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 创建源列表 echo 'deb http ...
- 研究jdk关于TreeMap 红黑树算法实现
因为TreeMap的实现方式是用红黑树这种数据结构进行存储的,所以呢我主要通过分析红黑树的实现在看待TreeMap,侧重点也在于如何实现红黑树,因为网上已经有非常都的关于红黑树的实现.我也看了些,但是 ...
- 微信公共平台开发2 .net
成功的走出第一步后,我们紧接着趁热打铁开始下一步: 成为了开发者之后微信平台会给您AppId和AppSecret,在订阅号中是没有的,所以因该申请一下服务号, 若没有请注意上一篇http://www. ...
- parseInt第二个参数详解
前阵子在stackOverflow上看到两个这样的问题: 为什么parseInt(8,3) == NaN,parseInt(16,3) == 1? 为什么parseInt('dsff66',16) = ...
- SharePoint 2010 文档管理系列
前言,这是自己第一次写一个系列的文档,本来想使用SharePoint 2013版本,但是碍于SharePoint 2013对于硬件要求过高,自己的笔记本无法承受,所以退而求其次选择了在SharePoi ...
- R语言学习笔记:因子
R语言中的因子就是factor,用来表示分类变量(categorical variables),这类变量不能用来计算而只能用来分类或者计数. 可以排序的因子称为有序因子(ordered factor) ...
- android 多屏幕适配 : 第一部分
1.在xml布局文件中,控件的宽度和高度用 dp ; 字体大小用 sp 2.根据屏幕的宽高来动态的适配 , 获取屏幕的宽高的两种方法: 第一种方法: /** * 屏幕的宽度 * 屏幕的高度 * ...
- Android项目实战(二):安卓应用程序退出的三种方法
现在的APP退出的时候都不是让用户点击了“后退键”就退出.防止用户点错了后退键而造成的用户体检不好. 一年前搞的Demo代码不见了,重新写下就当是复习和以后直接拿来用把 目前流行的解决一般分为两种: ...