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 ...
随机推荐
- WPF自定义DataGrid分页控件
新建Custom Control,名:PagingDataGrid 打开工程下面的Themes\Generic.xaml xaml里面代码替换如下 <Style x:Key="{x:T ...
- 【技术宅11】php入门运算
//1.空bool $a=''; $b=NULL; $c=false; $d=0; $e='0'; $f=array(); $g=array(array()); $h='NULL'; var_dump ...
- windows 下 pgsql 拓展开启
环境:windowxp + apache2.2+php5.2.13+postgresql8.3 操作步骤:1.修改php.ini,去掉“extension=php_pgsql.dll ”和“exten ...
- Library:python-memcached on Windows
1 install memcached 1.4.4 Windows 32-bit 2 cd into the base file and type memcached.exe -d install ...
- Unix/Linux 'dirctory tree' command.
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' It se ...
- C# 当double数值较大且小数位过多时转化成字符串并保留小数位
今天在C#中碰到了一个问题,需要将double转换成字符串显示,要求保留小数位. 在网上查询了一下相关的文章 具体如下: double temp=3.1415926; (F)Fixed point:s ...
- EasyPR--开发详解
我正在做一个开源的中文车牌识别系统,Git地址为:https://github.com/liuruoze/EasyPR. 我给它取的名字为EasyPR,也就是Easy to do Plate Reco ...
- 【转】常见 jar包详解
转载自:http://www.cnblogs.com/xusir/archive/2013/05/19/3086878.html jar包 用途 axis.jar SOAP引擎包 commons- ...
- Sliding Window
poj2823:http://poj.org/problem?id=2823 题意:给出一个序列,要求得到所有长度为k的连续子序列的最大和最小值.题解:直接上线段树 #include<iostr ...
- PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中
PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中 效果图: html+jquery: <html> <head> <meta http-equiv=& ...