Codeforces 220B
4 seconds
256 megabytes
standard input
standard output
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 ai.
Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, ..., arj.
Help the Little Elephant to count the answers to all queries.
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≤ ai ≤ 109). 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 lj and rj (1 ≤ lj ≤ rj ≤ n).
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.
7 2
3 1 2 2 3 3 7
1 7
3 4
3
1 题目大意:询问区间l,r间有几个数的出现次数等于数值本身。 由于题目满足由区间(l,r)-> (l,r+1)、(l,r-1)、(l-1,r)、(l+1,r)的快速转移,所以可以用莫队算法。刚接触不是太懂,等以后更加了解再更。
#include<bits/stdc++.h>
using namespace std; const int maxn=;
const int maxm=;
int n,m,unit;
struct Query{
int l,r,id;
}node[maxm]; int a[maxn],b[maxn],c[maxn],num[maxn],ans[maxm]; bool cmp(Query a,Query b) {
if(a.l/unit!=b.l/unit) return a.l/unit<b.l/unit;
else return a.r<b.r;
} int main() {
scanf("%d%d",&n,&m);
unit=(int)sqrt(n*1.0);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
for(int i=;i<=n;i++) {
c[i]=lower_bound(b+,b++n,a[i])-b;
}
for(int i=;i<=m;i++) {
node[i].id=i;
scanf("%d%d",&node[i].l,&node[i].r);
}
sort(node+,node+m+,cmp);
int l=,r=,temp=;
for(int i=;i<=m;i++) {
while(r<node[i].r) { //右扩
r++;
if(num[c[r]]+==a[r]) temp++;
else if(num[c[r]]==a[r]) temp--;
num[c[r]]++;
}
while(r>node[i].r) { //右缩
if(num[c[r]]==a[r]) temp--;
else if(num[c[r]]-==a[r]) temp++;
num[c[r]]--;
r--;
}
while(l>node[i].l) { //左扩
l--;
if(num[c[l]]+==a[l]) temp++;
else if(num[c[l]]==a[l]) temp--;
num[c[l]]++;
}
while(l<node[i].l) { //左缩
if(num[c[l]]==a[l]) temp--;
else if(num[c[l]]-==a[l]) temp++;
num[c[l]]--;
l++;
}
ans[node[i].id]=temp;
}
for(int i=;i<=m;i++) printf("%d\n",ans[i]);
}
Codeforces 220B的更多相关文章
- CodeForces 220B(B. Little Elephant and Array)
http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...
- codeforces 220B . Little Elephant and Array 莫队+离散化
传送门:https://codeforces.com/problemset/problem/220/B 题意: 给你n个数,m次询问,每次询问问你在区间l,r内有多少个数满足其值为其出现的次数 题解: ...
- 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 ...
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...
- 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排 ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
- CodeForces - 220B 离散化+莫队算法
莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- groupBy 后附加数量和每组百分比
SELECT i_State, n, , ) rat FROM ( SELECT * FROM ( ) n FROM planinfo GROUP BY i_State ) t1 ) s ) t
- Spring MVC(五)--控制器通过注解@RequestParam接受参数
上一篇中提到,当前后端命名规则不一致时,需要通过注解@RequestParam接受参数,这个注解是作用在参数上.下面通过实例说明,场景如下: 在页面输入两个参数,控制器通过注解接受,并将接受到的数据渲 ...
- Flask ——路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- 05_jQuery对象初识(三)登录案例
1.案例需求:点击登录按钮验证用户名和密码都不为空,为空就在对应的input标签下面显示一个错误的提示信息. 1.给登录的按钮绑定点击事件 2.点击事件要做的事情 1.找到input标签.取值.判断是 ...
- 01-python 学习第一天
今天是学习python语言的第一天,通过中国大学MOOC网的相关课程和其他在线教程,对python 语言有了初步的认识.今天我记住了<人生苦短,Python是岸>这句话.如果有人学习,建议 ...
- sqlmap:入门(手工注入)
一. 联合查询注入union(less-1) 1. union操作符用于合并两个或多个select语句结果集: 2. union后的select语句必须拥有和最前的select语句拥有相同数量的字段, ...
- C++给组合框控件(Combo box)加变量后不能运行
是一个BUG,找出你程序存储的位置,打开一个Debug的文件夹,将除.res文件之外的所有文件删除,然后运行,就可以了!!
- nulls_hlist原理 和 tcp连接查找
原文链接 http://abcdxyzk.github.io/blog/2018/09/28/kernel-sk_lookup/
- layui 表格点击图片放大
表格 ,cols: [[ //表头 {checkbox: true,fixed: true} ,{type: 'numbers', title: 'ID', sort: true,width:80} ...
- [转]WPF中的ControlTemplate(控件模板)
WPF中的ControlTemplate(控件模板) ...