HDU5869-Different GCD Subarray Query
来源:互联网 发布:网络电信诈骗宣传片 编辑:程序博客网 时间:2024/04/28 00:35
Different GCD Subarray Query
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1243 Accepted Submission(s): 467
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 arraya 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] .
Given an array
Input
There are several tests, process till the end of input.
For each test, the first line consists of two integersN 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
For each test, the first line consists of two integers
You can assume that
Output
For each query, output the answer in one line.
Sample Input
5 31 3 4 6 93 52 51 5
Sample Output
666
Source
2016 ACM/ICPC Asia Regional Dalian Online
Recommend
wange2014
题意:给出n个数,有q个询问,问区间【l,r】的所有子区间有几种公约数
解题思路:固定右端点,预处理 每个点向左延伸区间产生的不同gcd的值,gcd的个数不会超过log(r - l + 1)个,将所有询问离线按右端点从小到大排序,然后树状数组维护,枚举右端点,一个个求出当前右端点固定的时候,枚举所有的gcd值出现的位置,如果这个gcd前面没有出现过,那么它出现的时候就是这个位置并且这个位置 + 1,否则之前出现的位置 - 1,并且这个位置 + 1(因为后来再次出现的位置一定在之前出现的右边),最后区间查询
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int a[100005], n, Q;int c[100005];vector<pair<int, int> >q[100005], b[100005];int vis[1000050];int ans[100005];int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }int lowbit(int x){return x & (-x);}void add(int x, int add){while (x <= n){c[x] += add;x += lowbit(x);}}int getsum(int x){int sum = 0;while (x>0){sum += c[x];x -= lowbit(x);}return sum;}int main(){while (~scanf("%d%d",&n,&Q)){for(int i=1;i<=n;i++){scanf("%d",&a[i]);q[i].clear();b[i].clear();}for (int i = 1; i <= n; i++){int x = a[i], pos = i;b[i].push_back(make_pair(x, pos));for (int j = 0; j<b[i - 1].size(); j++){int k = b[i - 1][j].first; pos = b[i - 1][j].second;int g=gcd(k, a[i]);if (g != x){b[i].push_back(make_pair(g, pos));x = g;}}}for(int i=1;i<=Q;i++){int l, r;scanf("%d%d", &l, &r);q[r].push_back(make_pair(l, i));}memset(c, 0, sizeof c);memset(vis, 0, sizeof vis);for(int i=1;i<=n;i++){for (int j = 0; j<b[i].size(); j++){int x = b[i][j].first, y = b[i][j].second;if (vis[x]) add(vis[x], -1);vis[x] = y;add(y, 1);}for (int j = 0; j<q[i].size(); j++){int x = q[i][j].first, y = q[i][j].second;ans[y] = getsum(i) - getsum(x - 1);}}for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]);}return 0;}
0 0
- HDU5869-Different GCD Subarray Query
- hdu5869 Different GCD Subarray Query 线段树
- hdu5869 Different GCD Subarray Query(rmq+树状数组+gcd)
- HDU5869 Different GCD Subarray Query(线段树&&离线处理)
- hdu5869 Different GCD Subarray Query(预处理+树状数组)
- HDU5869 Different GCD Subarray Query (离线+树状数组)
- HDU5869 Different GCD Subarray Query(离线线段树)
- [离线+树状数组] HDU5869 Different GCD Subarray Query
- 【gcd分块+离线+树状数组】HDU5869[Different GCD Subarray Query]题解
- hdu5869——Different GCD Subarray Query(思考+树状数组)
- hdu5869——Different GCD Subarray Query(思考+树状数组)
- HDU5869 Different GCD Subarray Query(2016亚洲区大连站网络赛)
- HDU5869 Different GCD Subarray Query(线段树&&离线处理) 2016大连网络赛
- Different GCD Subarray Query HDU
- Different GCD Subarray Query (离线的处理)
- HDU 5869 Different GCD Subarray Query
- hdu 5869 Different GCD Subarray Query
- HDU 5869 Different GCD Subarray Query
- webpack学习-webpack.config.js配置
- 使用 C++11 智能指针时要避开的 10 大错误
- 第一次走上山东卫视
- javascript模块化开发之cmd规范
- 蓝桥杯 算法训练 最大最小公倍数 JAVA
- HDU5869-Different GCD Subarray Query
- 第四届蓝桥杯第三题
- uva 11082Matrix Decompressing
- 关于正则表达式的一些知识和函数总结
- AspNetPager+mysql+数据集+ObjectDataSource+ListView 做的翻页
- LeetCoe:Majority Element
- laravel5.1启动详解
- Problem C: 整数的封装
- 基础练习 时间转换