2016大连网络赛 Different GCD Subarray Query

Different GCD Subarray Query

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

分析:对于每个点来说,向前的gcd很少,可以预处理出gcd出现的最后的左端点;

   然后离线查询右端点,每走到一个点,更新gcd出现的左端点,然后树状数组询问答案即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t,a[maxn],c[maxn],pr[maxn*10],ans[maxn];
vector<pii >b[maxn];
vector<pii >q[maxn];
void add(int x,int y)
{
    for(int i=x;i<=n;i+=(i&(-i)))
        c[i]+=y;
}
int get(int x)
{
    int ret=0;
    for(int i=x;i;i-=i&(-i))
        ret+=c[i];
    return ret;
}
void init()
{
    for(int i=1;i<=n;i++)
    {
        int x=a[i],y=i;
        b[i].pb(mp(x,y));
        for(int j=0;j<b[i-1].size();j++)
        {
            int now=gcd(b[i-1][j].fi,a[i]),pos=b[i-1][j].se;
            if(now!=x)
            {
                b[i].pb(mp(now,pos));
                x=now;
            }
        }
    }
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        rep(i,1,n)scanf("%d",&a[i]),b[i].clear(),q[i].clear();
        memset(c,0,sizeof c);
        memset(pr,0,sizeof pr);
        init();
        rep(i,1,m)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            q[y].pb(mp(x,i));
        }
        rep(i,1,n)
        {
            for(j=0;j<b[i].size();j++)
            {
                int x=b[i][j].fi,pos=b[i][j].se;
                if(pr[x])add(pr[x],-1);
                add(pos,1);
                pr[x]=pos;
            }
            for(int j=0;j<q[i].size();j++)
            {
                int x=q[i][j].fi,y=q[i][j].se;
                ans[y]=get(i)-(x-1==0?0:get(x-1));
            }
        }
        rep(i,1,m)printf("%d\n",ans[i]);
    }
    //system("Pause");
    return 0;
}
时间: 2024-10-17 05:44:18

2016大连网络赛 Different GCD Subarray Query的相关文章

2016大连网络赛 Function

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description The shorter, the simpler. With this problem, you should be convinced of this truth.    You are given an array A of N postive integers,

2016大连网络赛 1008 &amp; hdu5875 (优先队列+离线)=不确定暴力

题意:给你一个区间,求a_l%a_(l+1)%a_(l+2)%-%a_r 的值 分析:听说一个数在给定区间中只有不是很多的位置可一连续对它求模,所以想到一个比较暴力有可行的方法,猜想复杂度应该是nlogn.具体是这样的,从左到有枚举每个位置, L[]记录[1,r]中所有元素连续取模到r的值.一开始把a[1]加进优先队列pq,对于第二位置,若pq.top()>=a[i],取出并取模,然后更新对应的位置l的答案,并把取模后答案插入优先队列,然后处理有区间是2的所有询问.对于第i个位置,若pq.top

2016大连网络赛 Sparse Graph

Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description In graph theory, the complement of a graph G is a graph H on the same vertices such that two distinct vertices of H are adjacent if

2016大连网络赛 Weak Pair

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 333    Accepted Submission(s): 111 Problem Description You are given a rooted tree of N nodes, labeled from 1 to N. To the ith node a

2016大连网络赛 Football Games

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description A mysterious country will hold a football world championships---Abnormal Cup, attracting football teams and fans from all around th

2016大连网络赛

1008 Function 题解:如果a>=b,那么a%b<=a/2; 一个数n从本来的位置递减到0最多只需要logn次,也就是往右递减的位置最多不超过30个,那么我们就可以预处理出每个数往右递减的位置,然后离线询问, 用一个优先队列存储数和数所在的位置,比如8 7 6 9 10,先push(8,1)进去,然后每次把队首拿出来,8%7=1,把(1,1)push进去,然后把(8,1)pop出来,(7,2)push进去,优先队列最大值优先,当队首元素小于当前元素的时候,把当前元素和位置push进去

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 (Java/Others)Total Submission(s): 828    Accepted Submission(s): 300 Problem Description This is a simple problem. The teacher gives Bob a list of prob

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

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 n

大连网络赛 1006 Football Games

1 //大连网络赛 1006 2 // 吐槽:数据比较水.下面代码可以AC 3 // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) 4 5 #include <bits/stdc++.h> 6 using namespace std; 7 #define LL long long 8 typedef pair<int,int> pii; 9 const double inf = 123456789012345.0; 10 const LL MOD =100000000L