Codeforces 221 D. Little Elephant and Array
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 numbersalj, 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]内出现次数等于它本身的数的个数 莫队算法
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,siz,ans;
int a[],hash[];
int sum[];
struct node
{
int bl,id;
int l,r,k;
}e[];
bool cmp(node p,node q)
{
if(p.bl!=q.bl) return p.bl<q.bl;
return p.r<q.r;
}
bool cmp2(node p,node q)
{
return p.id<q.id;
}
void update(int pos,int w)
{
if(sum[a[pos]]!=hash[a[pos]]&&sum[a[pos]]+w==hash[a[pos]]) ans++;
else if(sum[a[pos]]==hash[a[pos]]&&sum[a[pos]]+w!=hash[a[pos]]) ans--;
sum[a[pos]]+=w;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]),hash[i]=a[i];
siz=sqrt(n);
sort(hash+,hash+n+);
int cnt=unique(hash+,hash+n+)-(hash+);
for(int i=;i<=n;i++) a[i]=lower_bound(hash+,hash+cnt+,a[i])-hash;
int ll,rr;
for(int i=;i<=m;i++)
{
scanf("%d%d",&ll,&rr);
e[i].id=i;
e[i].bl=(ll-)/siz+;
e[i].l=ll;
e[i].r=rr;
}
sort(e+,e+m+,cmp);
int l=,r=,opl,opr;
for(int i=;i<=m;i++)
{
opl=e[i].l; opr=e[i].r;
while(l>opl) update(--l,);
while(l<opl) update(l++,-);
while(r<opr) update(++r,);
while(r>opr) update(r--,-);
e[i].k=ans;
}
sort(e+,e+m+,cmp2);
for(int i=;i<=m;i++) printf("%d\n",e[i].k);
}
Codeforces 221 D. Little Elephant and Array的更多相关文章
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- Codeforces 221 C. Little Elephant and Problem
C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 221 E. Little Elephant and Shifts
E. Little Elephant and Shifts time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 221 B. Little Elephant and Numbers
B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 221 A. Little Elephant and Function
A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...
- CodeForces 220B(B. Little Elephant and Array)
http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- 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 ...
随机推荐
- Android开发第二阶段(7)
今天:对项目的最后总结,宣传给下届学生做准备.为了更好的了解和深入书写本次项目的总结随笔.
- 软工实践-Alpha 冲刺 (6/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- 搜索引擎Elasticsearch REST API学习
Elasticsearch为开发者提供了一套基于Http协议的Restful接口,只需要构造rest请求并解析请求返回的json即可实现访问Elasticsearch服务器.Elasticsearch ...
- app测试更多机型系统解决方法
手头上测试机有限,不可能每个机型每个系统都 有一部手机,此时寻求一个什么都有的测试平台就显得尤为重要了. 作为小白的我刚刚使用了一波腾讯优测,简单粗暴有效给力,而且新注册认证用户还有60min免费使用 ...
- Android ContentProvider基本用法
转自:https://www.jianshu.com/p/601086916c8f 一.基本概念 ContentProvider是Android系统中提供的专门用户不同应用间进行数据共享的组件,提供了 ...
- 查询出menupath字段中 出现 “- "(横杆)大于3次的 记录
- default.properties文件
在地址栏访问某个 action 之所以能访问到,只因为在 default.properties 配置文件中有一个键值对,key 为struts.action.extension,值为 action,, ...
- 第107天:Ajax 实现简单的登录效果
使用 Ajax 实现简单的登录效果 Ajax是一项使局部网页请求服务器信息,而不需整体刷新网页内容的异步更新技术.这使得向服务器请求的数据量大大减少,而且不会因局部的请求失败而影响到整体网页的加载. ...
- 【.Net】Net开发
博客里的好多文章都是本人看着比较好,就转过来的,好少自己亲自去写点什么,也很少把自己学的一点心得于大家分享,今天特别想聊一下,关于本人做Net开发时的那段回忆! 一.关于知识的回忆 还记得Handle ...
- Collections带有的排序方法 传入的元素类型 需是子类或者这个类的实例