Description

One day,Little-Y saw many numbers standing in a row. A question suddenly appeared in her mind, ”From the L-th number to the R-th number, how many of them is a mutiple of P ? (P is a prime number) , and how quickly can I settle this problem ? ”

Input

Mutiple test cases. Please process till the end of file.

For each test case:

The first line contains two positive integers n and q (1<=n,q<=10^5), which means there are n integers standing in a row and q queries followed. 

The second line contains n positive integers, a1,a2,a3,…,an (no more than 10^6) . Here, ai means the i-th integer in the row.

The next are q queries, each of which takes one line. For each query, there are three integers L,R,P (1<=L<=R<=n, 1<=P<=10^6, P is gurantteed to be a prime number). Their meanings have been mentioned in the discription.

Output

For each query, output the answer in one line.

Sample Input

6 5
12 8 17 15 90 28
1 4 3
2 6 5
1 1 2
3 5 17
1 6 999983

Sample Output

2
2
1
1
0

HINT

Source


题意:
给出一个数组
然后m个询问,每次询问l,r,p,询问数组l~r区间内有几个p的倍数

思路:
首先打出素数表。然后对于数组每一个数分解质因子。将同样的质因子作为下标。存包括这些质因子的那些数的坐标,然后能够直接查询范围

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std; #define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 1000000
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
vector<int> prime[N+5];
bool vis[N+5];
int pos[N+5],tot; void set()
{
int i,j;
memset(vis,1,sizeof(vis));
for(i = 2; i<=N; i++)
{
if(vis[i])
{
if(N/i<i)
break;
}
for(j = (LL)i+i; j<=N; j+=i)
vis[j] = 0;
}
tot = 1;
for(i = 2; i<=N; i++)
if(vis[i])
pos[i] = tot++;
} int main()
{
set();
int i,j,n,m,x,y,l,r,L,R;
while(~scanf("%d%d",&n,&m))
{
for(i = 1; i<tot; i++)
prime[i].clear();
for(i = 1; i<=n; i++)
{
scanf("%d",&x);
for(j = 2; j*j<=x; j++)
{
if(x%j!=0)
continue;
prime[pos[j]].push_back(i);
while(x%j==0)
x/=j;
}
if(x>1)
prime[pos[x]].push_back(i);
}
for(i = 1; i<tot; i++)
sort(prime[i].begin(),prime[i].end());
while(m--)
{
scanf("%d%d%d",&l,&r,&x);
y = pos[x];
int len = prime[y].size();
if(len==0||l>prime[y][len-1]||r<prime[y][0])
{
printf("0\n");
continue;
}
L = lower_bound(prime[y].begin(),prime[y].end(),l)-prime[y].begin();
R = lower_bound(prime[y].begin(),prime[y].end(),r)-prime[y].begin();
if(R==len||prime[y][R]>r)
R--;
printf("%d\n",R-L+1); }
} return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

CSU1661: Query Mutiple的更多相关文章

  1. springJDBC和SpringJDBCTemplate解决方案探究

    先来看一个纯JDBC的例子,体会一下springJDBC和SpringJDBCTemplate两者的区别 一个Customer类 package com.mkyong.customer.model; ...

  2. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  3. 相关query挖掘

    1.何为相关query 我通常也把相关query称为相似query,搜索日志中一个用户在短时间内的一系列搜索词被称为相关query.相关就是两个query间有一定的关系,反映了用户在当时的需求.本文就 ...

  4. 第三篇 Entity Framework Plus 之 Query Cache

    离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...

  5. 第二篇 Entity Framework Plus 之 Query Future

    从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...

  6. FunDA(1)- Query Result Row:强类型Query结果行

    FunDA的特点之一是以数据流方式提供逐行数据操作支持.这项功能解决了FRM如Slick数据操作以SQL批次模式为主所产生的问题.为了实现安全高效的数据行操作,我们必须把FRM产生的Query结果集转 ...

  7. 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型

    在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...

  8. elasticsearch__5__java操作之FilterBuilders构建过滤器Query

    FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...

  9. [LeetCode] Range Sum Query 2D - Mutable 二维区域和检索 - 可变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

随机推荐

  1. [FindBugs分析记录]Redundant nullcheck of o,which is known to be non-null

    官网解释: This method contains a redundant check of a known non-null value against the constant null. 这种 ...

  2. Js regular exprission

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表 ...

  3. Linux redhat

    挂载U盘 fdisk -l 可以列出所有的分区,包括没有挂上的分区和usb设备.我一般用这个来查找需要挂载的分区的位置,比如挂上u盘. mount /dev/sdb1 usb/

  4. 手机端QQ客服直接跳转到QQ

    企业QQ呼出QQ对话框方法 1.手机端链接是这样的:mqqwpa://im/chat?chat_type=wpa&uin=386807630&version=1&src_typ ...

  5. MySql数据库3【优化3】缓存设置的优化

    1.表缓存 相关参数: table_open_cache 指定表缓存的大小.每当MySQL访问一个表时,如果在表缓冲区中还有空间,该表就被打开并放入其中,这样可以更快地访问表内容.通过检查峰值时间的状 ...

  6. PHP 获取目录

    取得当前文件名,当前目录,上层目录 文件名 test.php 路径 + 文件名 (要取得 /var/www/test/test.php)      echo __FILE__; 文件名 (要取得 te ...

  7. Python学习 - 编写一个简单的web框架(二)

    在上一篇日志中已经讨论和实现了根据url执行相应应用,在我阅读了bottle.py官方文档后,按照bottle的设计重写一遍,主要借鉴大牛们的设计思想. 一个bottle.py的简单实例 来看看bot ...

  8. 【转】python中List的sort方法(或者sorted内建函数)的用法

    原始出处:http://gaopenghigh.iteye.com/blog/1483864 python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. ...

  9. EasyUI篇のDataGrid

    HTML: <table id="dg"></table> 或者 <div id="dg"></div> JS: ...

  10. Java增强的泛型

    尽管Java 8是2014年年初才发布的,而Java 9要等到2016年年中,但是目前有一些计划放到某个未来版本(希望是Java 10)中的特性已经合并了进来. 具体而言,有两个比较大的特性已经开始原 ...