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 ...
随机推荐
- Jumony Core 3,真正的HTML引擎
Jumony Core 3,真正的HTML引擎,正式版发布 2013-11-28 17:22 by Ivony..., 778 阅读, 18 评论, 收藏, 编辑 Jumony是一个开源项目,已经有三 ...
- 默认python2.6切换成python27
# 安装修改pythonyum -y install python27 python27-devel python -V; python2.6 -V # 查看当前python版本 这两个应该都 ...
- TD中{text-overflow:ellipsis;} 用法
Styles: table{ table-layout:fixed; } table td{ text-overflow:ellipsis;overflow:hidden;white-space: n ...
- [转]A Faster UIWebView Communication Mechanism
ref:http://blog.persistent.info/2013/10/a-faster-uiwebview-communication.html Use location.hash or t ...
- C#:iterator 迭代器/partial class 分布类/泛型
C#:iterator 迭代器/partial class 分布类/泛型 iterator 迭代器 写个最简单的迭代,(迭代一个字符串数组): 1.实现接口中的方法: 1 using System; ...
- 反向代理(Reverse Proxy)
反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时 ...
- linux下安装NPM管理工具
根据”挖一下“开发需要,选择nodejs实现异步IO,目的是为了解决服务器卡死导致无法处理后续的http请求.看了花瓣的架构视频讲座,才决定这么做的,挺有道理的. 安装nodejs很顺利,下载源码包, ...
- STM8S ADC初始化设置及应用
//ADC通道号定义 #define ADC_Chanel0 (unsigned char)0x00 #define ADC_Chanel1 (unsigned char)0x01 #define A ...
- SharePoint Solutions Deployment-PowerShell
之前群里有人问到了这个,项目一期开发结束后正好整理了一下,发上来分享一下,也是从谷歌搜索里面学来的,大家要用好搜索哈 $ver = $host | select version if ($ver.Ve ...
- php memcached缓存集群
一.需求描述 一linode xen vps 1G RAM,40+sites,IO频繁,openfiles ulimit已65535 US时间访问量大增,nginx不定时502 二.解决办法 page ...