map+pair Bayan 2015 Contest Warm Up D题

2024-09-02 18:32
文章标签 map 2015 contest pair warm bayan

本文主要是介绍map+pair Bayan 2015 Contest Warm Up D题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


D. CGCDSSQ
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given a sequence of integers a1, ..., an andq queries x1, ..., xq on it. For each queryxi you have to count the number of pairs(l, r) such that 1 ≤ l ≤ r ≤ n and gcd(al, al + 1, ..., ar) = xi.

is a greatest common divisor ofv1, v2, ..., vn, that is equal to a largest positive integer that divides allvi.

Input

The first line of the input contains integer n, (1 ≤ n ≤ 105), denoting the length of the sequence. The next line containsn space separated integers a1, ..., an, (1 ≤ ai ≤ 109).

The third line of the input contains integer q, (1 ≤ q ≤ 3 × 105), denoting the number of queries. Then followsq lines, each contain an integer xi, (1 ≤ xi ≤ 109).

Output

For each query print the result in a separate line.

Sample test(s)
Input
3
2 6 3
5
1
2
3
4
6
Output
1
2
2
0
1
Input
7
10 20 3 15 1000 60 16
10
1
2
3
4
5
6
10
20
60
1000
Output
14
0
2
2
2
0
2
2
1
1

题意给你一个序列和q个询问,输出有多少个区间[l,r]满足区间gcd为询问的值。

思路:处理出每个区间的gcd(要注意合并区间),如果有询问则直接加上去。



#include <bits/stdc++.h>
using namespace std;
std::map<int, int> id;
vector<pair<int,int> >gcdList;
const int MAXN=100000+5;
int query[MAXN*3],A[MAXN];
long long ans[MAXN*3];
int _gcd(int a,int b){return b?_gcd(b,a%b):a;}
int ID(int x){if(!id[x])id[x]=id.size();return id[x];
}
int main(int argc, char const *argv[])
{int n;ios_base::sync_with_stdio(false);gcdList.clear();id.clear();cin>>n;for(int i=1;i<=n;i++)cin>>A[i];int q;cin>>q;for(int i=1;i<=q;i++){cin>>query[i];query[i]=ID(query[i]);}for(int i=1;i<=n;i++){for(int j=0;j<gcdList.size();j++)gcdList[j].first=_gcd(gcdList[j].first,A[i]);gcdList.push_back(make_pair(A[i],i));int cnt=1;//sort(gcdList.begin(), gcdList.end());for(int j=1;j<gcdList.size();j++){if(gcdList[j].first!=gcdList[cnt-1].first)gcdList[cnt++]=gcdList[j];}gcdList.resize(cnt);for(int j=0;j<gcdList.size();j++){if(id[gcdList[j].first]){int r=i+1;if(j+1<gcdList.size())r=gcdList[j+1].second;ans[id[gcdList[j].first]]+=r-gcdList[j].second;}}}for(int i=1;i<=q;i++)cout<<ans[query[i]]<<endl;return 0;
}




这篇关于map+pair Bayan 2015 Contest Warm Up D题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130730

相关文章

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

JavaScript中的Map用法完全指南

《JavaScript中的Map用法完全指南》:本文主要介绍JavaScript中Map用法的相关资料,通过实例讲解了Map的创建、常用方法和迭代方式,还探讨了Map与对象的区别,并通过一个例子展... 目录引言1. 创建 Map2. Map 和对象的对比3. Map 的常用方法3.1 set(key, v

Golang中map缩容的实现

《Golang中map缩容的实现》本文主要介绍了Go语言中map的扩缩容机制,包括grow和hashGrow方法的处理,具有一定的参考价值,感兴趣的可以了解一下... 目录基本分析带来的隐患为什么不支持缩容基本分析在 Go 底层源码 src/runtime/map.go 中,扩缩容的处理方法是 grow

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int