题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=5869

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
 
Source
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5877 5876 5874 5873 5872 
 
题意:输入N和Q,表示有N个数的一个序列,Q次询问,每次输入 l 和 r 表示一个区间,求这个区间不同的最大公倍数的个数(由这个区间的子区间得到);
 
思路:对数列进行GCD离散处理(~我也是才知道还有这样的离散~) 

for(int i=;i<=N;i++)
{
int tot=a[i],pos=i;
for(int j=;j<v[i-].size();j++)
{
int r=__gcd(a[i],v[i-][j].first);
if(tot!=r)
{
v[i].push_back(make_pair(tot,pos));
tot=r; pos=v[i-][j].second;
}
}
v[i].push_back(make_pair(tot,pos));
}
       然后对Q次询问离线处理,先输入Q次询问的区间,然后按右端点从小到大排序,i从1~N循环,当i==node[len].r  则 ans[node[len].id]=Sum(i)-Sum(node[len].l-1) ;
可以方便快速的用树状数组处理;
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
int a[];
int c[];
int vis[];
int sum[];
struct Node
{
int l,r;
int id;
}node[];
bool cmp(const Node s1,const Node s2)
{
return s1.r<s2.r;
}
vector<pair<int,int> > v[]; int __gcd(int x,int y)
{
int r=x%y;
x=y;
y=r;
if(r==) return x;
return __gcd(x,y);
}
int Lowbit(int t)
{
return t&(t^(t-));
}
int Sum(int x)
{
int sum = ;
while(x > )
{
sum += c[x];
x -= Lowbit(x);
}
return sum;
}
void add(int li,int t)
{
while(li<=)
{
c[li]+=t;
li=li+Lowbit(li);
}
}
int main()
{
int N,Q;
while(scanf("%d%d",&N,&Q)!=EOF)
{
for(int i=;i<=N;i++) scanf("%d",&a[i]);
for(int i=;i<=N;i++)
{
int tot=a[i],pos=i;
for(int j=;j<v[i-].size();j++)
{
int r=__gcd(a[i],v[i-][j].first);
if(tot!=r)
{
v[i].push_back(make_pair(tot,pos));
tot=r; pos=v[i-][j].second;
}
}
v[i].push_back(make_pair(tot,pos));
} for(int i=;i<Q;i++)
scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i;
sort(node,node+Q,cmp);
memset(c,,sizeof(c));
memset(vis,,sizeof(vis));
int len=;
for(int i=;i<=N;i++)
{
for(int j=;j<v[i].size();j++)
{
int s1=v[i][j].first;
int s2=v[i][j].second;
if(vis[s1]){
add(vis[s1],-);
}
vis[s1]=s2;
add(s2,);
}
while(node[len].r==i)
{
sum[node[len].id]=Sum(i)-Sum(node[len].l-);
len++;
}
}
for(int i=;i<Q;i++)
printf("%d\n",sum[i]);
for(int i=;i<=N;i++)
v[i].clear();
}
return ;
}

2016 大连网赛---Different GCD Subarray Query(GCD离散+树状数组)的更多相关文章

  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. 区间gcd问题 HDU 5869 离线+树状数组

    题目大意:长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种. 子段就是表示是要连续的a[] 思路:固定右端点,预处理出所有的gcd,每次都和i-1的gcd比较,然后 ...

  3. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

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

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

  5. 2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

    I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...

  6. 沈阳网络赛J-Ka Chang【分块】【树状数组】【dfs序】

    Given a rooted tree ( the root is node 11 ) of NN nodes. Initially, each node has zero point. Then, ...

  7. 北邮校赛 F. Gabriel's Pocket Money(树状数组)

    F. Gabriel's Pocket Money 2017- BUPT Collegiate Programming Contest - sync 时间限制 2000 ms 内存限制 65536 K ...

  8. 【HDU4947】GCD Array(莫比乌斯反演+树状数组)

    点此看题面 大致题意: 一个长度为\(n\)的数组,实现两种操作:将满足\(gcd(i,k)=d\)的\(a_i\)加上\(v\),询问\(\sum_{i=1}^xa_i\). 对于修改操作的推式子 ...

  9. 2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)

    https://nanti.jisuanke.com/t/41422 题目大意: 给出n和b,求1到n,各数在b进制下各位数之和的总和. 直接暴力模拟,TLE.. 没想到是要打表...还是太菜了. # ...

随机推荐

  1. 状态栏 a.getBoolean(1, false) 报错

    状态栏 a.getBoolean(1, false) 报错 这个错误在编译运行时候并不会出现,但是当需要编译打包的时候,就会报出这个异常. TypedArray a = mContext.obtain ...

  2. 大数据时代的IT架构设计

    大数据时代的IT架构设计(来自互联网.银行等领域的一线架构师先进经验分享) IT架构设计研究组 编著   ISBN 978-7-121-22605-2 2014年4月出版 定价:49.00元 208页 ...

  3. salesforce 零基础开发入门学习(三)sObject简单介绍以及简单DML操作(SOQL)

    salesforce中对于数据库操作和JAVA等语言对于数据库操作是有一定区别的.salesforce中的数据库使用的是Force.com 平台的数据库,数据表一行数据可以理解成一个sObject变量 ...

  4. 第三节:Vue计算属性

    计算属性就是当其依赖的属性的值发生变化的时候,这个属性的值就会自动更新. 例子: <!DOCTYPE html> <html> <head> <meta ch ...

  5. HTML5横竖屏提示

    HTML代码: <div class="screen-prompt"></div> CSS判断代码: /*横竖屏提示*/ @media screen and ...

  6. WPF自定义控件与样式(11)-等待/忙/正在加载状态-控件实现

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要有三种实现方式 ...

  7. JUnit4使用

    1.导入Junit4jar包: Eclipse中在项目上右键点击Bulid Path,然后再点击Add libraries,选择JUnit 2.初次使用 首先先创建一个java项目如下: Demo.j ...

  8. backbone库学习-Router

    backbone库的结构http://www.cnblogs.com/nuysoft/archive/2012/03/19/2404274.html 本文的例子来自http://blog.csdn.n ...

  9. Java之HashMap在多线程情况下导致死循环的问题

    PS:不得不说Java编程思想这本书是真心强大.. 学习内容: 1.HashMap<K,V>在多线程的情况下出现的死循环现象   当初学Java的时候只是知道HashMap<K,V& ...

  10. Javascript动画效果(一)

    Javascript动画效果(一) 前面我们介绍了Javascript的回到顶部效果,今天呢,我们对Javascript动画做进一步的研究.在这篇博文中我们只介绍简单的匀速运动.简单的缓冲运动和简单的 ...