Range Modular Queries

题意

给出一个数列,q个查询,问查询区间内有几个数 a[i] % x == y。

分析

其实裸的分块就能过了,跑的还特别快。

这里分块的作用就是排序。

在x较小时可以暴力打表,x较大时枚举显得更加高效。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 4e4 + 10;
const int BLOCK = 200;
int n, q, L, R;
int a[MAXN];
int ans[MAXN];
int mp[4 * MAXN];
struct block
{
int l, r, x, y;
int id, bid;
bool operator < (const block& other) const
{
if(bid == other.bid) return r < other.r;
return bid < other.bid;
}
}b[MAXN]; void query(int l, int r, int id, int x, int y)
{
if(id)
{
for(int i = l; i < L; i++) mp[a[i]]++;
for(int i = R + 1; i <= r; i++) mp[a[i]]++;
for(int i = L; i < l; i++) mp[a[i]]--;
for(int i = r + 1; i <= R; i++) mp[a[i]]--;
}
else
{
for(int i = l; i <= r; i++)
mp[a[i]]++;
}
for(int i = 0; i < MAXN; i += x)
ans[b[id].id] += mp[i + y];
L = l; R = r;
} int main()
{
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 0; i < q; i++)
{
scanf("%d%d%d%d", &b[i].l, &b[i].r, &b[i].x, &b[i].y);
b[i].id = i;
b[i].bid = b[i].l / BLOCK;
}
sort(b, b + q);
for(int i = 0; i < q; i++) query(b[i].l, b[i].r, i, b[i].x, b[i].y);
for(int i = 0; i < q; i++) printf("%d\n", ans[i]);
return 0;
}

code

#include<bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MAXN = 4e4 + 10;
const int MAXK = 205;
int a[MAXN], b[MAXN];
vector<int> pos[MAXK][MAXK];
vector<int> poss[MAXN];
int main()
{
int n, q;
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
for(int i = 1; i < MAXK; i++)
for(int j = 0; j < n; j++)
pos[i][a[j] % i].push_back(j);
for(int i = 0; i < n; i++)
{
poss[a[i]].push_back(i);
}
while(q--)
{
int l, r, x, y;
scanf("%d%d%d%d", &l, &r, &x, &y);
if(x < MAXK) // 打表
{
int ans = upper_bound(all(pos[x][y]), r) - lower_bound(all(pos[x][y]), l);
printf("%d\n", ans);
}
else // x >= MAXK ,下面的循环会非常高效
{
int ans = 0;
for(int i = y; i < MAXN; i += x)
{
ans += upper_bound(all(poss[i]), r) - lower_bound(all(poss[i]), l);
}
printf("%d\n", ans);
}
}
return 0;
}

Range Modular Queries的更多相关文章

  1. arc098E Range Minimum Queries

    题意:给你一个n个数的数组,每次能够选取连续的长度为K的子序列,取出其中任意一个最小元素. 一共操作Q次.问取出的元素中Max-Min最小是多少? 标程: #include<bits/stdc+ ...

  2. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  3. MySQL Range Optimization

    8.2.1.3 Range Optimization MYSQL的Range Optimization的目的还是尽可能的使用索引 The range access method uses a sing ...

  4. mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2

    3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...

  5. sysbench 压力测试

    200 ? "200px" : this.width)!important;} --> 介绍 sysbench是一个模块化.跨平台.多线程基准测试工具,主要用于测试不同系统参 ...

  6. DNS(企业级)

    构建DNS(企业级) 1.硬件选型 CPU:12C以上配置 内存:16G 网络:千兆 2.初始化系统配置 关闭 iptables service iptables stop chkconfig ipt ...

  7. DNS视图以及日志压力测试

    1    访问控制列表 配置在/etc/named.conf文件的最顶端    acl innct {        192.168.1.0/24;        127.0.0.0/8;    }; ...

  8. DNS主从服务,子域授权,view视图,日志系统,压力测试

    DNS主从服务,子域授权,view视图,日志系统,压力测试 DNS性能测试工具queryperfDNS查询过程: DNS主从建立: 环境: 主服务器:10.140.165.93 从服务器:10.140 ...

  9. openstack通过salt-cloud创建虚拟机

    saltstack有三大功能:远程执行.配置管理.云管理:saltstack的云管理就是通过salt-cloud完成的,salt-cloud是基于openstack来做的:salt-cloud能够管理 ...

随机推荐

  1. TDD 中关于mock一些理解

    最近在写代码的UT时case注意到: 在写某个类的test suit时,如果这个类既有组合(Composition),又有聚合关系(Aggregation). 组合关系(Composition):部分 ...

  2. IE和其他浏览器用JS新窗口打开的问题

    Chrome中 window.open(pageURL,name,parameters) pageURL 为子窗口路径 name 为子窗口句柄 parameters 为窗口参数(各参数用逗号分隔) 例 ...

  3. Extjs6组件——Form大家族成员介绍

    本文基于ext-6.0.0 一.xtype form一共有12种xtype,下面来一一举例说一下. 1.textfield 这个是用的最多的form之一. { xtype: 'textfield', ...

  4. LinkCode 下一个排列、上一个排列

    http://www.lintcode.com/zh-cn/problem/next-permutation-ii/# 原题 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列 ...

  5. Linux(ubuntu)安装MediaWiki

    本篇文档所述步骤,作者完全验证过.一切OK. 作者:http://gaoxingf.blog.51cto.com/612518/188132,Younger Liu 本作品采用知识共享署名-非商业性使 ...

  6. 蓝桥杯-凑算式-java

    /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2016, 广州科技贸易职业学院信息工程系学生 * All rights reserved. * 文件名称: ...

  7. 记录——excel导出lua工具(python实现)

    项目需要一个从excel导出lua配置表的工具,之前的工具是主程写的,效率极差,i7 CPU 一次全部导出要花掉1个多小时.匪夷所思的是,这么渣的效率,居然用了整整一年.当 然,中途有人反映效率差,主 ...

  8. 安卓模拟器tools修改

    defaults write com.apple.finder AppleShowAllFiles -bool true 这步是显示隐藏文件夹, 然后打开finder,在应用程序上右键,选择在上层文件 ...

  9. hdu2059 龟兔赛跑 DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 虽然 知道是DP ,刚开始一直没有想出状态转移方程. 刚开始的思路就是定义dp[i]表示到达第i ...

  10. 深入理解Java常用类----String

         Java中字符串的操作可谓是最常见的操作了,String这个类它封装了有关字符串操作的大部分方法,从构建一个字符串对象到对字符串的各种操作都封装在该类中,本篇我们通过阅读String类的源码 ...