题目链接:点击打开链接

#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. HTML5给我们带来了什么

    websocket 根据维基定义 WebSocket 使得客户端和服务器之间的数据交换变得更加简单,允许服务端直接向客户端推送数据而不需要客户端进行请求,在 WebSocket API 中,浏览器和服 ...

  2. QScriptEngine

    其实你有好多没有介绍 比如qt文字 我一直很迷惑qt的文字的长宽 qt文字的字间距 等等这些东西还有QProcess QProcess可能是qt调用c#的唯一方法了QScript要比你想象的重要,一个 ...

  3. Offer_1

    #include <iostream> #include <cstring> using namespace std; class CMyString { public: CM ...

  4. Win8.1应用开发之动态磁贴

    using demo02.Common; using System; using System.Collections.Generic; using System.IO; using System.L ...

  5. poll调用深入解析

    poll调用深入解析http://blog.csdn.net/zmxiangde_88/article/details/8099049 poll调用和select调用实现的功能一样,都是网络IO利用的 ...

  6. OC中多线程的一些概念

    1.进程1.1>进程是指在系统中正在运行的一个应用程序(同时打开QQ和Xcode,系统会分别启动2个进程)1.2>每个进程之间是独立的,每个进程均运行在其专用的且受保护的内存空间内 2.线 ...

  7. C++中vector的实现

     注意几点: 分配内存不要使用new和delete,由于new的同一时候就把对象构造了.而我们须要的是原始内存. 所以应该使用标准库提供的allocator类来实现内存的控制.当然也能够重载ope ...

  8. State Design Pattern 状态设计模式

    设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换. 有点像Finite Automata算法,两者的思想是一样的. 会Finite Automata,那么这个设计模式就非常easy了. # ...

  9. 多屏广告技术调研 & 广告基础介绍

    之前做的多屏广告产品调研,并简单介绍了一些基础广告知识.见ppt:多屏广告技术调研

  10. git-daemon的快捷搭建

    使用git-daemon进行git服务器搭建 1.安装git-daemon 前提是已经安装git sudo apt-get install git git-core 然后安装git-daemon su ...