因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺。废话不多说了,补题。。。

update////2016/10/3-19:03;


题意:

第一个数求给定询问区间的GCD,第二个数求在这个给定区间里面有多少种连续区间的GCD是等于第一个数。

思路:

区间GCD个数可以利用线段树,或者RMQ搞;鉴于RMQ的查询是O(1),所以RMQ;

求各种GCD的区间数量,预处理掉;

(下面这种说是二分,其实还是很难想到的)

枚举起点,然后其实还是说是枚举终点,只是每次我枚举到一个终点,这个区间是[s,t],我就可以二分查找到最远的那个区间gcd[i, max_distance]是等于gcd[s,t],然后后面终点t就会变成最远的那个位置max_distance,以至于可以说是二分终点。

PS:这种二分的写法还是蛮常用的,长见识的把~

#include<map>
#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; typedef long long LL;
const int N=1e5+10; int d[N<<2];
int dp[N][30];
int mm[N];
int a[N]; void RMQ(int n,int b[])
{
mm[0]=-1;
for(int i=1;i<=n;i++)
{
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
dp[i][0]=b[i];
}
for(int j=1;j<=mm[n];j++)
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=__gcd(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int query(int x,int y)
{
int k=mm[y-x+1];
return __gcd(dp[x][k],dp[y-(1<<k)+1][k]);
}
map<int ,LL>mp; int main()
{
int n,t,q;
scanf("%d",&t);
int cas=1;
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
RMQ(n,a);
mp.clear(); for(int s=1;s<=n;s++)
{
int t=s;
while(t<=n)
{
int left=t;
int right=n;
int tmp=query(s,left);
while(left<=right)
{
int mid=(left+right)>>1;
if(query(s,mid)==tmp)
{
left=mid+1;
}
else
{
right=mid-1;
}
}
mp[tmp]+=left-t;
t=left;
}
}
scanf("%d",&q);
printf("Case #%d:\n",cas++);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
int gcd=query(l,r);
printf("%d %lld\n",gcd,mp[gcd]);
}
}
return 0;
}

2016 Multi-University Training Contest 1 GCD【RMQ+二分】的更多相关文章

  1. 2016 Multi-University Training Contest 1 GCD RMQ+二分(预处理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 题意:有N(N <= 100,000),之后有Q(Q <= 100,000)个区间查询[ ...

  2. 2016 Al-Baath University Training Camp Contest-1

    2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...

  3. HDU 5726 GCD (RMQ + 二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...

  4. 2016 Al-Baath University Training Camp Contest-1 E

    Description ACM-SCPC-2017 is approaching every university is trying to do its best in order to be th ...

  5. 2016 Al-Baath University Training Camp Contest-1 A

    Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...

  6. [CFGym101028] 2016 Al-Baath University Training Camp Contest-1

    比赛链接:http://codeforces.com/gym/101028/ 由于实习,几乎没有时间刷题了.今天下午得空,断断续续做了这一套题,挺简单的. A.读完题就能出结果. /* ━━━━━┒ギ ...

  7. 2016 Al-Baath University Training Camp Contest-1 J

    Description X is fighting beasts in the forest, in order to have a better chance to survive he's gon ...

  8. 2016 Al-Baath University Training Camp Contest-1 I

    Description It is raining again! Youssef really forgot that there is a chance of rain in March, so h ...

  9. 2016 Al-Baath University Training Camp Contest-1 H

     Description You've possibly heard about 'The Endless River'. However, if not, we are introducing it ...

随机推荐

  1. HTML小知识点积累

    1.怎样让heigth:100%起效?   有时候我们设置heigth:100%,想让当前控件铺满整个屏幕,可是非常少情况下这个属性能达到我们想要的效果,这是为什么呢?   而依据W3C的规范.百分比 ...

  2. 微信小程序 - 音频播放(1.2版本和1.2版本之后)

    不多说了,直接贴code // 1.2版本以后便不在维护 wx.getBackgroundAudioManager({ success:function(res){ var status =res.s ...

  3. 零基础学python-5.9 集合set

    今天我们来说说set 集合:是一些唯一的.不可变的对象(数值和字符串等)的一个无序的集合(collection).而且这些对象支持与数学集合理论相相应的操作. 特点: 1.一个项仅仅可以出现一次 2. ...

  4. ZOJ 3230 Solving the Problems(数学 优先队列啊)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3230 Programming is fun, Aaron is ...

  5. 深入理解JVM:HotSpot虚拟机对象探秘

    对象的创建 java是一门面向对象的语言.在Java程序执行过程中无时无刻有Java对象被创建出来.在语言层面上,创建对象(克隆.反序列化)一般是一个newkeyword而已,而在虚拟机中,对象的创建 ...

  6. Mysql 数据库中间件

    读写分离:简单的说是把对数据库读和写的操作分开对应不同的数据库服务器,这样能有效地减轻数据库压力,也能减轻io压力.主数据库提供写操作,从数据库提供读操作,其实在很多系统中,主要是读的操作.当主数据库 ...

  7. openwrt: firstboot

    # cat /sbin/firstboot #!/bin/sh /sbin/jffs2reset jffs2reset 是fstools里的工具.做的工作有: 在/proc/mtd里找到名为" ...

  8. Android 打造随意层级树形控件 考验你的数据结构和设计

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:[张鸿洋的博客] 1.概述 大家在项目中或多或少的可能会 ...

  9. sqlldr trailing nullcols

    由于要导入到tmp_content表的一些列(列:要导入的源文件txt or csv文件)为空,也,按理讲我当时另存为(在windows处理)csv,以,分隔.就是这个列没有内容,也该显示, ,之类的 ...

  10. node js 安装时选择勾上path

    勾上path则会自动配置环境变量,否则必须手动去添加nodejs的环境变量.