2016 Multi-University Training Contest 1 GCD【RMQ+二分】
因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,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+二分】的更多相关文章
- 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)个区间查询[ ...
- 2016 Al-Baath University Training Camp Contest-1
2016 Al-Baath University Training Camp Contest-1 A题:http://codeforces.com/gym/101028/problem/A 题意:比赛 ...
- HDU 5726 GCD (RMQ + 二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5726 给你n个数,q个询问,每个询问问你有多少对l r的gcd(a[l] , ... , a[r]) ...
- 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 ...
- 2016 Al-Baath University Training Camp Contest-1 A
Description Tourist likes competitive programming and he has his own Codeforces account. He particip ...
- [CFGym101028] 2016 Al-Baath University Training Camp Contest-1
比赛链接:http://codeforces.com/gym/101028/ 由于实习,几乎没有时间刷题了.今天下午得空,断断续续做了这一套题,挺简单的. A.读完题就能出结果. /* ━━━━━┒ギ ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- oracle SQL语句(转)
Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...
- vim note (1)
'vim' go into the vim mode 'i' 'a' 's' is means insert mode 'v' is means visual mode 'esc' is mea ...
- java类加载机制的代码实例
package typeinfo; import java.util.Random; class Initable { static final int staticFinal = 47 ; stat ...
- quilt - 制作patch的工具
quilt - 制作patch的工具 在尝试为openwrt做一个patch时,查到这个工具.openwrt官方已经有很详细的文档对步骤进行说明了. quilt并不是专为openwrt的开发工具.qu ...
- Android如果动态改变CursorAdapter Item个数
//adapter内部类 private class SearchAdapter extends CursorAdapter { @Override public View newView(Conte ...
- archlinux yaourt安装 以及出错细节 database file for "archlinuxfr" does not exist.
archlinux yaourt安装 但一直报错如下: :: Synchronizing package databases... core is up to date extra is u ...
- python安装easy_install和pip
1 安装easy_install https://pypi.python.org/pypi/setuptools 下载setuptools 执行python setup.py install就安装成功 ...
- webdriver.close() quit() 批量kill进程 内存耗尽的解决办法
问题现象: shell窗口卡,换IP的登录窗,不开: 猜测: 内存耗尽 spider_url,py driver = webdriver.PhantomJS( executable_path='/us ...
- Codility经典算法题之九:MissingInteger
Task description: This is a demo task. Write a function: that, given an array A of N integers, retur ...
- 如何使用git 生成patch 和打入patch【转】
本文转载自:http://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用 ...