Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, indexed from 1 to n. Let's denote the number with index i as ai.
Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rjthe Little Elephant has to count, how many numbers x exist, such that number xoccurs exactly x times among numbers alj, alj + 1, ..., arj.
Help the Little Elephant to count the answers to all queries.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 ≤ lj ≤ rj ≤ n).
Output
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.
Examples
7 2
3 1 2 2 3 3 7
1 7
3 4
3
1 题意:
问区间内含有出现次数等于其值的数字个数
思路:
莫队。
没有什么值得解释的,只不过预处理离散化将时间由3790 ms优化到186 ms,必须写一个博客纪念一下。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[maxn];
struct node{
int l,r;
int id;
}a[maxm];
int anss[maxm];
int block; bool cmp(node a,node b){
return (a.l/block!=b.l/block)?a.l<b.l:a.r<b.r;
}
int vis[];
int rem[maxn],cnt;
int pos[maxn];
int get_id(int x){
return lower_bound(rem+,rem++cnt,x)-rem;
} int main()
{
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
rem[i]=num[i];
}
block=sqrt(n);
for(int i=;i<=m;i++){
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id=i;
} sort(a+,a++m,cmp);
sort(rem+,rem++n); cnt=unique(rem+,rem++n)-rem-;
for(int i=;i<=n;i++){
pos[i]=get_id(num[i]);
}
int L=,R=;
int ans=;
num[]=-;
for(int i=;i<=m;i++){
while(R<a[i].r){
R++;
if(vis[pos[R]]==num[R]){ans--;}
vis[pos[R]]++;
if(vis[pos[R]]==num[R]){ans++;}
}
while(L<a[i].l){
if(vis[pos[L]]==num[L]){ans--;}
vis[pos[L]]--;
if(vis[pos[L]]==num[L]){ans++;}
L++; }
while(R>a[i].r){
if(vis[pos[R]]==num[R]){ans--;}
vis[pos[R]]--;
if(vis[pos[R]]==num[R]){ans++;}
R--;
}
while(L>a[i].l){
L--;
if(vis[pos[L]]==num[L]){ans--;}
vis[pos[L]]++;
if(vis[pos[L]]==num[L]){ans++;}
}
anss[a[i].id]=ans;
}
for(int i=;i<=m;i++){
printf("%d\n",anss[i]);
}
return ;
}
Little Elephant and Array CodeForces - 220B (莫队)的更多相关文章
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...
- 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)) 在 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- XOR and Favorite Number CodeForces - 617E -莫队-异或前缀和
CodeForces - 617E 给n个数, m个询问, 每次询问问你[l, r]区间内有多少对(i, j), 使得a[i]^a[i+1]^......^a[j]结果为k.(注意 i ! = j) ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- D. Powerful array 莫队算法或者说块状数组 其实都是有点优化的暴力
莫队算法就是优化的暴力算法.莫队算法是要把询问先按左端点属于的块排序,再按右端点排序.只是预先知道了所有的询问.可以合理的组织计算每个询问的顺序以此来降低复杂度. D. Powerful array ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces 86D - Powerful array(莫队算法)
题目链接:http://codeforces.com/problemset/problem/86/D 题目大意:给定一个数组,每次询问一个区间[l,r],设cnt[i]为数字i在该区间内的出现次数,求 ...
随机推荐
- 洛谷P3286 [SCOI2014]方伯伯的商场之旅
题目:洛谷P3286 [SCOI2014]方伯伯的商场之旅 思路 数位DP dalao说这是数位dp水题,果然是我太菜了... 自己是不可能想出来的.这道题在讲课时作为例题,大概听懂了思路,简单复述一 ...
- UNP服务器设计范式总结
一:客户端 本章总结的服务器程序设计范式,使用同一个客户端程序进行测试.个连接.使用的命令如下: client 206.62.226.36 8888 5 500 4000 二:结论 上图测量 ...
- 高可用Kubernetes集群原理介绍
■ 文/ 天云软件 云平台开发工程师 张伟 1. 背景 Kubernetes作为容器应用的管理中心,对集群内部所有容器的生命周期进行管理,结合自身的健康检查及错误恢复机制,实现了集群内部应用层的高可用 ...
- c++ 模板和traits
#define TEST(ITEMNAME) AddItem(ITEMNAME, #ITEMNAME); template <typename T> void AddItem(T& ...
- IoT SaaS加速器——助力阿尔茨海默病人护理
场景介绍 阿尔茨海默病,是导致中老年人认知功能障碍的最常见疾病之一,是发生在老年期及老年前期的一种原发性退行性脑病.据估计,全世界痴呆症患者数量为4700万,到2030年将达到7500万人.痴呆症患者 ...
- @atcoder - AGC040C@ Neither AB nor BA
目录 @description@ @solution@ @accepted code@ @detail@ @description@ 给定偶数 N,求由 'A', 'B', 'C' 三种字符组成的字符 ...
- 如何用Chrome浏览器下载网页音乐视频
打开网页,先让要下载的视频播放,右键单击选择审查元素(F12),选择上方的Network选项,按F5刷新,这个时候我们可以看到框架中Size下的不少文件数据数字正在变大,按size降序排列.点击表格的 ...
- php 位运算 3<<2;
- python基础之逻辑题(1)
python基础之逻辑题(1) 1.用一行代码实现数值交换? 2.求结果--fromkeys? 3.1,2,3,4,5能组成多少个互不相同且无重复的三位数? 4.有两个字符串列表a和b,每个字符串是逗 ...
- Project Euler Problem 23-Non-abundant sums
直接暴力搞就行,优化的地方应该还是计算因子和那里,优化方法在这里:http://www.cnblogs.com/guoyongheng/p/7780345.html 这题真坑,能被写成两个相同盈数之和 ...