B. Little Elephant and Array
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The Little Elephant loves playing with arrays. He has array a, consisting of n positive 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, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs 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
input

Copy
7 2
3 1 2 2 3 3 7
1 7
3 4
output

Copy
3
1 题目大意:询问区间l,r间有几个数的出现次数等于数值本身。 由于题目满足由区间(l,r)-> (l,r+1)、(l,r-1)、(l-1,r)、(l+1,r)的快速转移,所以可以用莫队算法。刚接触不是太懂,等以后更加了解再更。
 #include<bits/stdc++.h>
using namespace std; const int maxn=;
const int maxm=;
int n,m,unit;
struct Query{
int l,r,id;
}node[maxm]; int a[maxn],b[maxn],c[maxn],num[maxn],ans[maxm]; bool cmp(Query a,Query b) {
if(a.l/unit!=b.l/unit) return a.l/unit<b.l/unit;
else return a.r<b.r;
} int main() {
scanf("%d%d",&n,&m);
unit=(int)sqrt(n*1.0);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
for(int i=;i<=n;i++) {
c[i]=lower_bound(b+,b++n,a[i])-b;
}
for(int i=;i<=m;i++) {
node[i].id=i;
scanf("%d%d",&node[i].l,&node[i].r);
}
sort(node+,node+m+,cmp);
int l=,r=,temp=;
for(int i=;i<=m;i++) {
while(r<node[i].r) { //右扩
r++;
if(num[c[r]]+==a[r]) temp++;
else if(num[c[r]]==a[r]) temp--;
num[c[r]]++;
}
while(r>node[i].r) { //右缩
if(num[c[r]]==a[r]) temp--;
else if(num[c[r]]-==a[r]) temp++;
num[c[r]]--;
r--;
}
while(l>node[i].l) { //左扩
l--;
if(num[c[l]]+==a[l]) temp++;
else if(num[c[l]]==a[l]) temp--;
num[c[l]]++;
}
while(l<node[i].l) { //左缩
if(num[c[l]]==a[l]) temp--;
else if(num[c[l]]-==a[l]) temp++;
num[c[l]]--;
l++;
}
ans[node[i].id]=temp;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
}
 

Codeforces 220B的更多相关文章

  1. CodeForces 220B(B. Little Elephant and Array)

    http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...

  2. codeforces 220B . Little Elephant and Array 莫队+离散化

    传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...

  3. Codeforces 220B - Little Elephant and Array 离线树状数组

    This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...

  4. Little Elephant and Array CodeForces - 220B(莫队)

    给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...

  5. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  6. Codeforces - 220B Little Elephant and Array(莫队模板题)

    题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排 ...

  7. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

  8. CodeForces - 220B 离散化+莫队算法

    莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...

  9. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

随机推荐

  1. python中使用xlrd、xlwt操作excel

    python 对 excel基本的操作如下: # -*- coding: utf-8 -*- import xlrd import xlwt from datetime import date,dat ...

  2. [JZOJ4633] 【GDOI2017模拟7.15】萌萌哒

    题目 描述 题目大意 给你一个数列,接下来有许多个操作,使得区间[l1,r1][l_1,r_1][l1​,r1​]和[l2,r2][l_2,r_2][l2​,r2​]对应的位置染上同样的颜色(使得它们 ...

  3. @Component 和 @Bean 的区别

    Spring帮助我们管理Bean分为两个部分,一个是注册Bean,一个装配Bean.完成这两个动作有三种方式,一种是使用自动配置的方式.一种是使用JavaConfig的方式,一种就是使用XML配置的方 ...

  4. C++ const修饰不同类型的用法

    const取自constant的缩写,本意是不变的,不易改变的意思 一.修饰普通变量 const int a = 7; int b = a;         //正确 a = 8;           ...

  5. webGL动画

    在做这个项目之前,我也和很多人的想法一样觉得:H5做动画性能不行,只能完成简单动画,可是事实并非如此.所以借此篇分享振奋下想在H5下做酷炫游戏的人心. 体验游戏请长按二维码识别: 好吧,知道你懒.不想 ...

  6. Django的日常-视图层

    目录 Django的日常-3 JsonResponse form表单上传文件 CBV的源码分析 视图层 模板传值 过滤器 标签 自定义过滤器和标签 Django的日常-3 JsonResponse 在 ...

  7. python collections模块 之 orderdict

    普通字典善于隐射,其次追踪插入顺序.而 orderdict 更善于后者.因为 orderdict 内部维护了一个双向链表,大小会是普通字典的两倍. 增加方法: popitem(last=True) 移 ...

  8. Chapter 4 图

    Chapter 4 图 . 1-   图的存储结构 无向图:对称 有向图:…… 2-   图的遍历 1   深度优先搜索(DFS) 类似于二叉树的先序遍历 2   广度优先搜索(BFS) 类似于二叉树 ...

  9. PKUOJ 区间内的真素数

    http://bailian.openjudge.cn/tm2018/A/ #include <iostream> #include <math.h> #include < ...

  10. PKU 百炼OJ 简单密码

    http://bailian.openjudge.cn/practice/2767/ #include<iostream> #include <cmath> #include ...