更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

Description

The Little Elephant loves playing with arrays. He has array \(a\), consisting of \(n\) positive integers, indexed from \(1\) to \(n\). Let's denote the number with index \(i\) as \(a_i\).

Additionally the Little Elephant has \(m\) queries to the array, each query is characterised by a pair of integers \(l_j\) and \(r_j (1 \le l_j \le r_j \le n)\). For each query \(l_j, r_j\) the Little Elephant has to count, how many numbers \(x\) exist, such that number \(x\) occurs exactly \(x\) times among numbers \(a_{l_j}, a_{l_j + 1}, \cdots , a_{r_j}\).

Help the Little Elephant to count the answers to all queries.

Input

The first line contains two space-separated integers \(n\) and \(m (1 \le n, m \le 105)\) - the size of array a and the number of queries to it. The next line contains \(n\) space-separated positive integers \(a_1, a_2, \cdots , a_n (1 \le ai \le 10^9)\). Next \(m\) lines contain descriptions of queries, one per line. The \(j\)-th of these lines contains the description of the \(j\)-th query as two space-separated integers \(l_j\) and \(r_j (1 \le l_j \le r_j \le n)\).

Output

In \(m\) lines print \(m\) integers - the answers to the queries. The \(j\)-th line should contain the answer to the \(j\)-th query.

Sample Input1

7 2
3 1 2 2 3 3 7
1 7
3 4

Sample Output

3
1

Description in Chinese

小象喜欢和数组玩。现在有一个数组\(a\),含有\(n\)个正整数,记第\(i\)个数为\(a_i\)。

现在有\(m\)个询问,每个询问包含两个正整数\(l_j\)和\(r_j (1 \le l_j \le r_j \le n)\),小象想知道在\(a_{l_j}到\)a_{r_j}\(之中有多少个数\)x\(,其出现次数也为\)x$。

Solution

我们先看题目,发现只有查询,没有修改,所以可以用普通的莫队解决。

题目中的\(a_i\)的范围是\(\in [1, 10^9]\),而数的总数的范围是\(\in [1, 10^5]\),所以当这个数大于\(10^5\)了,这个数就不可能为所求的\(x\)忽略这个数后就不用进行离散化了。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; const int MAXN = 100005;
int n, m, nowans, 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 ;//如果数值超了n的范围就退出
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);
int block = (int)sqrt(n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
bl[i] = i / block;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d", &info[i].l, &info[i].r);//莫队是离线算法,需要记录询问的左右端点
info[i].id = i;//记录每个询问的编号
}
sort(info + 1, info + m + 1);
memset(cnt, 0, sizeof(cnt));
int l = 1, r = 0;
for (int i = 1; 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 = 1; i <= m; i++)
printf("%d\n", ans[i]);
return 0;
}

『题解』Codeforces220B Little Elephant and Array的更多相关文章

  1. 『题解』洛谷P1063 能量项链

    原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...

  2. 『Python』内存分析_list和array

    零.预备知识 在Python中,列表是一个动态的指针数组,而array模块所提供的array对象则是保存相同类型的数值的动态数组.由于array直接保存值,因此它所使用的内存比列表少.列表和array ...

  3. 『题解』Codeforces1142B Lynyrd Skynyrd

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...

  4. 『题解』Codeforces446C DZY Loves Fibonacci Numbers

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...

  5. 『题解』Codeforces1142A The Beatles

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...

  6. 『题解』洛谷P1993 小K的农场

    更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...

  7. 『题解』洛谷P2296 寻找道路

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...

  8. 『题解』洛谷P1351 联合权值

    更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...

  9. 『题解』Codeforces656E Out of Controls

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...

随机推荐

  1. k8s中负载均衡器【ingress-nginx】部署

    在Kubernetes中,服务和Pod的IP地址仅可以在集群网络内部使用,对于集群外的应用是不可见的.为了使外部的应用能够访问集群内的服务,在Kubernetes 目前 提供了以下几种方案: Node ...

  2. Io流的概述

    Io流的概述IO: I输入(Input),O 输出(Output)1.什么是IO流? 数据流,IO是严格的“水流模型” 所以IO流是用来读写数据,或者传输数据. 注意:File只能操作文件对象本身,而 ...

  3. MyEclipse10 使用JRebel实现热部署

    MyEclipse10 使用JRebel实现热部署 Window --Preferences-Tomcat 6.x-JDK-JVM -noverify -javaagent:D:\JRebel\jre ...

  4. shark恒破解笔记2-绕过自校验

    这集讲的是绕过自校验 主要是通过文件大小的自校验 首先查壳 有壳  可以用esp定律搞定 OD载入  右键od脱裤壳调试进程 可以看到一些信息 包括入口点252F0 修正后地址为252F0 loadP ...

  5. 三种常见字符编码:ASCII、Unicode和UTF-8

    什么是字符编码? 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节(byte),所以,一个字节能表示的最大的整数就是255( ...

  6. LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)

    LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...

  7. 题解 CF600E 【Lomsat gelral】

    没有多少人用莫队做吗? 蒟蒻水一波莫队 这是一道树上莫队好题. 时间复杂度(\(n\sqrt{n}logn\)) 蒟蒻过菜,不会去掉logn的做法qaq 思路很简单: 1.dfs跑一下树上点的dfs序 ...

  8. python常用算法(6)——贪心算法,欧几里得算法

    1,贪心算法 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的的时在某种意义上的局部最优解. 贪心算法并不保证会得到最优解,但 ...

  9. java集合第一节,List简单介绍

    Java中List集合的常用方法   List接口是继承Collection接口,所以Collection集合中有的方法,List集合也继承过来. package 集合; import java.ut ...

  10. lnmp安装mysql

    lnmp安装mysql 下载lnmp wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz 解压 tar ‐xf lnmp1..tar.gz 安装数据库 ./i ...