树状数组 gcd 查询 Different GCD Subarray Query
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
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].
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
1 3 4 6 9
3 5
2 5
1 5
6
6
#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的更多相关文章
- HDU 4630 No Pain No Game 树状数组+离线查询
		思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ... 
- hdu 1556 树状数组+点查询
		树状数组 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一 ... 
- bzoj 2743 树状数组离线查询
		我们按照询问的右端点排序,然后对于每一个位置,记录同颜色 上一个出现的位置,每次将上上位置出现的+1,上次出现的-1,然后 用树状数组维护就好了 /************************** ... 
- 【树状数组】2019徐州网络赛 query
		(2)首先成倍数对的数量是nlogn级别的,考虑每一对[xL,xR](下标的位置,xL < xR)会对那些询问做出贡献,如果qL <= xL && qR >= xR, ... 
- 【树状数组+离线查询】HDU 3333 Turing Tree
		https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ... 
- 【莫比乌斯反演+树状数组+分块求和】GCD Array
		https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/I [题意] 给定长度为l的一个数组,初始值为0:规定了两种操作: [思路] 找到 ... 
- HDU 3333 树状数组离线查询
		题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ... 
- HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)
		还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ... 
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
		<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ... 
- 【CF1042D】Petya and Array 离散化+树状数组
		题目大意:给定一个长度为 N 的序列,给定常数 t,求有多少个区间 [l,r] 满足 \(\sum\limits_{i=l}^{r}a_i<t\). 题解:先跑一边前缀和,问题等价于求有多少个数 ... 
随机推荐
- jQuery中jQuery.extend() 和 jQuery.fn.extend()的功能和区别
			昨天下午和今天上午断断续续的一直在看jQuery中jQuery.extend() 和 jQuery.fn.extend()两个函数的功能及区别,现在自认为是掌握的差不多了.好记性不如烂笔头,这里一方面 ... 
- java mongodb-crud
			本篇文章主要介绍了mongodb对应java的常用增删改查的api,以及和spring集成后mongoTemplate的常用方法使用,废话不多说,直接上代码: 1.首先上需要用到的两个实体类User和 ... 
- LINUX一网卡多IP设置
			方法1:少量IP手动绑定(这里以绑定IP到eth0为例,其它网卡的话修改相应的文件名即可) 1.复制ifcfg-eth0的网卡配置文件并改名为ifcfg-eth0:0 [root@akinlau /] ... 
- 设置umask
			umask 002 例子:umask为003,建立的文件与目录权限是什么? umask为003,所有去掉的属性为-------wx,因此 文件 -rw-rw-r-- 目录 drwxrwxr-- 
- 压力测试工具segie的使用
			压力测试工具segie的使用 使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/sie ... 
- UVA Live 3713 Astronauts (2-SAT)
			用布尔变量表示状态,把限制条件转化为XνY的形式以后跑2SAT,根据变量取值输出方案. #include<bits/stdc++.h> using namespace std; ; #de ... 
- 动态规划初步--最长上升子序列(LIS)
			一.问题 有一个长为n的数列 a0,a1,a2...,an-1a.请求出这个序列中最长的上升子序列的长度和对应的子序列.上升子序列指的是对任意的i < j都满足ai < aj的子序列. 二 ... 
- js 返回上一页并刷新页面
			js 方法 代码如下 self.location=document.referrer; 
- 使用prelu
			一个使用方式:http://blog.csdn.net/xg123321123/article/details/52610919 还有一种是像relu那样写,我就是采用的这种方式,直接把名字从relu ... 
- VC-基础:关于一些符号的意义
			GUI应用程序:Graphic User Interface图形 用户 接口 SDI:单文档程序(典型的记事本就是SDI) MID:多文档程序(比如VS2008默认就是多文档的) 
