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. Linux用户管理-用户账号管理

    一.用户账号的增.删.改.查 1>添加用户------useradd 注:1.用户名不应是纯数字或者以数字开头 2.将登陆shell改为/sbin/nologin可禁止用户登录 格式:usera ...

  2. cp参数详解

    -a 相当于pdr的意思 -d 若原文件为连接文件,则复制链接文件属性,而非文件本身 -f 强制复制,有重复时,不询问用户,而直接强制复制 -i 目标文件存在的话,先询问 -p 与文件的属性一起复制 ...

  3. ef导航属性

    https://msdn.microsoft.com/en-us/data/jj574232.aspx  场景是   A表中有B,B表中又C.都是一堆多的关系.怎样Mapping是个问题啊.  var ...

  4. 基于eclipse搭建android开发环境-win7 32bit

    基于eclipse搭建android开发环境-win7 32bit 前言:在使用朋友已搭建的Android开发环境时,发现朋友的开发环境版本较低且在update SDk时失败,便决定根据网上文章提示从 ...

  5. restful十项规范

    1.协议 API与用户的通信都是通过HTTPS协议进行的 2.域名 应尽量将API部署在专有域名下:https://api.example.com 如果确定API很简单,不会有什么扩展,则可以放在主域 ...

  6. ios UI自动化测试

    转载:http://www.cnblogs.com/dokaygang128/p/3517674.html 一.一些注意事项: 1.做自动化测试时注意如果是真机话首先要设置不锁屏. 2.自动化测试过程 ...

  7. codeforces 121 E. Lucky Array

    time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. An internal error occurred during: "Map/Reduce location status updater". java.lang.NullPointerException

    eclipse配置hadoop 2.6 服务器做的虚拟机,因为window是的hadoop会出现意想不到的错误,因为,我做了ubuntu的虚拟机供我使用 在虚拟机中进行映射设置 在eclipse中dr ...

  9. Latex 分块矩阵的处理

    在 \(\mathrm{\LaTeX}\) 中,如果想输入类似的矩阵: 可以这样实现: \[ \left[ \begin{array}{cc|cc|c} \lambda & 0 & 1 ...

  10. BZOJ1009: [HNOI2008]GT考试 (矩阵快速幂 + DP)

    题意:求一个长度为n的数字字符串 (n <= 1e9) 不出现子串s的方案数 题解:用f i,j表示长度为i匹配到在子串j的答案 用kmp的失配函数预处理一下 然后这个转移每一个都是一样的 所以 ...