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. Cutting Game

    Cutting Game 刚开始有一\(n\times m\)的矩形网格纸,双方轮流操作,剪网格纸,对于任意一个局面而言,你可以选择其中一张网格纸,把它剪成两个长宽都是整数的网格纸,剪出\(1\tim ...

  2. Excel生成Oracle数据库表sql工具类

    1.解决问题: 开发文档中字段比较多的时候,建表sql(Oracle下划线命名规范)比较麻烦,容易出错~~ (主要是懒) 特意手写一个工具,根据excel字段,生成建表的sql语句. ~~~末尾附Gi ...

  3. VUE环境下获取当前时间并格式化--按秒数更新

    <el-col :span="8"><div class="grid-content title-time"> {{date}}< ...

  4. 19-10-28-A

    竟然?竟然?竟然? 我已经用了半个键盘的编号了$\text{T_T}$ $\mathbb{AFO}$感稍强 h1是不是有点大? ZJ+TJ: T1 以为是什么数据结垢,但是是个链表. 所以可以使用 v ...

  5. 洛谷P5104 红包发红包

    题目链接: P5104 题目分析: 题目和\(n\)是没什么关系的,因为是\(n\)个人抢,其实不一定抢完 其实很显然--就是求一个连续型随机变量的期望 首先设一个随机变量\(X\),表示第一个人拿到 ...

  6. 杂项-公司:IBM

    ylbtech-杂项-公司:IBM IBM (IT公司-国际商业机器公司) IBM(国际商业机器公司)或万国商业机器公司,简称IBM(International Business Machines C ...

  7. 微信小程序 拼团商品倒计时(拼团列表、拼团商品详情)

    直接上图: 拼团列表.拼团详情-倒计时                                    //单个倒计时,适用用于单个商品的倒计时 js文件: //倒计时 function cou ...

  8. python基础数据类型初始,用户交互

    一.基础数据类型初始 1.数字:int  1,2,3 print(100,type(100)) ',type('100')) 查看数据类型的方法:type()函数 取值范围: int(整型) 在32位 ...

  9. springboot 2 修改端口号

    springboot 废弃了EmbeddedServletContainerCustomizer ,修改端口,从官方文档上看到的方法, 1 import org.springframework.boo ...

  10. 解决linux机器克隆后eth0不见的问题

    克隆机器之后,两个几的物理地址和ip地址是一样的,导致克隆的机器网络不可用,可以通过通过如下步骤修改: 通过ifconfig –a   命令可查看所有的ip地址配置. 通过这个命令可以发现有一个eth ...