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 ...
随机推荐
- memcached优化方法
工作原理 基本概念:slab,page.chunk. slab,是一个逻辑概念. 它是在启动memcached实例的时候预处理好的,每一个slab相应一个chunk size.也就是说 ...
- linux 进程间通信之 消息队列
消息队列就是一个消息的链表. 能够把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程能够向中依照一定的规则加入新消息.有读权限的进程则能够读走消息. 读走就没有了.消息队列是 ...
- struts开发<在eclipse中配置struts. 一>
1.获取struts的jar包 1.1首先在http://struts.apache.org/download.cgi#struts23163这里下载 struts的文件包(选择struts-2.3. ...
- react 创建组件 (三)PureComponet
我们知道,当组件的props或者state发生变化的时候:React会对组件当前的Props和State分别与nextProps和nextState进行比较,当发现变化时,就会对当前组件以及子组件进行 ...
- pdf reference 格式具体说明
1. PDF概要 1.1. 图像模型 PDF能以平台无关.高效率的方式描叙复杂的文字.图形.排版. PDF 用图像模型来实现设备无关. 图像模型同意应用程序以抽象对象描叙文字.图像.图标.而不是通过详 ...
- oracle SQL语句(转)
Oracle数据库语句大全 ORACLE支持五种类型的完整性约束 NOT NULL (非空)--防止NULL值进入指定的列,在单列基础上定义,默认情况下,ORACLE允许在任何列中有NULL值. CH ...
- 利用python暴力破解ssh
# -*- coding:utf-8 -*- #python 2.7 import optparse,sys,threading import pexpect PROMPT = ['#','>' ...
- hdu 4858 项目管理(vector模拟)
# include <stdio.h> # include <algorithm> # include <string.h> # include <vecto ...
- Ajax的简单实现(Json)
之前写的是一般的Ajax if (request.status === 200) { document.getElementById("createResult").innerHT ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...