http://acm.hdu.edu.cn/showproblem.php?pid=5726

求不修改区间gcd可以用线段树或者倍增。

求l-n的我们注意观察gcd(a​l​​,a​l+1​​,...,a​r​​),当l固定不动的时候,r=l...n时,我们可以容易的发现,随着r的増大,gcd(a​l​​,a​l+1​​,...,a​r​​)是递减的,同时gcd(a​l​​,a​l+1​​,...,a​r​​)最多 有log 1000,000,000个不同的值,因为a​l​​最多也就有log 1000,000,000个质因数。

然后我们用链表记录所有gcd改变的点,这些点将l...n这一段分成若干个相同gcd的区间。由l...n的gcd关系可以推出l-1...n的gcd关系。相邻区间gcd相同时将两个区间合并。用map统计gcd是x的区间有多少个。

 #include<cstdio>
#include<cstring>
#include<map>
#define N 100010
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
int te,l[N*],r[N*],a[N*],f[N],x[N];
long long j,n,nex[N];
int t,m,ans,ll,rr,num,i,_;
map<int,long long> sc; int gcd(int aa,int bb)
{
int tt,a=aa,b=bb;
while (a%b!=)
{
tt=a;
a=b;
b=tt%b;
}
return b;
}
void build(int s,int ll,int rr)
{
l[s]=ll;r[s]=rr;
if (ll==rr)
{
scanf("%d",&x[++num]);
a[s]=x[num];
}
else
{
build(s*,ll,(ll+rr)/);
build(s*+,(ll+rr)/+,rr);
a[s]=gcd(a[s*],a[s*+]);
}
}
void sea(int s)
{
if (l[s]>rr || r[s]<ll) return;
if (ll<=l[s] && rr>=r[s])
if (ans==-)
ans=a[s];
else
ans=gcd(ans,a[s]);
else
{
sea(s*);
sea(s*+);
}
}
int main()
{
scanf("%d",&_);
while (_--)
{
printf("Case #%d:\n",++te);
num=;
mem(f);sc.clear();mem(nex);mem(l);mem(r);mem(a);
scanf("%d",&n);
build(,,n);
f[n]=x[n];sc[x[n]]++;
for (i=n-;i>=;i--)
{
j=i+;
while (j!=)
{
f[j]=gcd(f[j],x[i]);
j=nex[j];
}
f[i]=x[i];
nex[i]=i+;
j=i;
while (nex[j]!=)
{
if (f[j]==f[nex[j]])
nex[j]=nex[nex[j]];
else
j=nex[j];
}
j=i;
while (j!=)
{
if (nex[j]==)
sc[f[j]]+=n-j+;
else
sc[f[j]]+=nex[j]-j;
j=nex[j];
} }
scanf("%d",&m);
for (t=;t<=m;t++)
{
scanf("%d%d",&ll,&rr);
ans=-;
sea();
printf("%d %I64d\n",ans,sc[ans]);
}
}
return ;
}
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
17643445 2016-07-20 14:35:10 Accepted 5726 1950MS 14112K 1557B G++ lbz007

2016 Multi-University Training Contest 1 T4的更多相关文章

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

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

  2. 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 ...

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

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

  4. 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 ...

  5. 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 ...

  6. 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 ...

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

    Description The forces of evil are about to disappear since our hero is now on top on the tower of e ...

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

    Description Zaid has two words, a of length between 4 and 1000 and b of length 4 exactly. The word a ...

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

    Description X is well known artist, no one knows the secrete behind the beautiful paintings of X exc ...

随机推荐

  1. windows 不能在本地计算机启动apache2。有关更多信息,查阅系统事件日志。如果这是非Microsoft服务,请与服务厂商联系,并参考特定服务错误代码1

    今天使用apache的时候又无法启动了,之前也遇到过,这次重点说这一次的情况,其他情况可以查看博主apache相关的其他博文:网上关于apache服务端的设置的很多,但是都不适合我的情况: 一般使用a ...

  2. Docker的部署安装(CentOS)-by paymob

    环境准备 通过命令查看系统版本和内核版本等信息 [gmuser@--- ~]$ cat /etc/redhat-release CentOS Linux release (Core) [gmuser@ ...

  3. LG_3459_[POI2007]MEG-Megalopolis

    题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who onc ...

  4. 吴裕雄--天生自然python学习笔记:Python3 模块

    Python3 模块 在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了. 为此 Python 提供了一个办 ...

  5. 吴裕雄--天生自然 R语言开发学习:中级绘图(续二)

    #------------------------------------------------------------------------------------# # R in Action ...

  6. 为什么就连iPhone、三星手机的电池都能出问题?

    近年来关于三星.苹果.华为等知名手机厂商电池爆炸的消息一直不断在媒体上报道.这在一定程度上引发了消费者的重度忧虑,也给这些知名手机厂商从一定程度上造成了信任危机.为何连这些知名品牌都无法避免手机电池的 ...

  7. 码海拾遗:简单Socket(TCP)类实现

    最近刚开始啃Unix网络编程(卷1:套接字联网API),为加深TCP连接的建立和终止的理解与记忆,记下本文,方便以后翻看. 同时留下的还有简单的Socket(TCP)类: mySocket.h #pr ...

  8. ckeditor 捕获键代码

    <!--<script type="text/javascript"> var ctrlKey = false; var shiftKey = false; if ...

  9. 自动清理IIS log 日志脚本

    系统环境:windows server 2012 r2 IIS 版本:IIS8 操作实现清理IIS log File 脚本如下: @echo off ::自动清理IIS Log file set lo ...

  10. js数组冒泡排序、快速排序、插入排序

    1.冒泡排序 //第一种 function bubblesort(ary){ for(var i=0;i<ary.length-1;i++){ for(var j=0;j<ary.leng ...