传送门

#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. 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S

    本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...

  2. 安装apache ActiveMQ

    笔者环境 操作系统centos 6.9 java jdk 1.8 activemq版本 5.15.9 a)安装activemq需要jdk 环境,这里使用的是 jdk 1.8 yum安装jdk比较简单, ...

  3. 每天进步一点点------ModelSim仿真Altera的ROM

    1. 在QuartusII中生成rom的初始化文件,可以是hex,也可以是mif.MIF文件的格式很简单明了,所以我一向都是用MIF. 2.下载convert_hex2ver.dll文件,conver ...

  4. 【做题笔记】洛谷P1464 Function

    我先谔谔一波 /kk 我谔谔 看题第一眼:欸这不就是按题意递归嘛,,直接搞不就好了 3 min 后,重新看题 然后自己手玩了几个样例,噢,递归太多了,铁定会 T 啊...... 然后,作为一个从没写过 ...

  5. Attribute "resultType" must be declared for element type "update" or "insert"

    仔细查看错误如图所示: 解决错误就是把resultType去掉,因为在insert和update语句中是没有返回值的.小坑小坑 转自:https://blog.csdn.net/u013144287/ ...

  6. 【转载】Java反射机制详解

    转自:http://baike.xsoftlab.net/view/209.html#3_8 1反射机制是什么 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对 ...

  7. 对象析构谈—— delete this 的使用及注意事项

    this对象是必须是用 new操作符分配的(而不是用new[],也不是用placement new,也不是局部对象,也不是global对象): delete this后,不能访问该对象任何的成员变量及 ...

  8. Duizi and Shunzi HDU

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. go基础_控制语句

    if控制语句 说明:(1)if后面的条件语句不用加括号 (2)if后面可以跟一个简单的初始化语句,并以分号分割,初始化语句中的变量的作用域是整个if语句块 (3)if语句的条件语句需要尽量简单 (4) ...

  10. 使用dockerfile构建镜像并在容器中安装软件遇到的问题

    今天想在容器中安装一个pigz插件,于是就在dockerfile中使用RUN命令RUN apt-get install -y pigz结果构建镜像的时候报错Unable to locate packa ...