题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个。

解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了。

那么怎么求区间内与区间其他某个数互质的数的个数(记为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 --容斥原理+树状数组的更多相关文章

  1. HDU 4777 Rabbit Kingdom(树状数组)

    HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...

  2. HDU 4947 GCD Array 容斥原理+树状数组

    GCD Array Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  4. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  5. HDU 4777 Rabbit Kingdom 树状数组

    分析:找到每一个点的左边离他最近的不互质数,记录下标(L数组),右边一样如此(R数组),预处理 这个过程需要分解质因数O(n*sqrt(n)) 然后离线,按照区间右端点排序 然后扫一遍,对于当前拍好顺 ...

  6. HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化

    题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d] 要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格 ...

  7. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  8. HDU 5862 Counting Intersections (树状数组)

    Counting Intersections 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 Description Given ...

  9. 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 ...

随机推荐

  1. (旧)子数涵数·C语言——指针

    一.什么是指针? 指针在百度的解释:是编程语言中的一个对象,利用地址,它的值直接指向(points to)存在电脑存储器中另一个地方的值. 也就是说,指针是用于指向某一内存单元. 简而化之,指针便是地 ...

  2. 那些教程没有的php3-命名空间

    php.net (PHP 5 >= 5.3.0, PHP 7) 定义命名空间 虽然任意合法的PHP代码都可以包含在命名空间中,但只有以下类型的代码受命名空间的影响,它们是:类(包括抽象类和tra ...

  3. 【poj 3461】Oulipo(字符串--KMP)

    题意:求子串在文本串中出现了多少次. 解法:使用KMP的next[ ]和tend[ ]数组计数. #include<cstdio> #include<cstdlib> #inc ...

  4. Delphi又要换东家了

    前几天听到这个消息,搞个FMX出来,64位还没搞清楚,又开始折腾了!http://www.deltics.co.nz/blog/posts/2371 No Seriously – Let’s Buy ...

  5. HorizontalScrollView实现先左滑动后右滑动动画

    @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s ...

  6. Android拍照保存在系统相册不显示的问题

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Uri uri = Uri.fromFile(new File(& ...

  7. 错误:升级为xcode8之后无法上网的解决方法

    主要是在info.list中增加以下的节点,因为XCode开始所有的http都转为https来联网了. 添加类型为Dictionary的:NSAppTransportSecurity 再添加节点为:N ...

  8. 通过注册的URL Scheme向目标APP传递参数

    通过注册的URL Scheme向目标APP传递参数 通过URL Scheme启动APP很简单就可以做到,但有时候我们想在启动APP的时候传递一些参数,这个时候我们就可以通过URL Scheme自定义U ...

  9. Java 网络编程----基本概念

    网络现在是一个非常普遍的概念. 以下是维基百科上的解释: 网络一词有多种意义,可解作: 网络流也简称为网络(network).一般用于管道系统.交通系统.通讯系统建模. 有时特指计算机网络. 或特指其 ...

  10. Eclipse 导入项目后启动报异常:java.lang.UnsatisfiedLinkError: Native Library *.dll already loaded in another classloade 解决方法

    tomcat 服务器的配置信息如下: