[原创]HDU 5869 Different GCD Subarray Query [区间gcd预处理+离线]【数据结构】
[原创]HDU 5869 Different GCD Subarray Query [区间gcd预处理+离线]【数据结构】
2017-01-29 23:14:26 Tabris_ 阅读数:240
博客爬取于2020-06-14 22:41:55
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54780836
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869
---------------------------------------------------------------------------------------------------------.
Different GCD Subarray Query
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1172 Accepted Submission(s): 444
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
2016 ACM/ICPC Asia Regional Dalian Online
---------------------------------------------------------------------------------------------------------.
题目大意:
给长度为N的序列,M次询问,每次询问【L,R】之间所有子区间的不同GCD有多少个、
解题思路:
固定左端点,然后离线预处理出结果,用树状数组或者线段树均可。
首先预处理出每一个查询右区间位置渐近到达1位置的所有gcd的结果,这个结果显然是单调递减的,且不超过
个数的, 这个显然很好想…
然后在采取更新数值,
将每一个新旧的gcd更新到树状数组即使的最靠右的位置(+1,-1),即可,每一次查询的时候 就直接在树状数组查询区间里的个数就行了.
然后vis[1e6]居然RE。。。。最后改了map才过、
时间复杂度
附本题代码
---------------------------------------------------------------------------------------------------------.
1 |
|