题目链接:点击打开链接

#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. windows下使用openssl的一种方法

    下载openssl之后,全部解压到一个路径下,如:c:\program files\openssl sdk 举个例子,如使用SHA1,开发时引用头文件: #include <sha.h> ...

  2. QTabWidget 实现类似QQ聊天窗口(拖动分离出新的窗口)

    新版本的QQ聊天窗口可以实现拖动,分离出新的窗口.浏览器等软件也可以实现类似操作.所以心血来潮想用Qt实现类似的功能.想用QTabWidget直接实现是很难的,仔细阅读源码,发现QTabWidget内 ...

  3. Android Studio创建库项目及引用

    Android Studio创建库项目其实创建的是在主项目下创建Module模块,这个Module模块创建的时候选择库项目模式. 为什么要这样处理呢?因为在Android Studio中一个WorkS ...

  4. haproxy /admin跳转 不会在接口上再次加上admin

    http://www.xx.com/admin/api/menu [root@wx03 mojo]# cat test.pl use Mojolicious::Lite; use JSON qw/en ...

  5. Java基础04 封装与接口

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 总结之前的内容,对象(object)指代某一事物,类(class)指代象的类型.对 ...

  6. 简单区分`:before`与`::before`的区别

    简单区分:before与::before的区别 :hover我们都知道,称作伪类,英文名pseudo-class,而我们此处提到的:before以及:after也是伪类,属于css2的内容,在ie8下 ...

  7. 网站遭遇DDOS简易处理

    网站遭遇DDOS攻击 netstat -an | grep ESTABLISHED 我们看到有大量的链接存在着,并且都是ESTABLISHED状态 for i in `netstat -an | gr ...

  8. Maven项目红色叹号+JavaWeb: 报错信息The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java B

    昨天写的关于解决JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  9. android 设置字体颜色、EditText自己主动输入转换成大写字母的多种方式

    在TextView上面设置某一个字的字体颜色为指定颜色时,能够通过java类SpannableString类和Html语言来实现. (一)SpannableString类方式 private void ...

  10. asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师

    创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...