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 ...
随机推荐
- ioc和aop
IOC容器的基本原理 ioc又称为控制反转.或者依赖注入.为什么叫控制反转呢? 可以这样来看.软件开发中会有很多的类,这些类在一起完成复杂的功能.这些类之间存在了耦合的关系.就如同很多的齿轮,啮合 ...
- Bootstrap相关优质项目推荐
Bootstrap 编码规范by @mdo Bootstrap 编码规范:编写灵活.稳定.高质量的 HTML 和 CSS 代码的规范. jQuery API 中文手册 根据最新的 jQuery 1.1 ...
- jQuery特殊符号转义
我们在使用jquery选择器的时候 对一些ID属性中有特殊符号的地方需要进行转义. 列举部分如下: <input id="entity.username" type=&quo ...
- synchronized简介
synchronized简介 Java提供了一种内置的锁机制来支持原子性:同步代码块(Synchronized Block).同步代码块包括两部分:一个作为锁对象的引用,一个作为由这个锁保护的代码块. ...
- javascript遍历select下拉框判断其中值是否与指定值相等
用jquery多了,就忘了原生的js是如何写的了,还需要多加巩固. 需求:jsp回显一select下拉框.选中指定值. 用户点击修改 该select进行已有值回显.有两种解决方法 一.js中获取用户的 ...
- java常用的环境变量配置
JDK配置 JAVA_HOME: C:\java\jdk1.7.0 CLASSPATH: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar; PAT ...
- Sublime Text 3设置笔记
Sublime Text 3设置笔记 Sublime Text 3设置指南 1. 安装package control 下载package control源码安装包,并解压: http://yun.ba ...
- 《.NET 编程结构》专题汇总
<.NET 编程结构>专题汇总 前言 掌握一门技术,首要的是掌握其基础. 笔者从事.NET相关开发多年,也非常喜欢.NET,多年来也积累了很多相关的资料,在此将一些基础性的 ...
- python JSON API duckduckgo search engine 使用duckduckgo API 尝试搜索引擎
The duckduckgo.com's search engine is very neat to use. Acutally it has many things to do with other ...
- jquery跨域请求数据
jquery跨域请求数据 jquery跨越请求数据.实际开发中经常会碰到两个网站数据交互问题,当向另一个站点请求数据该如何做? 实际上非常容易,请按照下面的步骤做: 第一:编写js,通过get获取远程 ...