小象喜欢玩数组。他有一个数组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. 找到所有的txt文件并删除

    1.find /oldboy/ -type f -name "*.txt" -delete 2.find /oldboy/ -type f -name "*.txt&qu ...

  2. ajax异步获取请求,获得json数组后对数组的遍历

    如果响应数据是以html的形式发出来的,即 response.setContentType("text/html;charset=utf-8"); 那么一般用下面这种方式,但是要注 ...

  3. 云原生学习笔记(3)——Kubernetes基本概念

    学习地址:https://developer.aliyun.com/lesson_1651_13078?spm=5176.270689.1397405.6.716ef5f8Q9z1z3#_13078 ...

  4. Ultra-Thin LED Downlight Selection: 6 Things

    LED Decorative Light Manufacturer    description: ultra-thin LED downlight features can maintain the ...

  5. 剑指offer 面试题38 字符串的排列

    我惯用的dfs模板直接拿来套 class Solution { public: vector<string> Permutation(string str) { if(str.empty( ...

  6. git上传时出现ERROR: Repository not found.的解决办法

    今天在上传时出现错误,原因是之前更改了gitee上的个人空间地址,导致找不到.需要重新配置 https://gitee.com/help/articles/4114#article-header0

  7. 线段树 区间查询最大值,单体修改 hdu 1754

    #include<cstdio> #include<algorithm> #include<string.h> #include<math.h> #in ...

  8. 数据库程序接口——JDBC——API解读第一篇——建立连接的核心对象

    结构图 核心对象 Driver Java通过Driver接口表示驱动,每种类型的数据库通过实现Driver接口提供自己的Driver实现类. Driver由属性,操作,事件三部分组成. 属性 公共属性 ...

  9. MVVM架构的理解

    摘自维基百科 MVVM(Model–view–viewmodel)是一种软件架构模式. MVVM有助于将图形用户界面的开发与业务逻辑或后端逻辑(数据模型)的开发分离开来,这是通过置标语言或GUI代码实 ...

  10. 【使用python urllib时出现[SSL: CERTIFICATE_VERIFY_FAILED]报错的解决方案】

    "首先,这个报错是告诉你,你的证书有问题. 其次,出现这个问题的原因,在于Python本身. 问题原因 Python升级到2.7.9以后,引入了一个新特性. 当使用urllib打开https ...