题目描述

给定一个元素个数为 n 的整数数组 a 和 Q 个问题,每个问题有 x,y 两个参数,请统计共有多少个整数 K 满足 K 在 a[x]…a[y] 中出现了恰好 K 次。

输入格式

第一行两个整数 n,Q,表示数组 a 的元素个数和询问数;

接下来一行 n 给整数,描述数组 a ;

接下来 Q 行,每行两个数 xi,yi(1≤xi≤yi≤n),表示询问的左右边界。

输出格式

输出 Q 行,每行一个整数表示满足询问的 K 的个数。

样例数据 1

输入

7 2

3 1 2 2 3 3 7

1 7

3 4

输出

3

1

备注

样例说明

Q1: 1、2、3 分别满足,所以共有 3 个数满足要求。

Q2: 2 满足,所以只有 1 个数满足要求。

数据范围

对 50% 的输入数据:1≤n,Q≤1000

对 100% 的输入数据:1≤n,Q≤100000,1≤a[i]≤109

题目分析

莫队裸题,因为不熟练所以第一次没有分块,导致时间复杂度退化。

一定要分块!!!

莫队的时间复杂度证明:

右端点移动:

首先我们考虑一个块里面的转移情况

由于一个块里面的询问都按右端点排序

所以我们右端点在一个块里面最多移动n次

有 O(√n)个块,那么同一个块内的右端点移动最多就是O(n√n)

然后考虑从一个块到另一个块导致的右端点变化

最坏情况,右端点由n到1,那么移动n次

有 O(√n)个块

那么从一个块到另一个块的事件只会发生O(√n)次……

所以这种右端点移动的次数也是O(n√n)次

没有别的事件导致右端点移动了

左端点移动:

同一个块里面,由于左端点都在一个长度为O(√n)的区间里面

所以在同一块里面移动一次,左端点最多变化O(√n)

总共有n个询问……

所以同一块里面的移动最多n次

那么同一个块里面的左端点变化最多是O(n√n)的

考虑跨越块

每由第i个块到第i+1个块,左端点最坏加上O(√n)

总共能加上O(√n)次

所以跨越块导致的左端点移动是O(n)的

综上,分块做法是O(n∗√n)。

code

#include<iostream>
#include<cstdio>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std; const int N = 100000;
int n, Q, cnt[N + 5], ans;
int a[N + 5], b[N + 5], len, ret[N + 5];
bool vst[N + 5];
int S;
struct node{
int x, y, id, bl;
node(){}
node(int _x, int _y, int o, int b):x(_x), y(_y), id(o), bl(b){}
inline bool operator < (const node &p) const{
if(bl != p.bl) return bl < p.bl;
return y < p.y;
}
}qry[N + 5]; inline int read(){
int i = 0, f = 1; char ch = getchar();
for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
if(ch == '-') f = -1, ch = getchar();
for(; ch >= '0'&& ch <= '9'; ch = getchar())
i = (i << 1) + (i << 3) + (ch - '0');
return i * f;
} inline void disc_init(){
sort(b + 1, b + n + 1);
len = unique(b + 1, b + n + 1) - (b + 1);
for(int i = 1; i <= n; i++){
a[i] = lower_bound(b + 1, b + len + 1, a[i]) - b;
cnt[a[i]]++;
}
} inline void wr(int x){
if(x < 0) x = -x, putchar('-');
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
} int main(){
// freopen("count.in", "r", stdin);
// freopen("count.out", "w", stdout);
n = read(), Q = read();
S = sqrt(n);
for(int i = 1; i <= n; i++) a[i] = b[i] = read();
for(int i = 1; i <= Q; i++){
int x = read(), y = read();
qry[i] = node(x, y, i, (x - 1) / S + 1);
}
sort(qry + 1, qry + Q + 1);
disc_init();
for(int i = 1; i <= n; i++){
if(vst[a[i]]) continue;
vst[a[i]] = true;
if(cnt[a[i]] == b[a[i]])
ans++;
}
int head = 1, tail = n;
for(int i = 1; i <= Q; i++){
int x = qry[i].x, y = qry[i].y;
while(head > x){
head--;
if(cnt[a[head]] == b[a[head]]) ans--;
cnt[a[head]]++;
if(cnt[a[head]] == b[a[head]]) ans++;
}
while(head < x){
if(cnt[a[head]] == b[a[head]]) ans--;
if(cnt[a[head]] - 1 == b[a[head]]) ans++;
cnt[a[head]]--, head++;
}
while(tail > y){
if(cnt[a[tail]] == b[a[tail]]) ans--;
if(cnt[a[tail]] - 1 == b[a[tail]]) ans++;
cnt[a[tail]]--, tail--;
}
while(tail < y){
tail++;
if(cnt[a[tail]] == b[a[tail]]) ans--;
cnt[a[tail]]++;
if(cnt[a[tail]] == b[a[tail]]) ans++;
}
ret[qry[i].id] = ans;
} for(int i = 1; i <= Q; i++)
wr(ret[i]), putchar('\n');
return 0;
}

