Different GCD Subarray Query

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 681    Accepted Submission(s): 240

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
 

题意:

题解:

 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#define LL __int64
#define pii pair<int,int>
#define MP make_pair
const int N=;
using namespace std;
int gcd(int a,int b)
{
return b== ? a : gcd(b,a%b);
}
int n,q,a[N],ans[N];
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 ;
}

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  2. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  6. hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  7. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  8. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

随机推荐

  1. MyEclipse 修改代码不生效

    最近得了一个项目,java开发的web项目,修改代码时,无论怎么改,都不生效: 各种度娘,没用. 原因是没有建立发布设定 这个东西我开始不理解它的作用,现在知道了: mysqleclipse项目在一个 ...

  2. 一模 (1) day1

    第一题:(水题) 题目大意:求出n个  X% (X是小于等于2位的整数) 的乘积,去掉末尾的0: 解题过程: 1.直接 把整数乘好,然后确定小数点的位置,去掉多余的0 输出即可. 第二题:(搜索题) ...

  3. 可滑动的ToggleButton(开关)

    2013-12-28 17:25:01 网上看到一篇关于可滑动的ToogleButton的文章,有代码,觉得挺好,但是不符合我的要求,因此在他的代码基础上改了一些.(作者看到了勿喷啊,实在找不到原文了 ...

  4. RM报表 文本框 自动换行 相关代码

    procedure TRMCustomMemoView.WrapMemo1(aAddChar: Boolean); var lCurHeight, lOneLineHeight, lMaxWidth: ...

  5. 网易云课堂 OCP

    数据库DBA任务: 管理数据库可用性 设计并创建数据库 管理物理结构 管理基于设计的存储 管理安全性 网络管理 备份与恢复 数据库调整与优化 关系型数据库(RDBMS) 多个表数据之间存在着关系 关系 ...

  6. sql server日志不能shrink或truncate

    Backup log [dbxxx] with truncate_only sql server 2008之后不支持此操作,需要改为: BACKUP LOG dbxxx TO DISK='NUL:' ...

  7. 0125 多线程 继承Thread 练习

    //定义一个继承Thread类的类,并覆盖run()方法,在run()方法中每隔100毫秒打印一句话public class Csh extends Thread{ public void run() ...

  8. 转:Java面试题集(1-50)

    Java程序员面试题集(1-50) http://blog.csdn.net/jackfrued/article/details/17403101 一.Java基础部分 1.面向对象的特征有哪些方面? ...

  9. javascript 变量声明有var与无var 的区别

    1.在函数作用域内 加var定义的变量是局部变量,不加var定义的就成了全局变量.使用var定义var a = 'hello World';function bb(){var a = 'hello B ...

  10. DDOS攻击原理及防护方法论

      从 07年的爱沙尼亚DDOS信息战,到今年广西南宁30个网吧遭受到DDOS勒索,再到新浪网遭受DDOS攻击无法提供对外服务500多分钟. DDOS愈演愈烈,攻击事件明显增多,攻击流量也明显增大,形 ...