CSU1661: Query Mutiple
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
#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的更多相关文章
- springJDBC和SpringJDBCTemplate解决方案探究
先来看一个纯JDBC的例子,体会一下springJDBC和SpringJDBCTemplate两者的区别 一个Customer类 package com.mkyong.customer.model; ...
- 以bank account 数据为例,认识elasticsearch query 和 filter
Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...
- 相关query挖掘
1.何为相关query 我通常也把相关query称为相似query,搜索日志中一个用户在短时间内的一系列搜索词被称为相关query.相关就是两个query间有一定的关系,反映了用户在当时的需求.本文就 ...
- 第三篇 Entity Framework Plus 之 Query Cache
离上一篇博客,快一周,工作太忙,只能利用休息日来写一些跟大家分享,Entity Framework Plus 组件系列文章,之前已经写过两篇 第一篇 Entity Framework Plus 之 A ...
- 第二篇 Entity Framework Plus 之 Query Future
从性能的角度出发,能够减少 增,删,改,查,跟数据库打交道次数,肯定是对性能会有所提升的(这里单纯是数据库部分). 今天主要怎样减少Entity Framework查询跟数据库打交道的次数,来提高查询 ...
- FunDA(1)- Query Result Row:强类型Query结果行
FunDA的特点之一是以数据流方式提供逐行数据操作支持.这项功能解决了FRM如Slick数据操作以SQL批次模式为主所产生的问题.为了实现安全高效的数据行操作,我们必须把FRM产生的Query结果集转 ...
- 细谈Slick(6)- Projection:ProvenShape,强类型的Query结果类型
在Slick官方文档中描述:连接后台数据库后,需要通过定义Projection,即def * 来进行具体库表列column的选择和排序.通过Projection我们可以选择库表中部分列.也可以增加一些 ...
- elasticsearch__5__java操作之FilterBuilders构建过滤器Query
FilterBuilders构建过滤器Query 代码如下: package com.elasticsearch; import org.elasticsearch.action.ActionList ...
- [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 ...
随机推荐
- Android ListView+image的使用
首先创建layout部局文件xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...
- VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)
Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = ...
- redis 服务器端命令
redis 127.0.0.1:6380> time ,显示服务器时间, 时间戳(秒), 微秒数 1) "1375270361" 2) "504511" ...
- C++实现base64编码
将昨天的php代码改造成C++ /*base_64.h文件*/ #ifndef BASE_64_H #define BASE_64_H /** * Base64 编码/解码 * @author lir ...
- block 数组排序
#import <Foundation/Foundation.h> //定义⼀一个block,返回值为BOOL,有两个NSString参数.实现:判 //断字符串是否相等. BOOL (^ ...
- WPF学习笔记-使用自定义资源字典(style)文件
1.添加资源字典文件style.xmal 2.在资源字典中添加自定义style等 <ResourceDictionary xmlns="http://schemas.microsoft ...
- coroutine协程
如果你接触过lua这种小巧的脚本语言,你就会经常接触到一个叫做协程的神奇概念.大多数脚本语言都有对协程不同程度的支持.但是大多编译语言,如C/C++,根本就不知道这样的东西存在.当然也很多人研究如何在 ...
- [BZOJ 3207] 花神的嘲讽计划Ⅰ【Hash + 可持久化线段树】
题目链接:BZOJ - 3207 题目分析 先使用Hash,把每个长度为 k 的序列转为一个整数,然后题目就转化为了询问某个区间内有没有整数 x . 这一步可以使用可持久化线段树来做,虽然感觉可以有更 ...
- [BZOJ 3236] [Ahoi2013] 作业 && [BZOJ 3809] 【莫队(+分块)】
题目链接: BZOJ - 3236 BZOJ - 3809 算法一:莫队 首先,单纯的莫队算法是很好想的,就是用普通的第一关键字为 l 所在块,第二关键字为 r 的莫队. 这样每次端点移动添加或删 ...
- 这十大MCU厂商瓜分着中国市场
MCU(Micro Control Unit)中文名称为微控制单元,又称单片微型计算机(Single Chip Microcomputer)或者单片机,是指随着大规模集成电路的出现及其发展,将计算机的 ...