因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,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. 高速清除winXP系统中explorer.exe病毒

    关于这个explorer.exe病毒.是眼下xp最为常见的一个病毒,会大量的消耗系统资源,造成电脑特别的卡顿. 1.关闭还原(假设没有,则跳过),为的是防止我们改动后,还原之后又回来了. 2.打开注冊 ...

  2. react 实现pure render的时候,bind(this)隐患

    react 实现pure render的时候,bind(this)隐患 export default class Parent extends Component { ... render() { c ...

  3. odoo写邮件添加收件人

    在任何可以写消息的地方点击鼠标     或者回复消息     写消息的框会聚焦并变大             点击撰写框右上角的弹出窗图标     弹出完整的撰写消息窗口     在红色的地方添加收件 ...

  4. 【转载】WebService相关概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  5. 获取Bootstrap-Table的所有内容,修改行内容

    var allTableData = $tableLeft.bootstrapTable('getData');//获取表格的所有内容行 var flag = false; for( i=0;i< ...

  6. VUE 之 路由 VueRouter

    1.VueRouter的安装 1.1.https://unpkg.com/vue-router/dist/vue-router.js下载安装. 1.2.<script src="./s ...

  7. JUNO eclipse Version: 4.2.0 添加svn插件

    1.下载最新的这个版本的SVN http://www.eclipse.org/subversive/latest-releases.php 实际的下载地址 http://www.eclipse.org ...

  8. maven依赖排除

    单依赖过滤 同依赖过滤直接处理:可以过滤一个或者多个,如果过滤多个要写多个<exclusion>. <dependency> <groupId>org.apache ...

  9. Hadoop MapReduce基本原理

    一.什么是: MapReduce是一种编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都 ...

  10. Android的三种主流资源尺寸

    Android三种主流资源屏幕尺寸:QVGA.HVGA.WVGA VGA的分辨率是640x480像素 QVGA(Quarter VGA)就是320x240,即VGA分辨率的1/4 HVGA(Half ...