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 ;
}
 

最新文章

  1. Event Loop个人理解
  2. ASP.NET MVC 路由(三)
  3. UVA-11991 Easy Problem from Rujia Liu?
  4. Android计算器开发实例
  5. [CareerCup] 18.3 Randomly Generate Integers 随机生成数字
  6. 一个伪ajax图片上传代码的例子
  7. fb设置viewSourceURL
  8. java的单例设计模式
  9. linux开关机命令
  10. JSON-lib框架,转换JSON、XML不再困难
  11. Tencent分布式开源框架Pebble
  12. 给虚拟机添加新硬盘并分区,fdisk查看分区,分区,重新读取分区表信息partprobe,格式化,挂载,查看分区挂载信息,自动挂载文件/etc/fstab,/etc/fstab文件错误导致重启崩溃后的修复
  13. java keytool
  14. [深度应用]&#183;实战掌握Dlib人脸识别开发教程
  15. Windows的四类消息
  16. jQuery-4.动画篇---动画切换的比较(toggle与slideToggle以及fadeToggle的比较)
  17. java基本类型的默认值
  18. TPS和QPS的区别和理解
  19. JS快速入门
  20. makefile的一个错误:*** missing separator

热门文章

  1. poj1717
  2. codeforces1025
  3. NSTimer 实现时钟回调方法
  4. Codeforces Round #318 (Div. 2) D Bear and Blocks (数学)
  5. elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解
  6. spfa模板+讲解
  7. Bootstrap select(选择列表)
  8. 人脸识别中的检测(在Opencv中加入了QT)
  9. HTML防止重复提交
  10. CS193p Lecture 10 - Multithreating, UIScrollView