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

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

那么怎么求区间内与区间其他某个数互质的数的个数(记为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. tomcat学习笔记一:安装和配置

    安装 配置环境: win7 + centos7虚拟机 安装步骤: 到http://tomcat.apache.org/下载最新的tomcat安装包(8.0.24) 移到对应的目录并解压 安装问题: 安 ...

  2. 干净的停止tomcat/java应用程序

    通常在使用了jdbc或者netty的应用程序中,当shutdown tomcat或java应用程序时,会出现无法停止的情况,报类似如下错误: 严重: The web application [] re ...

  3. java系统库性能优化注意点

    对于大部分的应用系统开发来说,微秒级别的优化通常是被忽略不计或者不关心的,因为有着更多优化点,但是对于开发供应用系统使用的公用库来说,大部分的操作通常都是对字符.数字.字符串.字节的操作,且这些系统库 ...

  4. Mysql基本数据操作

    一.mysql中的逻辑对象 mysqld(process_id(threads)+memory+datadir)-->库-->表-->记录(由行与列组成) 什么是关系型数据库:表与表 ...

  5. ASP.NET本质论第二章应用程序对象学习笔记1

    1.请求的处理参数—上下文对象HttpContext 1) 针对每一次请求,ASP.NET将创建一个处理这次请求所使用的HttpContext对象实例,这个对象实例将用来在ASP.NET服务器的处理过 ...

  6. 如何解决div层被flash遮盖的问题

    页面构建中的Flash层会遮挡Div的问题,一般通过设置wmode="transparent" 或wmode="window"就可以解决.不过对于Flash视频 ...

  7. ASP.NET页面间传值总结

    本文我们将讨论的是ASP.NET页面间数据传递的几种方法,对此希望能帮助大家正确的理解ASP.NET页面间数据传递的用处以及便利性. Web页面是无状态的,服务器对每一次请求都认为来自不同用户,因此, ...

  8. andriod CheckBox

    <?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...

  9. ADB server didn't ACK 解决方法

    在安卓开发的过程中 连接真机的时候 连接不上 提示 The connection to adb is down, and a severe error has occured.[2015-01-22 ...

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

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