『题解』Codeforces220B Little Elephant and Array
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的更多相关文章
- 『题解』洛谷P1063 能量项链
原文地址 Problem Portal Portal1:Luogu Portal2:LibreOJ Portal3:Vijos Description 在\(Mars\)星球上,每个\(Mars\)人 ...
- 『Python』内存分析_list和array
零.预备知识 在Python中,列表是一个动态的指针数组,而array模块所提供的array对象则是保存相同类型的数值的动态数组.由于array直接保存值,因此它所使用的内存比列表少.列表和array ...
- 『题解』Codeforces1142B Lynyrd Skynyrd
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently Lynyrd and Skynyrd went to a ...
- 『题解』Codeforces446C DZY Loves Fibonacci Numbers
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...
- 『题解』Codeforces1142A The Beatles
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description Recently a Golden Circle of Beetlovers ...
- 『题解』洛谷P1993 小K的农场
更好的阅读体验 Portal Portal1: Luogu Description 小\(K\)在\(\mathrm MC\)里面建立很多很多的农场,总共\(n\)个,以至于他自己都忘记了每个农场中种 ...
- 『题解』洛谷P2296 寻找道路
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 在有向图\(\mathrm G\)中,每条边的长度均为\(1\),现给定起点和终点 ...
- 『题解』洛谷P1351 联合权值
更好的阅读体验 Portal Portal1: Luogu Portal2: LibreOJ Description 无向连通图\(\mathrm G\)有\(n\)个点,\(n - 1\)条边.点从 ...
- 『题解』Codeforces656E Out of Controls
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description You are given a complete undirected gr ...
随机推荐
- Adobe PS常用快捷键
ps使用快捷键 新建图层 Ctrl+Shift+N 取消选择区 Ctrl + D 新建标题 Ctrl + N 图片放大 Alt+鼠标滑动 图片位置拖动 空格 + 鼠标拖动 移动图层 ...
- 使用Jmeter并发websocket协议项目
1.安装Jmeter 网址:http://jmeter.apache.org/下载 2.启动Jmeter \apache-jmeter-5.1.1\bin\jmeter.bat 3.安装‘Plugin ...
- oracle查询当前用户下所有的表,包括所有的字段
oracle查询当前用户下所有的表,包括所有的字段 背景: 前两天接到一个需求,做一个展示所有表名,表备注,表数据,表字段数,点击查看按钮查看字段名和注释,支持导出. 在Oracle中,可用使用视 ...
- 域渗透基础之Windows 2012创建域控制器
创建备份域控制器 这里就拿windows 2012 R2来当备份域控 如果一个域内有多个域控制器,可以有如下好处. 提高用户登录的效率:如果同时有多台域控制器对客户提供服务,可以分担审核用户登录身份( ...
- Python编程系列---装饰器执行的底层原理及流程
代码中No.1 No.2 ...表示执行流程 """No.1 No.2 ...表示执行流程""" def set_func(func): ...
- 《HTML5+CSS3+JavaScript 从入门到精通(标准版)》学习笔记(一)
以下是以代码形式书写的笔记,本系列会持续更新,主要内容预计是类似下文的笔记,兼或一些思考与小项目,希望对你会有所帮助 1 <!-- --> <!DOCTYPE html>< ...
- Mybatis使用自定义类型转换Postgresql
Mybatis使用自定义类型转换Postgresql 主要目的 为了解决从数据库取出来之后再手动转换为javaBean的问题. 主要用mybatis提供的Handler来把处理前置 添加转换类 imp ...
- MongoDB实现问卷/考试设计
MongoDB的特点 MongoDB是一个面向文档存储的数据库.在MongoDB中,一条记录叫做document(文档),由类似于JSON结构的键值对组成. 由于类似于MongoDB直接存储JSON的 ...
- [最新方法]终于解决了 Ubuntu 14.04 网络图标不见了 的问题|Ubuntu14.04 网络图标消失
解决 Ubuntu 14.04 网络图标不见了 消失的问题 这个问题困扰了我大半年了.但是我就硬是不想重新装系统.搜索研究一番发现,这个问题是nm-applet的问题. 然后偶然发现nm-ap ...
- 你好,Go语言
本文是「vangoleo的Go语言学习笔记」系列文章之一. 官网: http://www.vangoleo.com/go/hello-golang/ 我在2015年第一次接触Go语言,完成了Hello ...