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. magento前台访问错误

    前台访问出现错误 General error: 145 Table './dbname/tablename' ismarked as crashed and should be repaired 解决 ...

  2. html5系列.基础知识

    兼容性问题 创建一个html5页面 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  3. 使用Raphael 画图(四) 路径(一) (javascript)

    这章介绍路径,本人觉得这是比较难和精髓的一部分. 先介绍基本知识: 可以参考: http://www.chinasvg.com/support/svg-tutorial/svg-path-direct ...

  4. JS生成二维码,支持中文字符

    一.使用jquery-qrcode生成二维码 先简单说一下jquery-qrcode,这个开源的三方库(可以从https://github.com/jeromeetienne/jquery-qrcod ...

  5. iOS判断手机中是否 有 SIM卡---备用

    [CTSIMSupportGetSIMStatus() isEqualToString:kCTSIMSupportSIMStatusNotInserted]可以判断是否插入了sim卡. 前提是把下面的 ...

  6. JS获取select选中的值

    var oSel=oFl.getElementsByTagName('select')[0]; oSel.onchange=function(){ var indexselect=oSel.selec ...

  7. 应用SVN(CentOS中搭建SVN服务器)

    简单介绍如何在虚拟机 CentOS 中,搭建 SVN 服务器. 软件版本信息 Vmware 10.0.0 build-1295980 CentOS 7.0-1406-x64 Java 1.7.0_67 ...

  8. WPF开发技术介绍

    本月做了一个小讲座,主要是WPF的开发技术介绍,由于是上班时间,去听的人不多,但对于自己来说至少是又巩固了Winform的知识,抽时间写一篇文章,在此分享给大家,有什么宝贵建议大家也可以提给我,谢谢. ...

  9. DB2中的系统表SYSIBM.SYSDUMMY1

    ORACLE中有一张系统数据库表DUAL用来访问系统的相关信息 SELECT SYSDATE FROM DUAL;  --返回当前系统日期 ------------------------------ ...

  10. vs2012 aps.net4.0/4.5尚未在web服务器上注册

    安装了vs2015后,vs2012 启动后报错: aps.net4.0/4.5尚未在web服务器上注册 解决办法: 下载微软补丁: http://blogs.msdn.com/b/webdev/arc ...