Fast Queries
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> #define int long long
using namespace std;
const int maxn = 1e5 + ;
int T, n, q, block, a[maxn], ans, cnt[maxn], res[maxn]; struct node {
int l, r, id;
} t[maxn];
bool cmp(node a, node b) {//按照奇偶性排序
if ((a.l / block) == (b.l / block)) {//当左端点位于同一个块时
if ((a.l / block) % )//左端点的块序号为奇数时
return a.r < b.r;//按照从小到大排
else//左端点的块序号为偶数时
return a.r > b.r;//按照从大到小排
} else//当左端点不位于同一个块时
return a.l < b.l;//按照块的位置进行排序
//return (a.l/block)^(b.l/block) ? a.l<b.l : ( ((a.l/block)&1)?a.r<b.r:a.r>b.r );
} void add(int x) {
cnt[x]++;
if (cnt[x] == )
ans++;
} void del(int x) {
cnt[x]--;
if (!cnt[x])
ans--;
} void work() {
int L = t[].l, R = t[].l - ;
ans = ;
for (int i = ; i <= q; i++) {
while (L > t[i].l) add(a[--L]);
while (R < t[i].r) add(a[++R]);
while (L < t[i].l) del(a[L++]);
while (R > t[i].r) del(a[R--]);
res[t[i].id] = ans;
}
} signed main() {
//freopen("in", "r", stdin);
cin >> T;
for (int k = ; k <= T; k++) {
memset(cnt, , sizeof(cnt));
scanf("%lld%lld", &n, &q);
block = sqrt(1.0 * n);
for (int i = ; i <= n; i++)
scanf("%lld", &a[i]);
for (int i = ; i <= q; i++) {
scanf("%lld%lld", &t[i].l, &t[i].r);
t[i].id = i;
}
printf("Case %lld:\n", k);
sort(t + , t + q + , cmp);
work();
for (int i = ; i <= q; i++)
printf("%lld\n", res[i]);
}
return ;
}
Fast Queries的更多相关文章
- LightOJ 1188 Fast Queries(简单莫队)
1188 - Fast Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...
- Sona && Little Elephant and Array && Little Elephant and Array && D-query && Powerful array && Fast Queries (莫队)
vjudge上莫队专题 真的是要吐槽自己(自己的莫队手残写了2个bug) s=sqrt(n) 是元素的个数而不是询问的个数(之所以是sqrt(n)使得左端点每个块左端点的范围嘴都是sqrt(n)) 在 ...
- mysql slow query---pt-query-digest----db structure consistency,monitor table records before and after transaction.
将数据库脚本纳入版本管理是很必要的,尤其对于需要在客户那里部署升级的系统. 对于Python Django等框架,由于数据库是通过Model生成的,因此框架本身包括数据库升级工具,并通过代码版本间接管 ...
- MongoDB:The Definitive Guide CHAPTER 1 Introduction
MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with ...
- MySQL的my-innodb-heavy-4G.ini配置文件的翻译
我根据MySQL配置文件的英文文档说明,在根据自己所学的知识,使用有道词典对不懂的单词进行了查询,一个一个翻译出来的.有的专业术语翻译的不好,我使用了英文进行标注,例如主机(master)和副机(sl ...
- 【MYSQL】MYSQLの環境構築
ダウンロード:https://dev.mysql.com/downloads/mysql/ 手順① 手順② mysql.iniの設定について [mysql]default-character-set= ...
- 【转】在一个Job中同时写入多个HBase的table
在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式. 原文地址:http://lookfirst.com/2011/07/hbase-multit ...
- What is Druid?
Druid is a data store designed for high-performance slice-and-dice analytics ("OLAP"-style ...
- 局部敏感哈希 Kernelized Locality-Sensitive Hashing Page
Kernelized Locality-Sensitive Hashing Page Brian Kulis (1) and Kristen Grauman (2)(1) UC Berkeley ...
随机推荐
- 【C语言】复合函数求值
例子:求复合函数F(G(X)),其中F(x)=|x-3|+|x+1|,函数G(x)=x^2-3x. 分析:从复合函数的结构可以看出,F函数的自变量为G函数的绝对值,可以将F函数和G函数作为独立的函数实 ...
- 左偏树(p3377)
题目描述 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删除或第x和第y个数在用一个堆 ...
- js中yyyymmdd hh:mm:ss字符转换为Date
var dateString="20190102 10:30:35"; var pattern = /(\d{4})(\d{2})(\d{2})/; var formatedDat ...
- 【资源分享】半条命2速通AHK脚本
*----------------------------------------------[下载区]----------------------------------------------* ...
- 集群Session一致性和同步问题
一. 何为session 用户使用网站的服务,基本上需要浏览器和web服务器进行多次交互,web服务器如何知道哪些请求是来自哪个会话的? 具体方式为:在会话开始时,分配一个唯一的会话标识(sessio ...
- 每天进步一点点------如何实现Sobel Edge Detector? (Image Processing) (C/C++)
使用C與C++/CLI實現Sobel Edge Detector. http://www.cnblogs.com/oomusou/archive/2008/07/23/sobel_edge_detec ...
- samba文件共享及账户映射
samba文件共享及账户映射 实验介绍:在虚拟机Linux系统上安装sanmba服务,并在另外一台虚拟机的win7系统上访问共享文件夹,主要分为:匿名访问.身份验证访问.以及添加白名单和为了保护服务器 ...
- Eclipse导入git上的maven web项目 以及部署成功运行
在公司开发的时候都是用maven 以及git环境 开发的环境,那么我们初学者怎么将公司的项目成功导入到eclipse中,并且成功运行那???下面的教程就可以告诉大家~ (ps:第二步可能是大家会遇 ...
- 题解 SP7579 YOKOF - Power Calculus
SP7579 YOKOF - Power Calculus 迭代加深搜索 DFS每次选定一个分支,不断深入,直至到达递归边界才回溯.这种策略带有一定的缺陷.试想以下情况:搜索树每个节点的分支数目非常多 ...
- 7、Java类型转换
类型转换 自动类型转换 自动类型转换指的是容量小的数据类型可以自动转换为空量大的数据类型.(容量大小不是看字节数来定的,是按照类型可以容纳多的数来定的,所以long,可以自动转为float) //特例 ...