HDU 5869 Different GCD Subarray Query
离线操作,树状数组,$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的更多相关文章
- HDU 5869 Different GCD Subarray Query rmq+离线+数状数组
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...
- HDU 5869 Different GCD Subarray Query 离线+树状数组
Different GCD Subarray Query Problem Description This is a simple problem. The teacher gives Bob a ...
- 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 ( ...
- HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...
- HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景
http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...
- 【刷题】HDU 5869 Different GCD Subarray Query
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
- HDU 5869 Different GCD Subarray Query 树状数组+离线
Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...
- HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)
还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...
- HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)
树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/6 ...
随机推荐
- CSLA .NET是一个.NET软件开发框架
CSLA .NET是一个.NET软件开发框架,帮助开发者“为Windows.Web.面向服务和工作流等应用构建强大和可维护的业务逻辑层”. CSLA是Component-based, Scalable ...
- [Usaco2008 Nov]Guarding the Farm 保卫牧场[DFS]
Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...
- 使用LFM(Latent factor model)隐语义模型进行Top-N推荐
最近在拜读项亮博士的<推荐系统实践>,系统的学习一下推荐系统的相关知识.今天学习了其中的隐语义模型在Top-N推荐中的应用,在此做一个总结. 隐语义模型LFM和LSI,LDA,Topic ...
- [转]Creating an iPhone Daemon
ref: http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/ http://chrisalvares.com/blog/3 ...
- [置顶] 从引爆点的角度看360随身wifi的发展
从引爆点的角度看360随身wifi的发展 不到一个月的时间,随身wifi预定量就数百万.它的引爆点在哪里,为什么相同的产品这么多它却能火起来,通过对随身wifi的了解和我知识层面分析,主要是因为随身w ...
- MVC几种传值方式
一,Model public class Course { public int Id { get; set; } public string Name { get; set; } } public ...
- cocos2d(背景图片循环滚动)
背景图片循环滚动 使用action 实现的: 主要有两个背景图片交替循环滚动:我选的两个背景图片的宽度都是1024的 ,所以定义了#define BGIMG_WIDTH 1024 代码如下: 在Hel ...
- Github官方app的账户身份验证的实现
作为github上被fork最多,被star最多的Android项目,实在是没有理由不让我去学习学习的.这篇文章是关于其账户身份验证模块的实现的.如果你对此有兴趣,请移步到我的blog,地址如下: 地 ...
- 支持异步同步的分布式CommandBus MSMQ实现
支持异步同步的分布式CommandBus MSMQ实现 先上一张本文所描述的适用场景图 分布式场景,共3台server: 前端Server Order App Server Warehouse App ...
- csshack技术
我最近想好好整理下csshack技术,但是结果很沮丧,下面我将我最初写的笔记和大家分享下. 我在单位整理的研究笔记: 不同的浏览器对某些CSS代码解析会存在一定的差异,因此就会导致不同浏览器下给用户展 ...