Different GCD Subarray Query

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1328    Accepted Submission(s): 504

Problem Description
This is a simple problem. The teacher gives Bob a list of problems about GCD (Greatest Common Divisor). After studying some of them, Bob thinks that GCD is so interesting. One day, he comes up with a new problem about GCD. Easy as it looks, Bob cannot figure it out himself. Now he turns to you for help, and here is the problem:
  
  Given an array a of N positive integers a1,a2,⋯aN−1,aN; a subarray of a is defined as a continuous interval between a1 and aN. In other words, ai,ai+1,⋯,aj−1,aj is a subarray of a, for 1≤i≤j≤N. For a query in the form (L,R), tell the number of different GCDs contributed by all subarrays of the interval [L,R].
  
 
Input
There are several tests, process till the end of input.
  
  For each test, the first line consists of two integers N and Q, denoting the length of the array and the number of queries, respectively. N positive integers are listed in the second line, followed by Q lines each containing two integers L,R for a query.

You can assume that 
  
    1≤N,Q≤100000 
    
   1≤ai≤1000000

 
Output
For each query, output the answer in one line.
 
Sample Input
5 3
1 3 4 6 9
3 5
2 5
1 5
 
Sample Output
6
6
6
 
Source
 
Recommend
wange2014
长度n的序列, m个询问区间[L, R], 问区间内的所有子段的不同GCD值有多少种.
树状数组满足这种操作,但是知道这个我也不会啊,不懂后来那种统计操作
#include<bits/stdc++.h>
using namespace std;
const int N=;
int c[N],a[N],n,Q,last[N*],ans[N];
vector < pair<int,int> >q[N],b[N];
void add(int k,int num) {
while(k<=n) {
c[k]+=num;
k+=k&-k;
}
}
int read(int k) {
int res=;
while(k) {
res+=c[k];
k-=k&-k;
}
return res;
}
int main() {
while(~scanf("%d%d",&n,&Q)) {
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);
q[i].clear();
b[i].clear();
}
for(int i=; i<=n; i++) {
int x=a[i],y=i;
b[i].push_back(make_pair(x,y));
for(int j=; j<b[i-].size(); j++) {
int t=b[i-][j].first;
y=b[i-][j].second;
int g=__gcd(t,a[i]);
if(g!=x) {
b[i].push_back(make_pair(g,y));
x=g;
}
}
}
for(int i=; i<=Q; i++) {
int L,R;
scanf("%d%d",&L,&R);
q[R].push_back(make_pair(L,i));
}
memset(c,,sizeof(c));
memset(last,,sizeof(last));
for(int i=; i<=n; i++) {
for(int j=; j<b[i].size(); j++) {
int x = b[i][j].first,y= b[i][j].second;
if(last[x])
add(last[x],-);
last[x] = y;
add(y,);
}
for(int j=; j<q[i].size(); j++) {
int x=q[i][j].first,y=q[i][j].second;
ans[y]=read(i)-read(x-);
}
}
for(int i=; i<=Q; i++) printf("%d\n",ans[i]); }
return ;
}
 

树状数组 gcd 查询 Different GCD Subarray Query的更多相关文章

  1. HDU 4630 No Pain No Game 树状数组+离线查询

    思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...

  2. hdu 1556 树状数组+点查询

    树状数组 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ...

  3. bzoj 2743 树状数组离线查询

    我们按照询问的右端点排序,然后对于每一个位置,记录同颜色 上一个出现的位置,每次将上上位置出现的+1,上次出现的-1,然后 用树状数组维护就好了 /************************** ...

  4. 【树状数组】2019徐州网络赛 query

    (2)首先成倍数对的数量是nlogn级别的,考虑每一对[xL,xR](下标的位置,xL < xR)会对那些询问做出贡献,如果qL <= xL && qR >= xR, ...

  5. 【树状数组+离线查询】HDU 3333 Turing Tree

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ...

  6. 【莫比乌斯反演+树状数组+分块求和】GCD Array

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/I [题意] 给定长度为l的一个数组,初始值为0:规定了两种操作: [思路] 找到 ...

  7. HDU 3333 树状数组离线查询

    题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...

  8. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  9. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  10. 【CF1042D】Petya and Array 离散化+树状数组

    题目大意:给定一个长度为 N 的序列,给定常数 t,求有多少个区间 [l,r] 满足 \(\sum\limits_{i=l}^{r}a_i<t\). 题解:先跑一边前缀和,问题等价于求有多少个数 ...

随机推荐

  1. dbf 工程模式连接(vfp c# )

    首先现在微软官网下载“Microsoft OLE DB Provider for Visual FoxPro 9.0”驱动 下载完成后得到“VFPOLEDBSetup.msi” 双击安装即可在“C:\ ...

  2. Java项目接入阿里云OSS存储

    需求背景 目前公司内部项目所支持的文件云存储方式还是公司内部项目组提供的方案,但在时间的考验之下,弊端显现,尤其是灾备切换过程中需要切换访问地址,这种操作不方便,更可能因为中间过程的失误导致资源不可用 ...

  3. lnmp.org + phpstorm + xdebug

    lnmp.org下载安装包安装之: lnmp是个集成安装包,就不用自己在配置lnmp环境 安装phpstorm,破解方法:注册服务器为http://idea.lanyus.com 就可以了 xdebu ...

  4. GreenDao3.2的使用

    原文:http://blog.csdn.net/qq_30379689/article/details/54410838 GreenDao3.2的使用,爱不释手 本篇文章包括以下内容: 前言 Gree ...

  5. shell中的-z

    -z 字符串为"null",即是指字符串长度为零.

  6. IOS typedef 函数指针的用法

    代码简化, 促进跨平台开发的目的. typedef 行为有点像 #define 宏,用其实际类型替代同义字. 不同点:typedef 在编译时被解释,因此让编译器来应付超越预处理器能力的文本替换. 用 ...

  7. LR11安装和配置教程

    LoadRunner11安装教程 #安装包文件.汉化文件.破解文件,可以自行百科来获得,这边仅提供安装步骤. 1.前期准备1)安装前需要关闭防火墙及杀毒软件2)安装路径不能包含中文字符,同时需要以管理 ...

  8. vue2.0的变化

    1. 在每个组件模板,不在支持片段代码 组件中模板: 之前: <template> <h3>我是组件</h3><strong>我是加粗标签</st ...

  9. file-leak-detector(文件句柄泄漏)在JDK1.6环境下 weblogic 和 tomcat安装方式以及使用方式

    file-leak-detector作者博客详见: http://file-leak-detector.kohsuke.org/ file-leak-detector学习贴: https://blog ...

  10. 如何在Mac上放大

    您是否发现有时自己眯眼盯着屏幕,希望屏幕上的东西只是“大”一点?无论您是否视力差,或只是想放大屏幕来看近景,这是很容易做到,只需要按一些按键.这篇文章将告诉您如何放大看浏览器或桌面的特写.   方法 ...