题目链接:点击打开链接

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if(x>9) pt(x/10);
putchar(x%10+'0');
}
/////////////////////////
const int N = 100000 + 2;
struct Node {
int pos,l,r;
ll gval;
Node(int pos = 0,int l = 0,int r = 0,ll gval = 0):pos(pos),l(l),r(r),gval(gval){}
bool operator < (const Node & a) const {
if(gval != a.gval) return gval < a.gval;
if(pos != a.pos) return pos < a.pos;
return r < a.r;
}
}; int n, a[N], tot;
vector<Node> vt[N];
Node node[N * 50];
ll sum[N * 50];
void prepare() {
for(int i = 0;i <= n;++i) vt[i].clear();
vt[n].push_back(Node(n,n,n,a[n]));
Node ntmp;
int cnt, Size;
ll x;
for(int i = n - 1;i >= 1;--i) {
Size = vt[i + 1].size();
cnt = 1;
vt[i].push_back(Node(i,i,i,a[i]));
for(int j = 0;j < Size;++j) {
ntmp = vt[i+1][j];
x = __gcd((ll)a[i],ntmp.gval);
if(cnt && vt[i][cnt-1].gval == x)
vt[i][cnt - 1].r = ntmp.r;
else
vt[i].push_back(Node(i,ntmp.l,ntmp.r,x)),cnt++;
}
}
tot = 0;
for(int i = 1;i <= n;++i)
for(int j = 0;j < (int)vt[i].size();++j)
node[++tot] = Node(vt[i][j]);
sort(node + 1,node + tot + 1);
sum[0] = 0;
for(int i = 1;i <= tot;++i)
sum[i] = sum[i-1] + node[i].r - node[i].l + 1;
}
int hehe;
void work(int x) {
int L, R, l = 0, r = tot + 1, mid;
while (r - l > 1) {
mid = (l + r) >> 1;
if (node[mid].gval > x)
r = mid;
else
l = mid;
}
-- r;
if (r == 0 || node[r].gval != x) {
putchar('0');
putchar('\n');
return ;
}
R = r;
l = 0; r = tot + 1;
while (r - l > 1) {
mid = (l + r) >> 1;
if (node[mid].gval >= x)
r = mid;
else
l = mid;
}
L = r;
pt(sum[R] - sum[L - 1]);
putchar('\n');
}
int main() {
rd(n);
for (int i = 1; i <= n; ++i)
rd(a[i]);
prepare();
int Q, x;
rd(Q);
while (Q -- > 0) {
rd(x);
work(x);
}
return 0;
}

Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数的更多相关文章

  1. Openjudge计算概论-求序列中的众数

    /*===================================== 求序列中的众数 总时间限制: 1000ms 内存限制: 65536kB 描述 输入一个长度为N的整数序列 (不多于128 ...

  2. Ping pong(树状数组求序列中比某个位置上的数小的数字个数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Time Limit: 2000/1000 MS (Java/Others) ...

  3. Codeforces 475D CGCDSSQ(分治)

    题意:给你一个序列a[i],对于每个询问xi,求出有多少个(l,r)对使得gcd(al,al+1...ar)=xi. 表面上是询问,其实只要处理出每个可能的gcd有多少个就好了,当左端点固定的时候,随 ...

  4. codeforces 475D. CGCDSSQ

    D. CGCDSSQ time limit per test 2 seconds memory limit per test 256 megabytes Given a sequence of int ...

  5. Codeforces 475D CGCDSSQ 区间gcd值

    题目链接 题意 给定一个长度为 \(n\) 的数列 \(a_1,...,a_n\) 与 \(q\) 个询问 \(x_1,...,x_q\),对于每个 \(x_i\) 回答有多少对 \((l,r)\) ...

  6. HDU - 2037 今年暑假不AC 贪心(求序列中不重叠子序列的最大值问题)

    HDU2037 今年暑假不AC  贪心算法 大意: 每次测试数据输入一个n,然后输入n对的电视节目播放时间:开始时间及结束时间, 求这个人能看的最多的完整的节目数. 解题思路: 对于这道解题,是对每个 ...

  7. td 中连续数字或连续英文内容不自动换行

    原因: 把连续的英文当做成了一个单词. 解决: 加上 : word-break: break-all (允许单词内换行)

  8. Task 4 求数组的连续子数组的最大和(团队合作)

    小组成员:李敏.刘子晗 1.设计思想:由于已经做过这个题目,只要对之前的程序加上相应的测试和约束即可.我们两个人一起商议后,决定了程序的主框架和并列出了最终可以实现的功能.先要定义数组长度和上下限的变 ...

  9. poj2182(线段树求序列第k小)

    题目链接:https://vjudge.net/problem/POJ-2182 题意:有n头牛,从1..n编号,乱序排成一列,给出第2..n个牛其前面有多少比它编号小的个数,记为a[i],求该序列的 ...

随机推荐

  1. Boost学习之可移植路径操作--filesystem

    Boost.Filesystem 库为对路径.文件和目录进行查询和操作提供了可移植的工具,已经被C++标准委员会接纳包含到TR2中. 编译 使用Boost.Filesystem 库之前要先编译它,请参 ...

  2. WCF技术剖析之十四:泛型数据契约和集合数据契约(下篇)

    原文:WCF技术剖析之十四:泛型数据契约和集合数据契约(下篇) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济频道<天天山海经>为此录制的节目视频(苏州话)]]在.NE ...

  3. CF 319D(Have You Ever Heard About the Word?-模拟)

    D. Have You Ever Heard About the Word? time limit per test 6 seconds memory limit per test 256 megab ...

  4. VB.net总结

    .NET视频差点儿相同用时一周结束,总体感觉就是"走耳不走脑".刚開始看视频理解起来有一点困难,台湾口音以及台湾与大陆计算机术语的差异,让我把前几集相当于直接忽略过了(建议打算看这 ...

  5. jQuery Validation让验证变得如此easy(一)

    一.官网下载jquery,和jquery validation plugin http://jqueryvalidation.org/ 二.引入文件 <script src="js/j ...

  6. canvas 多种形状绘图方法

    function canvasUploadImg(image,imageName,imgType,callbackfn){ var img_width = image.width; var img_h ...

  7. WPF的MVVM

    一.关于WPF WPF(Windows  Presentation Foundation) ,从名字来看,Microsoft想把WPF技术作为Windows程序外观(表现层)的基础.我们知道,现在开发 ...

  8. 安装Oracle时可能碰到的常见问题-1

    安装Oracle可能有些人觉得是一件非常easy的事情,但事实上是在安装的过程中蕴含着丰富的知识点.尤其安装在Linux平台,可能会碰到这样或那样各种诡异的问题,透过问题看到本质,这才是从深处理解Or ...

  9. 用log(N)的解法实现数值的整数次方

    // // main.m // c++test // // Created by andyyang on 6/3/13. // Copyright (c) 2013 andyyang. All rig ...

  10. JS - 鼠标经过边框旋转

    *右侧为鼠标经过时效果. 下载地址:http://www.lanrentuku.com/js/tupian-1200.html