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. H5的本地存储

    localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...

  2. assert sys.modules[modname] is old_mod

    使用了pypiwin32 包中的pythoncom的时候,当跑在apache下,日志报错: [Thu Aug 27 17:06:44 2015] [error] [client 127.0.0.1] ...

  3. I题 - A+B for Input-Output Practice (VIII)

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description You ...

  4. IOS 企业版证书($299)In-House方式发布指南

    一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...

  5. uitextfield动态限制输入的字数-b

    1.定义一个事件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

  6. Java 中 Comparable 和 Comparator 比较(转)

    转自http://www.cnblogs.com/skywang12345/p/3324788.html 本文,先介绍Comparable 和Comparator两个接口,以及它们的差异:接着,通过示 ...

  7. BZOJ 3893 Cow Jog

    Description The cows are out exercising their hooves again! There are \(N\) cows jogging on an infin ...

  8. 电容式触摸控制器PCB布局

    在目前市场上可提供的PCB(印刷电路板)基材中,FR4是最常用的一种.FR4是一种玻璃纤维增强型环氧树脂层压板,PCB可以是单层或多层. 在触摸模块的尺寸受限的情况下,使用单层PCB不是总能行得通的, ...

  9. 6.ListView

    Repeater一般只用来展示数据,如果要增删改则用ListView更方便,使用向导(强类型数据)来使用ListView会自动生成很多模板,免去手写模板代码的麻烦,再进行手工调整即可. 首先设定数据源 ...

  10. XFS:大数据环境下Linux文件系统的未来?

    XFS:大数据环境下Linux文件系统的未来?   XFS开发者Dave Chinner近日声称,他认为更多的用户应当考虑XFS.XFS经常被认为是适合拥有海量数据的用户的文件系统,在空间分配方面的可 ...