题目描述

给一个长度为n的非负整数序列A1,A2,…,An。现有m个询问,每次询问给出l,r,p,k,问满足l<=i<=r且Ai mod p = k的值i的个数。

输入

第一行两个正整数n和m。
第二行n个数,表示A1,A2,…,An。
以下m行,每行四个数分别表示l,r,p,k。满足1<=l<=r<=n。

输出

对于每个询问,输出一行,表示可行值i的个数。

样例输入

5 2
1 5 2 3 7
1 3 2 1
2 5 3 0

样例输出

2
1


题解

根号分治+STL-vector+二分+莫队算法

对于$p\le \sqrt A$的部分,由于只有$\sqrt A$种模数,因此我们可以开$vector[i][j]$维护模$i$等于$j$的数的出现位置,查询时直接在$vector[p][k]$上二分即可。这个过程预处理时间是$O(n\sqrt A)$,单次查询时间是$O(log n)$。

对于$p>\sqrt A$的部分,考虑到满足$\mod p=k$的数最多只有$\sqrt A$个,因此可以暴力统计这些数中的每一个在序列中的出现次数。由于此时再使用vector+二分的话时间复杂度为$O(n\sqrt A\log n)$,会TLE,因此需要使用莫队算法并使用桶来维护每个数的出现次数。这样极限复杂度为$O(n\sqrt n+n\sqrt A)$。

因此总的最坏时间复杂度为$O(n\sqrt n+n\sqrt A)$。

#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v[210][210];
int a[100010] , cnt[10010] , ans[100010] , si , tot;
struct data
{
int l , r , p , k , id;
bool operator<(const data &a)const {return (l - 1) / si == (a.l - 1) / si ? r < a.r : l < a.l;}
}q[100010];
int main()
{
int n , m , i , j , w , x , y , z , lp = 1 , rp = 0;
scanf("%d%d" , &n , &m) , si = (int)sqrt(n);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%d" , &a[i]);
for(j = 1 ; j <= 100 ; j ++ )
v[j][a[i] % j].push_back(i);
}
for(i = 1 ; i <= m ; i ++ )
{
scanf("%d%d%d%d" , &w , &x , &y , &z);
if(y <= 100) ans[i] = upper_bound(v[y][z].begin() , v[y][z].end() , x) - lower_bound(v[y][z].begin() , v[y][z].end() , w);
else q[++tot].l = w , q[tot].r = x , q[tot].p = y , q[tot].k = z , q[tot].id = i;
}
sort(q + 1 , q + tot + 1);
for(i = 1 ; i <= tot ; i ++ )
{
while(lp > q[i].l) lp -- , cnt[a[lp]] ++ ;
while(rp < q[i].r) rp ++ , cnt[a[rp]] ++ ;
while(lp < q[i].l) cnt[a[lp]] -- , lp ++ ;
while(rp > q[i].r) cnt[a[rp]] -- , rp -- ;
for(j = q[i].k ; j <= 10000 ; j += q[i].p) ans[q[i].id] += cnt[j];
}
for(i = 1 ; i <= m ; i ++ ) printf("%d\n" , ans[i]);
return 0;
}

【bzoj2506】calc 根号分治+STL-vector+二分+莫队算法的更多相关文章

  1. 莫队算法/二分查找 FZU 2072 Count

    题目传送门 题意:问区间内x的出现的次数分析:莫队算法:用一个cnt记录x的次数就可以了.还有二分查找的方法 代码: #include <cstdio> #include <algo ...

  2. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  3. HDU5381【莫队算法+区间GCD特性】

    前言: 主要最近在刷莫队的题,这题GCD的特性让我对莫队的使用也有了新的想法.给福利:神犇的一套莫队算法题 先撇开题目,光说裸的一个莫队算法,主要的复杂度就是n*sqrt(n)对吧,这里我忽略了一个左 ...

  4. hdu5381 The sum of gcd]莫队算法

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...

  5. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  6. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  7. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  8. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  9. 信心题--FUOJ2226(莫队算法)

    http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...

随机推荐

  1. C-net总结

    SMB服务器信息块   DHCP动态主机配置协议 STMP简单邮件传输协议 POP(邮件协议) Gnutella   网络分析数据   nslookup DNS(域名系统)  请求注释(RFC)文件 ...

  2. Exception occurred during processing request: The given object has a null identifier: com.zsn.crm.Model.SaleVisit; nested exception is org.hibernate.TransientObjectException: The given object has a nu

    edit.jsp页面没有加入隐藏字段 id ,导致模型驱动封装时缺少id ,,调用update更新数据库时出错!

  3. MySQL巧用FIND_IN_SET和GROUP_CONCAT函数减少Java代码量

    数据库表简介:物品表 `id` int(11)  '物品id,唯一标识', `name` varchar(255) '物品名称', `level` int(11) '物品类别等级,礼品包为最高级1,类 ...

  4. centos7 多网卡修改默认路由

    最近在virtualbox里搭了一个centos7的虚拟机,但是网络这一块总是有问题. 单网卡下的问题: 1.当我配置连接方式为NAT网络地址转换的时候,虚拟机可以访问外网.但是在网络地址转换的情况下 ...

  5. php-5.6.26源代码 - opcode处理器的注入

    .初始化 opcode处理器列表 // main实现在文件“php-5.6.26\sapi\cgi\cgi_main.c” int main(int argc, char *argv[]) { if ...

  6. ASPX页面请求响应过程

  7. Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/config/springdemo-config.xml]

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...

  8. C# 在窗口绘制图形(打点、画圆、画线)

    需要包含命名空间 using System.Drawing; 画图前需要先创建画板 void Display() { Graphics g = this.CreateGraphics(); //创建画 ...

  9. 笔记-scrapy-extentions

    笔记-scrapy-extentions 1.      extentions 1.1.    开始 The extensions framework provides a mechanism for ...

  10. TouTiao开源项目 分析笔记13 最后一个订阅号的实现主页面

    1.实现订阅号的基础类 1.1.本地订阅号的Bean类==>MediaChannelBean public class MediaChannelBean implements Parcelabl ...