离线操作,树状数组,$RMQ$。

这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个。

这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i]$的情况,现在是更新$log$级别个数的数字(因为以$i$为结尾的区间,最多只有$log$级别种不同的$gcd$)。

求区间$gcd$可以用$RMQ$预处理一下,然后就可以$O(1)$查询了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} const int maxn=+;
int T,n,Q,a[maxn],dp[maxn][],c[maxn],pre[maxn*],ans[maxn];
struct X{int L,R,id;}s[maxn]; int gcd(int a,int b) { if(b==) return a; return gcd(b,a%b); } void RMQ_init()
{
for(int i=;i<n;i++) dp[i][]=a[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<n;i++)
dp[i][j]=gcd(dp[i][j-],dp[i+(<<(j-))][j-]);
} int RMQ(int L,int R)
{
int k=;
while((<<(k+))<=R-L+) k++;
return gcd(dp[L][k],dp[R-(<<k)+][k]);
} bool cmp (X a,X b) { return a.R<b.R; } int lowbit(int x) {return x&(-x);}
int sum(int x)
{
int res=;
for(int i=x;i>;i=i-lowbit(i)) res=res+c[i];
return res;
}
void update(int x,int v)
{
for(int i=x;i<=n;i=i+lowbit(i)) c[i]=c[i]+v;
} int main()
{
while(~scanf("%d%d",&n,&Q))
{
for(int i=;i<n;i++) scanf("%d",&a[i]);
RMQ_init(); for(int i=;i<Q;i++) scanf("%d%d",&s[i].L,&s[i].R),s[i].id=i;
sort(s,s+Q,cmp); memset(pre,,sizeof pre); memset(c,,sizeof c); int p=;
for(int i=;i<n;i++)
{
int L=,R=i,g=a[i]; while()
{
int left=L,right=R,pos1,pos2; while(left<=right)
{
int mid=(left+right)/;
if(RMQ(mid,i)==g) pos1=mid,right=mid-;
else left=mid+;
} left=L,right=R,pos2;
while(left<=right)
{
int mid=(left+right)/;
if(RMQ(mid,i)==g) pos2=mid,left=mid+;
else left=mid+;
} pos1++,pos2++;
if(pre[g]>pos2) continue;
if(pre[g]!=) update(pre[g],-);
update(pos2,); pre[g]=pos2;
pos1--,pos2--; R=pos1-; if(R<) break; g=RMQ(R,i);
} while(p<Q&&s[p].R==i+)
ans[s[p].id]=sum(s[p].R)-sum(s[p].L-), p++;
} for(int i=;i<Q;i++) printf("%d\n",ans[i]);
}
return ;
}

HDU 5869 Different GCD Subarray Query的更多相关文章

  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. HDU 5869 Different GCD Subarray Query 离线+树状数组

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

  3. 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 ( ...

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

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

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

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

  6. 【刷题】HDU 5869 Different GCD Subarray Query

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

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

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

  8. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  9. HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)

    树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/6 ...

随机推荐

  1. noip模拟赛:部队[技巧?思想?]

    王国军总指挥——卡西乌斯准将决定重建情报局,需要从全国各地挑选有能力的士兵,选择的标准为A,B两种能力.对于每个候选士兵,如果存在另一名士兵的两项能力均大于等于他,那么他将被淘汰.(注意:若两名士兵两 ...

  2. Best Cow Line(POJ No.3617)

    问题: 链接:http://poj.org/problem?id=3617 思路: 按照字典序比较S和将S反转后的字符串S' 如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值) 如果S’ ...

  3. HTTP method GET is not supported by this URL(转)

    源地址:http://blog.csdn.net/qfs_v/article/details/2545168 Servlet  eroor:HTTP method GET is not support ...

  4. C语言面试问答5

    12个滑稽的C语言面试问答——<12个有趣的C语言问答>评析(5) 前文链接:http://www.cnblogs.com/pmer/archive/2013/09/17/3327262. ...

  5. MVC视图与控制器分离简单描述

    一,控制器 CheckIndexAreaRegistration.cs public class CheckIndexAreaRegistration : AreaRegistration { pub ...

  6. IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)

    最近滑动菜单比较流行,像facebook和人人等都在使用滑动菜单,今天做了一个小demo大体效果如下: 这次用了一个开源的项目ECSlidingViewController这个也是一个挺著名的托管在G ...

  7. javascript 闭包基础分享

    javascript 闭包基础分享 闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感觉,事实上,闭包的概念在函数式编程语言中算不上是难以理解的知识.如果对作用域,函数为独立的对象这 ...

  8. 将 Servlet (HTTP POST/GET)请求发布到OSB

    转载地址:http://user.qzone.qq.com/32221895/blog/1406960844 ------------------------------------- 发帖要200字 ...

  9. GetWindowRect和GetClientRect的区别详解

    一:关于坐标 MFC中绘图时经常涉及到坐标计算,GetWindowRect和GetClientRect这两个函数,是获取逻辑坐标系中窗口或控件(其实也是窗口)大小和坐标的常用函数了,有什么不一样的? ...

  10. 从Chrome源码看浏览器如何构建DOM树

    .aligncenter { clear: both; display: block; margin-left: auto; margin-right: auto } p { font-size: 1 ...