NOIP模拟 - 莫队
题目描述
给定一个元素个数为 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模拟 - 莫队的更多相关文章
- [BZOJ3757]苹果树(树上莫队)
树上莫队共有三种写法: 1.按DFS序列分块,和普通莫队类似.常数大,不会被卡. 2.按块状树的方式分块.常数小,会被菊花图卡到O(n). 3.按[BZOJ1086]王室联邦的方式分块.常数小,不会被 ...
- 20181009noip HZ EZ两校联考sum(莫队,组合数学)
题面戳这里 思路: noip考莫队???!!! 考场上死活没往这方面想啊!!!数据分治忘写endl50pts滚粗了 这里每个询问都有n,m两个参数 我们可以把它看做常规莫队中的l和r 然后利用组合数的 ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- 2018.11.07 NOIP训练 L的鞋子(权值分块+莫队)
传送门 乱搞题. 我直接对权值分块+莫队水过了. 不过调了30min30min30min发现ststst表挂了是真的不想说什么233. 代码
- 2018.10.29 NOIP训练 数据结构(带修改莫队)
传送门 带修莫队板题. 直接按照经典写法做就行了. 代码
- [Ynoi2019模拟赛]Yuno loves sqrt technology II(二次离线莫队)
二次离线莫队. 终于懂了 \(lxl\) 大爷发明的二次离线莫队,\(\%\%\%lxl\) 二次离线莫队,顾名思义就是将莫队离线两次.那怎么离线两次呢? 每当我们将 \([l,r]\) 移动右端点到 ...
- 2018.10.23 NOIP训练 Leo的组合数问题(组合数学+莫队)
传送门 好题. 考察了莫队和组合数学两个知识板块. 首先需要推出单次已知n,mn,mn,m的答案的式子. 我们令f[i]f[i]f[i]表示当前最大值为第iii个数的方案数. 显然iii之后的数都是单 ...
随机推荐
- Traveler Nobita (zoj 3456 最小生成树)
Traveler Nobita Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita used a time machin ...
- 区分json与jsonp
JSON(JavaScript Object Notation)和JSONP(JSON with Padding)虽然只有一个字母的差别,但其实他们根本不是一回事儿,下边简单区分概括一下: JSON是 ...
- Javascript和jquery事件--鼠标移动事件mousemove
mousemove,一个监听元素上鼠标移动的事件,如果鼠标在元素上移动,大概每16毫秒触发一次.我觉得挺有趣的一个元素,不过有替代还是不太推荐,从这个事件的触发频率就可以看出它会拖慢响应速度,消耗资源 ...
- hcharts实现堆叠柱形图
<!DOCTYPE > <html> <head> <meta charset="utf-8"><link rel=" ...
- [Node] Catch error for async await
When we try to do MongoDB opration, mongoose return Promise, we can use async/await to simply the co ...
- Android之怎样用代码使编辑框等组件显示为圆角
圆角button实现 圆角button大家很常见.有时候你可能会使用ps来加工圆角图片来实现想要的效果, 今天通过简短的代码来达到这样的效果.(由于这个跟project无关.仅仅是一种效果,所以我就单 ...
- Declarative Widgets is a QML plugin that adds Qt Widgets support to QML
05.04.2018 Nathan Collins 8 comments FacebookTwitterGoogle+LinkedInEmail Declarative Widgets is a ...
- FastSocket学习笔记~RPC的思想,面向对象的灵活
首先非常感谢这位来自新浪的老兄,它开发的这个FastSocket非常不错,先不说性能如何,单说它的使用方式和理念上就很让人赞口,从宏观上看,它更像是一种远程过程的调用RPC,即服务器公开一些命令,供客 ...
- [Angular2 Form] Create custom form component using Control Value Accessor
//switch-control component import { Component } from '@angular/core'; import { ControlValueAccessor, ...
- JAVA Concurrent包 中的并发集合类
我们平时写程序需要经常用到集合类,比如ArrayList.HashMap等,但是这些集合不能够实现并发运行机制,这样在服务器上运行时就会非常的消耗资源和浪费时间,并且对这些集合进行迭代的过程中不能进行 ...