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. spring学习笔记四:spring常用注解总结

    使用spring的注解,需要在配置文件中配置组件扫描器,用于在指定的包中扫描注解 <context:component-scan base-package="xxx.xxx.xxx.x ...

  2. piranha(注意iptables和selinux的问题)

    piranha是红帽官方提供的一套工具,安装和配置都非常简单,可以快速部署. piranha方案原理结构描述: piranha方案是基于lvs基础上设计的一套负载均衡高可用解决方案 LVS运行在一对有 ...

  3. centos7 开机/etc/rc.local 不执行的问题(转载)

    最近发现centos7 的/etc/rc.local不会开机执行,于是认真看了下/etc/rc.local文件内容的就发现了问题的原因了 #!/bin/bash # THIS FILE IS ADDE ...

  4. ContentType明细对照表(文件类型相关的设置)

    文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流,不知道下载文件类型) application/octet-st ...

  5. Ceph块设备

    Ceph块设备 来自这里. 块是一个字节序列(例如,一个512字节的数据块).基于块的存储接口是最常见的存储数据的方法,它通常基于旋转介质,像硬盘.CD.软盘,甚至传统的9道磁带. 基本的块设备命令 ...

  6. 一月七笔千万美元投资!国内VR行业在刮什么风?

    虽然直到现在仍然没有一款真正能够彻底普及并改变大众操控方式的虚拟现实设备出现,但其已经被认定是未来人类社会中不可或缺的重要组成部分和工作.生活.娱乐.休闲载体.而虚拟现实设备.内容在今年年初CES展会 ...

  7. MySQL占用CPU超过百分之100解决过程

    本文转载自: https://www.93bok.com 访问网页504 Gateway Time-out,登陆服务器查看,内存正常,CPU使用率达到了400%,因为是4核,所以到了400%,几乎全部 ...

  8. 7-6 jmu_python_最大公约数&最小公倍数 (10 分)

    本题要求从键盘输入两个整数(以逗号间隔),编程求出这两个数的最大公约数和最小公倍数 提示:求最大公约数可用辗转相除法,最小公倍数用两数的积除以最大公约数 输入格式: 在一行中输入两个整数,以逗号间隔 ...

  9. javascript功能插件大集合 前端常用插件 js常用插件

    转载来源:https://github.com/jobbole/aw... 包管理器管理着 javascript 库,并提供读取和打包它们的工具.•npm – npm 是 javascript 的包管 ...

  10. a++与++a的陷阱与盲区

    故事发生在2019.7.15的3.pm,正在复习数据结构题目,写了一句如下指令(以下函数运行在win10的dev编译器下面) #include<bits/stdc++.h> using n ...