CodeForces-220B Little Elephant and Array
小象喜欢玩数组。他有一个数组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的更多相关文章
- 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 ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces - 220B Little Elephant and Array(莫队模板题)
题意: m次查询.每次查询范围[L,R]中出现次数等于该数字的数字个数. 题解: 由于分块,在每次询问中,同一块时l至多移动根号n,从一块到另一块也是最多2倍根号n.对于r,每个块中因为同一块是按y排 ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- 【CF】220B Little Elephant and Array
区间动态统计的好题. /* */ #include <iostream> #include <string> #include <map> #include < ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 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 ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- 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 ...
随机推荐
- MySQL数据库 | 数据表的增删改查
MySQL数据的增删改查(crud) 本文结构 一.增加 create 二.修改 update 三.查询 retrieve(简单查询,下篇详细展开) 四.删除 delete 首先,创建简单的class ...
- php设计模式之简单工厂模式代码实例
<?php header("Content-type:text/html;charset=utf-8"); /** * 共同接口 */ interface db { func ...
- Docker - ubuntu 镜像安装网络工具
概述 给 ubuntu 镜像, 安装一些基本的网络工具 背景 尝试学习的时候, 需要检测网络的连通性 没有这些工具, 绕了不少弯路 工具们 ping ip a 环境 docker 18.09 ubun ...
- 每天进步一点点------verilog语言实现的分频
一 .占空比50%的任意奇数分频 如果要实现占空比为50%的三分频时钟,可以通过待分频时钟下降沿触发计数,和上升沿同样的方法计数进行三分频,然后下降沿产生的三分频时钟和上升沿产生的时钟进行相或运算,即 ...
- Java代码如何关联Hadoop源码
昨天在学习Hadoop的时候,遇到一个问题就是Java关联Hadoop源码的时候死活关联不上,但是我发现在公司的电脑就可以顺利关联到源码.考虑了一下觉得应该是Eclipse版本的问题,于是我下载了ec ...
- 在页面跳转的时候,在跳转后的页面中使用js 获取到 页面跳转的url中携带的参数。
common.js代码 //获取URL中的参数..等等function getQueryString(name){var reg = new RegExp("(^|&)"+ ...
- Oracle 11G统计信息自动收集及调整
查询统计信息的收集所对应的task,以及当前状态 col CLIENT_NAME for a50col TASK_NAME for a20SELECT client_name, task_name, ...
- Java 前加加和后加加 总结
public class Test { public static void main(String[] args) { int age = 6; //先自加,再使用(age先自加1,然后再打印age ...
- html 打印相关操作与实现
2020-02-19 | chrome 79.0.3945.130 测试无问题 未做兼容测试 原理为调用 window.print() 方法,但是该方法只能对当前页面全部打印,所以有了以下方案来解决局 ...
- acm数论之旅--中国剩余定理
ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯) 中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...