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个数Q个查询,每次查询询问[l,r]内不同gcd的个数
**思路:**Q很大,按照一般思路使用线段树在线操作似乎不可行,所以考虑使用离线操作。但重点在于如何预处理出GCD。
在这里我们固定右端点,枚举上一个端点所存的所有不同GCD值,求GCD,并记录不同的,延伸右端点时,再重复操作即可。
在树状数组更新时,当出现一个重复的GCD值,始终把标记维护到最靠右的,并把之前出现的相同GCD的位置所在的标记消掉。这样就能使数量数组拥有前缀特性,可以使用sum[r]-sum[l]了
容器套容器很好用阿,弱要加快学了。

/** @Date    : 2016-11-13-16.10

* @Author : Lweleth (SoungEarlf@gmail.com)

* @Link : https://github.com/

* @Version :

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define pii pair<int , int>

#define FF first

#define SS second

#define MP(x,y) make_pair((x), (y))

#define LL long long

#define MMF(x) memset((x),0,sizeof(x))

#define MMI(x) memset((x), INF, sizeof(x))

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+2000;



int n,q;

int c[N];

int a[N];

int ans[N];

vector< pair<int,int> >gp[N];

int vis[1000010];



struct yuu

{

int l, r;

int m;

}Q[N];



int cmp(yuu a, yuu b)

{

return a.r < b.r;

}

int gcd(int a, int b)

{

return b?gcd(b, a % b):a;

}



void add(int x, int y)

{

while(x < N)

{

c[x] += y;

x += x & (-x);

}

}



int sum(int x)

{

int ans = 0;

while(x)

{

ans += c[x];

x -= x & (-x);

}

return ans;

}



void init()

{



MMF(c);

MMF(vis);

MMF(ans);

for(int i = 1; i <= n; i++)

{

int x = a[i];

int p = i;

for(int j = 0; j < gp[i-1].size(); j++)

{

int g = gcd(gp[i-1][j].FF, x);

if(x != g)

{

gp[i].push_back(MP(x, p));

x = g;

p = gp[i-1][j].SS;

}

}

gp[i].push_back(MP(x, p));

}

}

int main()

{

while(~scanf("%d%d", &n, &q))

{



MMF(a);

for(int i = 1; i <= n; i++ )

{

scanf("%d", a + i);

gp[i].clear();

}

init();

for(int i = 0; i < q; i++)

{

scanf("%d%d", &Q[i].l, &Q[i].r);

Q[i].m = i;

}



sort(Q, Q + q, cmp);

//////

int pos = 0;

for(int i = 1; i <= n; i++)

{

for(int j = 0; j < gp[i].size(); j++)//

{

if(vis[gp[i][j].FF])

add(vis[gp[i][j].FF], -1);

vis[gp[i][j].FF] = gp[i][j].SS;

add(gp[i][j].SS, 1);

}

while(Q[pos].r == i)

{

ans[Q[pos].m] = sum(Q[pos].r) - sum(Q[pos].l-1);

pos++;

}

}

for(int i = 0; i < q; i++)

printf("%d\n", ans[i]);



}

return 0;

}

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

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

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

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

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

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

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

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

    Different GCD Subarray Query Problem Description   This is a simple problem. The teacher gives Bob a ...

  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 4630 No Pain No Game 树状数组+离线查询

    思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...

  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. 京东2018秋招c++岗 神奇数

    题意大概是: 一个数比如242,把所有数字分成两组,而且两组的和相等,那么这个数就是神奇数,此时242,能够分成{2,2}和{4},所以242是神奇数. 题目要求输入n和m求[n,m]区间内神奇数的个 ...

  2. 【转】CentOS: 开放80、22、3306端口操作

    #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT#/sbin/iptables -I INPUT -p tcp --dport 22 -j AC ...

  3. js经典试题之运算符的优先级

    js经典试题之运算符 1.假设val已经声明,可定义为任何值.则下面js代码有可能输出的结果为: console.log('Value is ' + (val != '0') ? 'define' : ...

  4. 关于命令行参数argv(《学习OpenCV》)

    在<学习OpenCV>这本书中,很多示例代码都用到了命令行参数.作为新手,之前总是很困扰,不知道怎么用.偶然的机会终于略知一二了. 在Visual Studio中,我们可以自行设置命令行参 ...

  5. Qt在VS(Visual Studio)中使用

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt在VS(Visual Studio)中使用     本文地址:https://www.te ...

  6. PAT L1-048 矩阵A乘以B

    https://pintia.cn/problem-sets/994805046380707840/problems/994805082313310208 给定两个矩阵A和B,要求你计算它们的乘积矩阵 ...

  7. 《Effective C#》快速笔记(二)- .NET 资源托管

    简介 续 <Effective C#>读书笔记(一)- C# 语言习惯. .NET 中,GC 会帮助我们管理内存,我们并不需要去担心内存泄漏,资源分配和指针初始化等问题.不过,它也并非万能 ...

  8. css样式 一定要reset?

    有大神讲过了,直接看http://www.zhangxinxu.com/wordpress/?p=758

  9. HUAS 1476 不等数列(DP)

    考虑DP. 如果把转移看出当前位填什么数的话,这样是有后效性的. 如果考虑当前的序列是将1至n依次插入序列中的话. 考虑将i插入1到i-1的序列中,如果插入到<号中或者首部,那么最后就会多出一个 ...

  10. URL 编码规则

    规则: 1.将空格转换为加号(+) 2.对0-9.a-z.A-Z之间的字符保持不变 3.对于所有其他的字符,用这个字符的当前当前字符集编码在内存中的十六进制格式表示,并在每一个字节前加上一个百分号(% ...