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. 数列F[19] + F[13]的值

    已知数列如下:F[1]=1, F[2]=1, F[3]=5,......,F[n] =F[n-1] + 2*F[n-2],求F[19] + F[13]? #include <stdio.h> ...

  2. No module ata_piix found的解决方法

    在一台as4u6的机器上升级内核到2.6.18时,最好make install的时候报了一个WARNING: No module ata_piix found for 2.6.18, 开始没有在意,重 ...

  3. Android计算时间差

    想要写个根据消耗时长来确定开始结束时间的小工具,发现Android处理时间上有点累,可能是我没找到合适的方法吧,先把我的解决办法贴出来,有好的解决方法还希望提醒一下: 1.根据时间字符串获取毫秒数 p ...

  4. DataNode,NameNode,JobTracker,TaskTracker用jps查看无法启动解决办法

    查看tasktracker的50060的地址无法正常查看,主要有两个原因,一个是在/tmp目录下有以前使用2.02版本留下的文件没有删除,二个是因为端口被占用了 解决方法: 一.删除/tmp目录下所有 ...

  5. jQuery 动态添加瀑布流

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. xml文件有误

    Unable to start activity ComponentInfo{com.anzi.jmsht.scripturelibrary/com.anzi.jmsht.scripturelibra ...

  7. cf--1C

    //Accepted 0 KB 60 ms //给出正多变形上的三个点,求正多形的最小面积 //记三个点之间的距离a,b,c; //由余弦定理得cosA //从而可求出sinA,和正多边形所在外接圆的 ...

  8. poj1159 dp最长公共子串

    //Accepted 204 KB 891 ms //dp最长公共子串 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) //dp[i][j]=max(dp[i][j],dp ...

  9. Ubuntu 14.10 下Ganglia监控Spark集群

    由于Licene的限制,没有放到默认的build里面,所以在官方网站下载的二进制文件中并不包含Gangla模块,如果需要使用,需要自己编译.在使用Maven编译Spark的时候,我们可以加上-Pspa ...

  10. C# virtual和abstract的

    virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义. 它们有一个共同点:如果用来修饰方法,前面必须添加public,要不然就会出现编译错误:虚拟方法或抽象方法是不能 ...