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 ...
随机推荐
- CS小分队第二阶段冲刺站立会议(5月28日)
昨日成果:昨天对我们的软件的主界面进行了思考,考虑到许多人建议我们团队添加可以自主增加软件快捷键的功能,我对这一想法的可行性和项目总体策划进行评估分析后,决定正式实施:已经完成从电脑上添加文件在我们的 ...
- Alpha 冲刺(6/10)
队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 测试服务器并行能力 学习MSI.CUDA ...
- asp.netcore mvc 权限拦截
1-背景介绍 需要做一个简单权限系统,基于 角色,用户,菜单 的模式 基于IActionFilter全局拦截,在内部跳转或者浏览器跳转的时候,拦截是成功的,当通过AJAX 请求的时候,页面就不会跳转 ...
- DB2 V9 默认帐户信息和服务启动信息
1 dasusr1 DB2 管理服务器用户是管理DAS(Database Adminitrator Service).要完全适用db2 cc 必须启动DAS.DB2 管理服务器(DAS)响应来自 DB ...
- java 基础 --File
1, 创建文件 File file = new File(path); file.createNewFile(); //如果路径不存在,会抛异常 file.mkdir();//如果路径不存在,返回fa ...
- 赋予Winform程序管理员访问权限
业务场景:Winform操作系统盘文件夹时进行文件的读写时,会需要管理员权限打开文件. 解决方案: 在项目文件中找到app.manifest文件打开. 替换requestedExecutionLeve ...
- poj2914-Minimum Cut
题意 \(n\) 个点 \(m\) 条边的无向带权图求全局最小割.\(n\le 500,m\le \frac{n(n-1)}{2}\) . 分析 参考了 这篇博客,去给他点赞. 嘛,今天研究了一下全局 ...
- 多线程---handlerthread
当我们需要工作线程来操作的时候,很多时候会有同步问题,UI更新问题. Handle机制就是为了解决这个问题而产生的. android允许每个线程都有自己的消息队列,同时也可以是主线程消息队列. 但是很 ...
- jsp和js中获取项目路径名
一.jspa.<%=request.getContextPath()%>//结果:/projectNameb.${pageContext.request.contextPath}//结果: ...
- 前端开发学习之——利用模板实现涉及url问题时的bug分析及解决(chrome源码)
例如我们要实现如下页面,其中历史页面列表想来自底层返回的数据,此处用testData代替: 最初我写的实现代码如下: html文件: <!doctype html> <html cl ...