题目链接:点击打开链接

#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. JS 2016-09-30T22:04:27.5220743+08:00 转换为日期

    1.转换代码 new Date(item.CreatedDate).Format("yyyy-MM-dd hh:mm") 2.需要拓展的方法 // 对Date的扩展,将 Date ...

  2. ORACLE客户端乱码

    sqlplus 打开CMD窗口,出现乱码情况的解决办法 C:\Documents and Settings>set NLS_LANG=american_america.AL32UTF8 C:\D ...

  3. 基于visual Studio2013解决C语言竞赛题之0612递归

     题目

  4. ORA-01093: ALTER DATABASE CLOSE only permitted with no sessions connected解决方法

    在进行物理主备库角色转换的时候遇到ORA-01093错误 SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY; ALTER ...

  5. php连接oracle及简单操作

    使你的php支持oracle,按照以下步骤即可: 1.安装php环境,找一下appserv或者xampp,一键安装,很方便 2.把php的ext目录下的php_oci8.dll拷到system32目录 ...

  6. C++操作符的优先级

    C++操作符的优先级 C++操作符的优先级 操作符及其结合性 功能 用法 L L L :: :: :: 全局作用域 类作用域 名字空间作用域 ::name class::name namespace: ...

  7. SqlParameter参数化查询

    上篇博客写了关于重构代码用到的SQLHelper类,这个类包括四种函数,根据是否含参和是否有返回值各分两种.在这里写写传参过程用到的SqlParameter. 如果我们使用如下拼接sql字符串的方式进 ...

  8. oncreate 测量尺寸

    在android中,在oncreate里面只是将布局信息设置好,并没有进行布局,因此是没法进行测量view或者屏幕的长高,可以通过下面的observer来观察,当view布局完成之后会回调下面的两个接 ...

  9. Java面试题精选(二)线程编程、数据库理论和Jdbc部分

    —— 线程编程.数据库理论和Jdbc部分内容 ——     数据库的开发应用想必是我们日常所碰到最多的知识点了,大致可分为:oracle.MySQL.SQL Server.Hadoop. NoSQL. ...

  10. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...