传送门

#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的更多相关文章

  1. LightOJ 1188 Fast Queries(简单莫队)

    1188 - Fast Queries    PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...

  2. 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)) 在 ...

  3. mysql slow query---pt-query-digest----db structure consistency,monitor table records before and after transaction.

    将数据库脚本纳入版本管理是很必要的,尤其对于需要在客户那里部署升级的系统. 对于Python Django等框架,由于数据库是通过Model生成的,因此框架本身包括数据库升级工具,并通过代码版本间接管 ...

  4. MongoDB:The Definitive Guide CHAPTER 1 Introduction

    MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with ...

  5. MySQL的my-innodb-heavy-4G.ini配置文件的翻译

    我根据MySQL配置文件的英文文档说明,在根据自己所学的知识,使用有道词典对不懂的单词进行了查询,一个一个翻译出来的.有的专业术语翻译的不好,我使用了英文进行标注,例如主机(master)和副机(sl ...

  6. 【MYSQL】MYSQLの環境構築

    ダウンロード:https://dev.mysql.com/downloads/mysql/ 手順① 手順② mysql.iniの設定について [mysql]default-character-set= ...

  7. 【转】在一个Job中同时写入多个HBase的table

    在进行Map/Reduce时,有的业务需要在一个job中将数据写入到多个HBase的表中,下面是实现方式. 原文地址:http://lookfirst.com/2011/07/hbase-multit ...

  8. What is Druid?

    Druid is a data store designed for high-performance slice-and-dice analytics ("OLAP"-style ...

  9. 局部敏感哈希 Kernelized Locality-Sensitive Hashing Page

    Kernelized Locality-Sensitive Hashing Page   Brian Kulis (1) and Kristen Grauman (2)(1) UC Berkeley ...

随机推荐

  1. sudo用户找不到环境变量 sudo找不到/usr/local/bin 下的执行文件,

    出于安全方面的考虑,使用sudo执行命令将在一个最小化的环境中执行,环境变量都重置成默认状态. 所以PATH这个变量不包括用户自定义设置的内容,如找不到/usr/local/bin/下面的命令在sud ...

  2. django入门与实践(开)

    1.什么是Django? 基于python的高级web开发框架 高效 快速 免费 开源 正常上网流程 浏览器浏览网页的基本原理 请求响应过程 开发环境搭建 Python Django pip inst ...

  3. The entity type XXX is not part of the model for the current context.

    今天遇到了一个奇葩问题,虽然解决了,但还是一脸懵,先附赠一下别人的解决方案:https://www.cnblogs.com/zwjaaron/archive/2012/06/08/2541430.ht ...

  4. Python单元测试unittest与HTMLTestRunner报告生成

    本文为简单介绍,使用python自带模块unittest来进行单元测试 首先我们有一个需要测试的类,employee.py  定义了涨薪的方法.我们需要测试这个类的功能是否正确. class Empl ...

  5. 第十八篇 Linux环境下常用软件安装和使用指南

          提醒:如果之后要安装virtualenvwrapper的话,可以直接跳到安装virtualenvwrapper的方法,而不需要先安装好virtualenv   安装virtualenv和生 ...

  6. 链剖-What you are?-大话西游-校内oj2440

    This article is made by Jason-Cow.Welcome to reprint.But please post the writer's address. http://ww ...

  7. 506,display有哪些值?说明他们的作用

    block:转换成块状元素 inline:装换成行内元素 none:设置元素不可见 inline-block:想行内元素那样显示,但是其内容像块类型元素一样显示 list-item:想块类型元素一样显 ...

  8. 【经典数据结构】B树与B+树的解释

    本文转载自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 前面讲解了平衡查找树中的2-3树以及其实现红 ...

  9. ALSA driver--HW Buffer

    当app在调用snd_pcm_writei时,alsa core将app传来的数据搬到HW buffer(即DMA buffer)中,alsa driver从HW buffer中读取数据传输到硬件播放 ...

  10. js加密(七)steam登录

    1. url: https://store.steampowered.com/login/?redir=&redir_ssl=1 2. target: 登录 3. 分析 3.1 老样子,抓包, ...