树状数组 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\). 题解:先跑一边前缀和,问题等价于求有多少个数 ...
随机推荐
- Bootstrap基础知识学习
Bootstrap中文网 http://www.bootcss.com/ Bootstrap菜鸟教程 http://www.runoob.com/bootstrap/bootstrap-tutoria ...
- tomcat在idea中启动乱码
server,localhost log,catalina log分别乱码 打开tomcat/conf/目录下修改logging.properties 找到"utf-8"行更改为 ...
- vscode设置html默认浏览器
Vscode版本:1.30.2,设置方法:file→preference→settings,剩余设置如下图.
- arcgis jsapi接口入门系列(3):各种类型的图层添加
这里说的tomcat切片,是指arcgis server切片后,把切片图片文件用tomcat发布(其他任意web服务器发布都行) //添加tomcat切片图层 addTomcatTileLayer: ...
- Android自定义view之仿微信录制视频按钮
本文章只写了个类似微信的录制视频的按钮,效果图如下: 一.主要的功能: 1.长按显示进度条,单击事件,录制完成回调 2.最大时间和最小时间控制 3.进度条宽度,颜色设置 二.实 ...
- Chisel语言
1 What is Chisel? Chisel(Constructing Hardware In a Scala Embedded Language)是一种嵌入在高级编程语言Scala的硬 ...
- MySQL优化汇总
1)mysql优化汇总,转载自网络
- 通过StringBuilder的reverse()实现倒序
import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...
- Oracle错误(包括PL/SQL)集合与修复
+-----------------------------------------------------------------------+ | 在本篇随笔中,仅根据个人经验累积错误进行描述 ...
- Windows Dos命令下查看端口号,杀死端口
PS:本文以 Redis 默认端口 6379 为例 1,首先查询该端口的 pid,使用命令 [netstat -ano | findstr 端口号] F:\Program Files\Redi ...