小象喜欢玩数组。他有一个数组a,由n个正整数组成,从1到n进行索引。让我们用索引i表示数字ai。
此外,小象对数组还有m个查询,每个查询的特征是一对整数lj和rj(1 ≤ lj ≤ rj ≤ n)。对于每一个查询LJ,小的大象必须计数,有多少个X数字存在,这样X数恰好在alj,alj+1…arj出现,X数为1。
帮助小象计算所有问题的答案。

输入

第一行包含两个空格分隔的整数n和m(1 ≤ n, m ≤ 105)-数组a的大小和对它的查询数。下一行包含n个空格分隔的正整数a1、a2、…、an(1 ≤ ai ≤ 109)。接下来的m行包含查询的描述,每行一个。这些行的j-th包含对j-th查询的描述,即两个空格分隔的整数lj和rj(1 ≤ lj ≤ rj ≤ n)。

输出

在m行中打印m个整数-查询的答案。第j行应该包含第j个查询的答案。

Examples
Input
7 2
3 1 2 2 3 3 7
1 7
3 4
Output
3
1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; const int maxn = 1e5 + ; int n, m, nowans, block, l, r;
int a[maxn], bl[maxn], ans[maxn], cnt[maxn]; struct node {
int l, r, id; bool operator<(const node &x) const {
return bl[l] == bl[x.l] ? r < x.r : bl[l] < bl[x.l]; }
} info[maxn]; inline void add(int x) {
if (a[x] > n) return;
if (cnt[a[x]] == a[x])
nowans--;
cnt[a[x]]++;
if (cnt[a[x]] == a[x])
nowans++;
} inline void dec(int x) {
if (a[x] > n) return;
if (cnt[a[x]] == a[x])
nowans--;
cnt[a[x]]--;
if (cnt[a[x]] == a[x])
nowans++;
} int main() {
scanf("%d%d", &n, &m);
block = (int) sqrt(n);
for (int i = ; i <= n; i++) {
scanf("%d", &a[i]);
bl[i] = i / block;
}
for (int i = ; i <= m; i++) {
scanf("%d%d", &info[i].l, &info[i].r);
info[i].id = i;
}
sort(info + , info + m + );
memset(cnt, , sizeof(cnt)); l = ;
r = ;
for (int i = ; i <= m; i++) {
while (l < info[i].l)
dec(l++);
while (l > info[i].l)
add(--l);
while (r < info[i].r)
add(++r);
while (r > info[i].r)
dec(r--);
ans[info[i].id] = nowans;
} for (int i = ; i <= m; i++)
printf("%d\n", ans[i]);
}

CodeForces-220B Little Elephant and Array的更多相关文章

  1. 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 ...

  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 (莫队+离散化 / 离线树状数组)

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

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

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

  5. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  6. 【CF】220B Little Elephant and Array

    区间动态统计的好题. /* */ #include <iostream> #include <string> #include <map> #include < ...

  7. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  8. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  9. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  10. Codeforces Round #136 (Div. 1) B. Little Elephant and Array

    B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. php设计模式之多态实例代码

    <?php header("Content-type:text/html;charset=utf-8"); /** * 虎 */ abstract class Tiger { ...

  2. 题解【洛谷P1807】最长路_NOI导刊2010提高(07)

    题面 题解 最长路模板. 只需要在最短路的模板上把符号改一下\(+\)初值赋为\(-1\)即可. 注意一定是单向边,不然出现了正环就没有最长路了,就好比出现了负环就没有最短路了. 只能用\(SPFA\ ...

  3. HDU 3530

    新手理解(可能有理解错误的地方,请指教,嘿嘿) #include<stdio.h> #include<string.h> #include<math.h> #inc ...

  4. HTML学习(7)格式化标签

    对文本格式进行编辑的标签.常用: <b>加粗文本</b> <strong>加重语气</strong>   与<b>效果一样,<stro ...

  5. 2019牛客暑期多校训练营(第三场) J LRU management 模拟链表操作

    输入n, m,n表示n种操作,m表示最多可以容纳m个串. 第一种操作:先在容器里找是否存在这个串,如果不存在,则添加在末尾,这个串携带了一个值v. 如果存在,则先把之前存在的那个拿出来,然后在后面添加 ...

  6. opencv:图像噪声

    常见噪声的类型: 椒盐噪声 高斯噪声 其他噪声...... 手动生成图像噪声: #include <opencv2/opencv.hpp> #include <iostream> ...

  7. Mongodb学习笔记(二)Capped Collection固定集合

    一.Capped Collection固定集合 简单介绍 capped collections是性能出色的有着固定大小的集合(定容集合),以LRU(Least Recently Used最近最少使用) ...

  8. Boxes and Candies

    问题 G: Boxes and Candies 时间限制: 1 Sec  内存限制: 128 MB[提交] [状态] 题目描述 There are N boxes arranged in a row. ...

  9. Python3.6打开EAIDK-610开发板(计算机通用)摄像头拍照并保存

    环境:python3.6 代码: import cv2 import os output_dir ='/home/openailab/Desktop/huahui/came/' i = cap = c ...

  10. python接口自动化测试 - unittest框架基本使用

    unittest简单介绍 单元测试框架 还可以适用WEB自动化测试用例的开发与执行 提供丰富的断言方法 官方文档:https://docs.python.org/zh-cn/3/library/uni ...