NOIP模拟 - 莫队的更多相关文章

  1. [BZOJ3757]苹果树(树上莫队)

    树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...

  2. 20181009noip HZ EZ两校联考sum(莫队,组合数学)

    题面戳这里 思路: noip考莫队???!!! 考场上死活没往这方面想啊!!!数据分治忘写endl50pts滚粗了 这里每个询问都有n,m两个参数 我们可以把它看做常规莫队中的l和r 然后利用组合数的 ...

  3. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  4. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  5. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  6. 2018.11.07 NOIP训练 L的鞋子(权值分块+莫队)

    传送门 乱搞题. 我直接对权值分块+莫队水过了. 不过调了30min30min30min发现ststst表挂了是真的不想说什么233. 代码

  7. 2018.10.29 NOIP训练 数据结构(带修改莫队)

    传送门 带修莫队板题. 直接按照经典写法做就行了. 代码

  8. [Ynoi2019模拟赛]Yuno loves sqrt technology II(二次离线莫队)

    二次离线莫队. 终于懂了 \(lxl\) 大爷发明的二次离线莫队,\(\%\%\%lxl\) 二次离线莫队,顾名思义就是将莫队离线两次.那怎么离线两次呢? 每当我们将 \([l,r]\) 移动右端点到 ...

  9. 2018.10.23 NOIP训练 Leo的组合数问题(组合数学+莫队)

    传送门 好题. 考察了莫队和组合数学两个知识板块. 首先需要推出单次已知n,mn,mn,m的答案的式子. 我们令f[i]f[i]f[i]表示当前最大值为第iii个数的方案数. 显然iii之后的数都是单 ...

随机推荐

  1. seaJS注意点:

    1.require 是同步往下执行,require.async 则是异步回调执行.require.async 一般用来加载可延迟异步加载的模块.

  2. Docker---(6)问题:bash: vi: command not found

    原文:Docker---(6)问题:bash: vi: command not found 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://bl ...

  3. [置顶] WebService学习总结(1)——WebService相关概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  4. [D3] Add image to the node

    We can create node with 'g' container, then append 'image' to the nodes. // Create container for the ...

  5. 通过WMI的方式去设置LCD背光亮度

    code例如以下: #include "stdafx.h" #include <objbase.h> #include <windows.h> #inclu ...

  6. hadoop组件及其作用

    1.hadoop有三个主要的核心组件:HDFS(分布式文件存储).MAPREDUCE(分布式的计算).YARN(资源调度),现在云计算包括大数据和虚拟化进行支撑. 在HADOOP(hdfs.MAPRE ...

  7. 软件——keil的查找,错误,不能跳转到相应的行

    为什么MDK  keil4.7双击搜索结果不能跳转到相应位置 KEIL搜索的时候双击不跳转到相应的位置 为什么keil点击不能跳转到错误处的问题 在keil中,双击Find In Files中某一行, ...

  8. sshfs 通过ssh 挂载远程目录

    安装:yum -y install sshfs 挂载远程 ssh 文件系统: sshfs -p 1234 root@192.168.1.218:/home/ /mnt/ sshfs -p SSH端口 ...

  9. 5W1H分析法和5W2H分析法

    5W1H分析法也称六何分析法,是一种思考方法,也可以说是一种创造技法.是对选定的项目.工序或操作,都要从原因(WHY).对象(WHAT).地点(WHERE).时间(WHEN).人员(WHO).方法(H ...

  10. 第一个hello word 驱动载入失败--------

    今天尝试自己载入第一个驱动模块,依据惯例hello word 然后失败了,如今说明我的操作过程.请个位看看. 首先我的内核版本号: 模块代码与MAKEFILE #include<linux/in ...