Different GCD Subarray Query

Problem Description
 
This is a simple problem. The teacher gives Bob a list of problems about GCD (Greatest Common Divisor). After studying some of them, Bob thinks that GCD is so interesting. One day, he comes up with a new problem about GCD. Easy as it looks, Bob cannot figure it out himself. Now he turns to you for help, and here is the problem:
  
  Given an array a of N positive integers a1,a2,⋯aN−1,aN; a subarray of a is defined as a continuous interval between a1 and aN. In other words, ai,ai+1,⋯,aj−1,aj is a subarray of a, for 1≤i≤j≤N. For a query in the form (L,R), tell the number of different GCDs contributed by all subarrays of the interval [L,R].
  
 
Input
 
There are several tests, process till the end of input.
  
  For each test, the first line consists of two integers N and Q, denoting the length of the array and the number of queries, respectively. N positive integers are listed in the second line, followed by Q lines each containing two integers L,R for a query.

You can assume that 
  
    1≤N,Q≤100000 
    
   1≤ai≤1000000

 
Output
 
For each query, output the answer in one line.
 
Sample Input
 
5 3
1 3 4 6 9
3 5
2 5
1 5
 
Sample Output
 
6
6
6
 

题意

  长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种.

题解:

  固定右端点

  预处理出 每个点向左延伸 的 不同gcd值

  这样的 值不会超过log a 个

  然后问题就变成了 问你一段区间内不同 gcd 值有多少,值是很少的 (询问一个区间有多少颜色的题型)

  树状数组维护就可以了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e2+, mod = 1e9+, inf = 2e9; int n,q,a[N],ans[N];
int gcd(int a, int b) { return b == ? a : gcd(b, a%b);}
vector<pii> G[N];
struct QQ{
int l,r,id;
bool operator < (const QQ &a) const {
return a.r > r;
}
}Q[N];
int C[N],vis[N];
void update(int x,int c) {
for(int i =x; i < N; i+=i&(-i)) C[i] += c;
}
int ask(int x) {
int s = ;
for(int i = x; i; i-= i & (-i))s += C[i];
return s;
}
int main() {
while(scanf("%d%d",&n,&q)!=EOF) {
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
for(int i = ; i <= n; ++i) G[i].clear();
for(int i = ; i <= n; ++i) {
int x = a[i];
int y = i;
for(int j = ; j < G[i-].size(); ++j) {
int res = gcd(x,G[i-][j].first);
if(x != res) {
G[i].push_back(MP(x,y));
x = res;
y = G[i-][j].second;
}
}
G[i].push_back(MP(x,y));
}
memset(C,,sizeof(C));
memset(vis,,sizeof(vis));
for(int i = ; i <= q; ++i) {scanf("%d%d",&Q[i].l,&Q[i].r),Q[i].id = i;}
sort(Q+,Q+q+);
for(int R = , i = ; i <= q; ++i) {
while(R < Q[i].r) {
R++;
for(int j = ; j < G[R].size(); ++j) {
int res = G[R][j].first;
int ids = G[R][j].second;
if(vis[res]) update(vis[res],-);
vis[res] = ids;
update(vis[res],);
}
}
ans[Q[i].id] = ask(R) - ask(Q[i].l-);
}
for(int i = ; i <= q; ++i) cout<<ans[i]<<endl;
}
return ;
}

HDU 5869 Different GCD Subarray Query 离线+树状数组的更多相关文章

  1. HDU 5869 Different GCD Subarray Query rmq+离线+数状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...

  2. HDU 5869 Different GCD Subarray Query 树状数组+离线

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

  3. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  4. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  5. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  6. HDU 5869 Different GCD Subarray Query

    离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...

  7. hdu 5869 Different GCD Subarray Query BIT+GCD 2016ICPC 大连网络赛

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  8. HDU 5603 the soldier of love 离线+树状数组

    这是bestcorder 67 div1 的1003 当时不会做 看了赛后官方题解,然后翻译了一下就过了,而且速度很快,膜拜官方题解.. 附上官方题解: the soldier of love 我们注 ...

  9. 【刷题】HDU 5869 Different GCD Subarray Query

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

随机推荐

  1. 常见web错误码 404 500

    404表示文件或资源未找到java WEB常见的错误代码1.1xx-信息提示:这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应.100-继续.101-切换协议.2. ...

  2. 2.kvm创建第一个虚拟机

    1.创建一个镜像 [root@kvm ~]# qemu-img create -f raw /opt/Centos_6.6_x86.raw 5G Formatting '/opt/Centos_6.6 ...

  3. Effective C++ -----条款19:设计class犹如设计type

    Class的设计就是type的设计.在定义一个新type之前,请确定你已经考虑过本条款覆盖的所有讨论主题. 新type的对象应该如何被创建和销毁? 对象的初始化和对象的赋值该有什么样的区别? 新typ ...

  4. Effective C++ -----条款09:绝不在构造和析构过程中调用virtual函数

    在构造和析构期间不要调用virtual函数,因为这类调用从不下降至derived class(比起当前执行构造函数和析构函数的那层).

  5. 【linux】虚拟机安装centos后ping ip地址出现错误:Network is unreachable

    来源:https://my.oschina.net/stonezing/blog/515480 方案一: 进入/etc/sysconfig/network-scripts/ 查看这下面的文件 每个人的 ...

  6. jquery 建议编辑器

    用谷歌搜索找了很久,发现所有的插件都是功能太复杂,不是我想要的.所以,我决定我自己来实现需要的编辑功能.刚开始我觉得应该要花费很多的时间,因为我想象内容编辑功能应该是很复杂的. 但事实证明,它是如此简 ...

  7. [Android Pro] Gradle Tips#2-语法

    referece to : http://blog.csdn.net/lzyzsd/article/details/46935063 在第一篇博客中,我讲解了关于tasks和构建过程中task的不同阶 ...

  8. Windows 删除 .svn标志

    之前一个项目是在SVN下面管理的,后来,考出来了,然后在Eclispe中使用,后来想用SVN管理起来,但是项目中,还是有.svn标志,只能先删除了.svn文件,然后在用svn管理起来,后来,发现.sv ...

  9. NYOJ题目611练练

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJ1CAIAAACgqiqJAAAgAElEQVR4nO3du27jSp4HYL+Ecj2IYz

  10. JavaScript基础——定义变量

    在JavaScript中使用变量来临时存储和访问来自JavaScript文件的数据.变量既可以指向简单的数据类型,如数字或者字符串:也可以指向更复杂的数据类型,比如对象. 在JavaScript中定义 ...