小象喜欢玩数组。他有一个数组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. [POI2011]ROT-Tree Rotations 线段树合并|主席树 / 逆序对

    题目[POI2011]ROT-Tree Rotations [Description] 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有\(n\)个叶子节点,满足这些权值为 ...

  2. php curl请求 header头携带参数

    $headers = array(    'api-key:'.$key,    'authorization:'.$authorization,      ); //初始化    $curl = c ...

  3. Verilog 编写规范

    在学习Python时,作者有一句话对我影响很大.作者希望我们在学习编写程序的时候注意一些业内约定的规范.在内行人眼中,你的编写格式,就已经暴露了你的程度.学习verilog也是一样的道理,一段好的ve ...

  4. java基础(十)之向上转型/向下转型

    向上转型:将子类的对象赋值给父类的引用. 向下转型:将父类的对象赋值给子类的引用. 向上转型 Student a = new Student(); Person zhang = a; 或者 Perso ...

  5. c#项目调用Python模块的方法

    将Python模块用pyinstaller打包成exe程序 下载安装UPX((http://upx.sourceforge.net/)) ,并把路径加到环境变量中. UPX是开源的加壳和压缩exe的程 ...

  6. Yum与list结合

    我用两台Linux LinuxA      IP:192.168.10.101 LinuxB      IP:192.168.10.102 首先我们在LinuxA上挂载光驱和安装FTP服务器 然后安装 ...

  7. C#中对虚拟属性和抽象属性的重写有什么异同

           public abstract class A         {             //抽象属性不能有实现代码             public abstract strin ...

  8. Docker镜像加速,设置国内源

    源地址设置 在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件) { "registry-mirrors": [ "https ...

  9. APP测试用例

    日程管理APP测试用例 测试编号 测试用例 实际结果 期望结果 测试结果(Pass/Failed) 备注 NO.1 输入正确的用户名和密码点击登录 登录成功 登录成功 Pass NO.2 点击注册界面 ...

  10. Linux - Shell - date

    概述 date 命令 准备 OS CentOS 7.6 基本功能 显示时间 格式化时间 翻译时间 转换时间格式 切换时区 设置时间 查看文件最后使用时间 1. 显示时间 概述 基本功能 命令 # 内容 ...