小象喜欢玩数组。他有一个数组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. FastDFS :java.lang.Exception: getStoreStorage fail, errno code: 28

    FastDFS 服务正常,突然报错:java.lang.Exception: getStoreStorage fail, errno code: 28 答:错误代码28表示 No space left ...

  2. 6.mybatis----日志工厂

    日志工厂 如果一个数据库操作出现了异常,我们需要排错,所以说日志就是最好的助手 曾经:sout,debug 现在:日志工厂 在Mybatis中具体使用哪一个日志,在设置中设定 咋设定? 在mybati ...

  3. java_基础_注解

    注解(annotation),不是注释(comment) 注解可以对程序做说明,这一点和注释一样但是,注解还可以被其他程序读取,这是注释所不具备的 内置注解:@Override(表示重写父类方法)—— ...

  4. js函数防抖和函数节流

    参考链接:https://juejin.im/post/5b651dc15188251aa30c8669 参考链接:https://www.jb51.net/article/158818.htm 在我 ...

  5. docker dial unix /var/run/docker.sock: connect: permission denied

    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker. ...

  6. MFC单文档视图中嵌入GLFW窗口

    开始学习OpenGL由于有一段时间,但是glfw只有窗口区,虽然通过某种手段(移步这里)可以加入工具栏,但仍然无法作为一个标准的GUI,而直接在MFC或Qt里面使用OpenGL API感觉有诸多制肘, ...

  7. JS高级---为内置对象添加原型方法

    为内置对象添加原型方法 我们能否为系统的对象的原型中添加方法, 相当于在改变源码   我希望字符串中有一个倒序字符串的方法 //我希望字符串中有一个倒序字符串的方法 String.prototype. ...

  8. Java+Selenium自动化测试学习(一)

    自动化测试基本流程 1.设置chromedriver的地址System.setProperty(); 2.创建一个默认浏览器ChromeDriver driver = new ChromeDriver ...

  9. swing开发一个修改项目数据库连接参数配置文件

    我们在开发web项目中,经常有properties配置文件配置数据库连接参数,每次修改的时候还要去找到配置文件,感觉有点麻烦,就用swing做了个小工具修改参数,运行界面如下: =========== ...

  10. PHP+Mysql防止SQL注入的方法

    这篇文章介绍的内容是关于PHP+Mysql防止SQL注入的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 方法一: mysql_real_escape_string -- 转义 S ...