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 ...
随机推荐
- 微信环境中如何实现跳转到手机默认外部浏览器下载apk文件
由于微信的限制,应用文件在内置浏览器中下载全部被屏蔽掉,造成很多人用微信扫描二维码下载时点击下载按钮没反应,我想到的是做一个提示用户在浏览器中打开下载.其实原来很简单,就是判断当前是在微信内置浏览器中 ...
- 题解【洛谷P1938】 [USACO09NOV]找工就业Job Hunt
题面 题解 将路径连边\((x, y, d)\) ,将航线连边\((x, y, d - w)\).其中线路是从\(x\)到\(y\),航线的费用为\(w\),\(d\)的含义如题面. 跑一遍\(SPF ...
- Educational Codeforces Round 82 (Rated for Div. 2)D(模拟)
从低位到高位枚举,当前位没有就去高位找到有的将其一步步拆分,当前位多余的合并到更高一位 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h&g ...
- LED Decorative Light Manufacturer - Led Wall Lamp Performance Characteristics
LED Decorative Light Manufacturer introduction: LED wall lamp is a light-emitting diode as a ligh ...
- Linux新建用户,切换后只显示$问题
1,执行以下命令创建一个新的用户 useradd -d /home/sam -m sam -s /bin/sh -g group -G adm,root 这个命令中指定了这个用户登录的shell 是/ ...
- AJAX--XMLHttpRequest对象
创建XMLHttpRequest对象 XMLHttpRequest是AJAX的基础. 所有现代浏览器(IE7+.Firefox.Chrome.Safari以及Opera)均内建XMLHttpReque ...
- PHP无法获取.env文件设置的值
EGPCS指代的是什么? 指的是可以从服务器配置和请求的信息中获取的信息,它包括了environment(环境).GET.POST.cookie.server,对应的全局数组:$_ENV.$_ ...
- try catch 语句中有return 的各类情况
在牛客上做java题时遇到过多到关于try catch语句的问题,看了很多答案解析,在这里记录一下. 首先给出一道题目: 下面代码的运行结果为? A.catch语句块 和是43 B.编译异常 C.fi ...
- MinGW dll导入导出类
dll不仅可以导入导出函数,还可以导入导出类.这篇文章就来介绍如何将类导入dll中并导出. 首先我们建立一个名为dll.cpp的文件(又是这种破名字),里面写上: #include <iostr ...
- maven一直加载2.0.0.M7 的 config server 失败
之前学习的时候使用F版的SpringBoot管理项目依赖一直好好的,今天不知idea为何抽疯,一直加载失败,各种重启,清除,没用 只能像之前学习注册consul 时将F版的SpringBoot 改为G ...